Creating custom module!

Couldn’t resist, so I decided to solve the annoying problems. As I’m newbie to drupal, it took some time, but not long.

Prerequisites:

  • Drupal modules basically works by defining hooks.
  • Execution order of different modules is determined by their weights.

That is, you can override results of other modules without modifying them by defining a module with bigger weight. Actually, many sites seems to be constructed that way, to minimize inconvenience during upgrade.

Creating module

First, decide a module name. Short and lowercase-only one. (I chose “moonz”.) After that, create a directory called sites/all/modules/. Minimum required files are .info and .module.

moonz.module

This is a php source file which defines hooks. Hook function should be named _. Here name of hook to override is “form_alter”, so function name becomes moonz_form_alter. I’ve read sources of form_alter hook of pathauto module and twitter module, and complement what I need.

<?php
// $Id$

function moonz_form_alter(&$form, $form_state, $form_id) {
  if (isset($form[‘twitter’])) {
    $form[‘twitter’][‘#collapsed’] = TRUE;
    $form[‘twitter’][‘post’][‘#default_value’] = FALSE;
  }

  $node = $form[‘#node’];
  if (empty($node->nid) && isset($node->pathauto_perform_alias)) {
    $node->pathauto_perform_alias = FALSE;
    $form[‘path’][‘pathauto_perform_alias’][‘#default_value’] = FALSE;
  }
}

By the way, missing ?&gt; which closes &lt;?php is intentional. (Manual says that there is problematic configurations when it presents.)

moonz.info

This is a meta info file for the module, and drupal recognize a module by this file’s existence.

; $Id$
name = MooNz.net customization
description = MooNz.net customization
core = 6.x
version = 6.x-1.0

Now, module can be installed.

Adjust weight

Module’s weight doesn’t exposed at the level of drupal core. One way is creating moonz.install file and setting the weight by DB update query. Otherwise, you can install the Utility module. For now, it’s sufficient if weight is bigger than (= executed later than) twitter and pathauto module. I’ve set it to moderately big value, 9999.