A Basic Drupal Module

My co-worker, Joe Tower, is a designer/developer who is just getting into coding his own Drupal modules. Over at his blog, earlier this weekend, he created a basic module. I thought it needed a couple more things, so I'm gonna pick up where he left off. You might learn a bit or two about how the innards of Drupal work if you keep reading.

First of all, I took his basic my_menu_module_form_content_type_form_alter() function and made it a bit more general. This version of the function, using the less specific hook_form_alter() function, is defined as my_menu_module_form_alter(). This code below searches for any content add/edit form and sets the checkbox to default to checked:

function my_menu_module_form_alter(&$form, &$form_state, $form_id) {
  if (substr($form_id, -10) == '_node_form') {
    $form['menu']['enabled']['#default_value'] = TRUE;
  }
]

I'm pretty sure that's not exactly what we want to do, though. This is where Drupal's built-in variable system is very helpful. Using the functions variable_set() and variable_get(), you can set/get small bits of data very easily. In this case, we're just setting "1" or "0", which is synonymous in this case to TRUE or FALSE. Therefore, this is the version of the above function where we get the content type name from the form id and then check a variable that is specific to that content type.

function my_menu_module_form_alter(&$form, &$form_state, $form_id) {
  if (substr($form_id, -10) == '_node_form') {
    $content_type = substr($form_id, 0, -10);
    $form['menu']['enabled']['#default_value'] = variable_get('my_menu_module_' . $content_type . '_enabled', FALSE);
  }
}

Now, we need to create a checkbox on the Content Type edit page. To do that, we'll use another form_alter function:

function my_menu_module_form_node_type_form_alter(&$form, &$form_state, $form_id) {
  $form['menu']['my_menu_module_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Default the "Provide a menu link" item to checked.'),
    '#default_value' => variable_get('my_menu_module_' . $form['#node_type']->type . '_enabled', FALSE),
  );
  $form['#submit'][] = 'my_menu_module_type_form_submit';
}
 
function my_menu_module_type_form_submit($form, &$form_state) {
  variable_set('my_menu_module_' . $form_state['values']['type'] . '_enabled', $form_state['values']['my_menu_module_enabled']);
  return;
}

Also, as you can see, I added a submit function that saves our custom variable. You'll also notice that the variable_get() function always expects a second variable which is the default value if this variable is not set. So, now you should have a much more functional module that has a checkbox on each content type's admin interface to decide if this will be defaulting to true on your site's content types.

Update: My co-workers have created a Drupal module out of this idea. Check out the Default Menu Link module. It might just help you out.

Categories: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.