Main menu

Mar 30 2011

Drupal is nice! I just realised how easy it is to save content type specific settings. That is, settings that you want to store in Drupal’s variables table for each content type.

Instead of creating a submit function to store your settings in the variables table, Drupal will do that just for you automatically!

How to store content type specific settings

Step 1: Add form elements on the content type settings form

Just perform a hook_form_FORM_ID_alter to add your formfields on the content type edit form (yourmodule_form_node_type_form_alter), as shown below.

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * We want to let the configurator decide if node authors
 * can set limited access to their node
 * and what the default values for that
 * content type should be.
 *
 */
function takealook_form_node_type_form_alter(&$form, $form_state) {

  // get the content type so we can do a variable_get
  // to retrieve the default values
  $type = $form['#node_type']->type;
  
   $form['takealook'] = array(
    '#type' => 'fieldset',
    '#title' => t('Take a look settings'),
    '#description' => t('Determine if user\'s can limit the access to a page.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
  );

  $form['takealook']['takealook_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable node authors to decide to temporary restrict access to their node.'),
    '#default_value' => 0,
    '#return_value' => variable_get('takealook_enabled_' .  $type, 1),
  );
 
  $form['takealook']['takealook_threshold'] = array(
    '#type' => 'textfield',
    '#title' => t('Threshold'),
    '#description' => t('Set the default threshold level. This is the number of times a user can access the content before being temporarily blocked'),
    '#default_value' => variable_get('takealook_threshold_' . $type, 1),
  );

  $form['takealook']['takealook_window'] = array(
    '#type' => 'textfield',
    '#title' => t('Window'),
    '#description' => t('Set the default time window for which the user can access the content. It should be spcified in seconds. For example 3600 is an hour.'),
    '#default_value' => variable_get('takealook_window_' . $type, 3600),
  );

}

I added a fieldset, textbox and two textareas. Now, we want to store these values in the variables table for each content type.

Step 2: Finished!

Yep, that’s all there is to it! We don’t need to do anything else. Drupal just stored those values in the variables table;

How are they stored?

Drupal will store each input in the variables table automatically. It uses the following pattern: inputName_contentType.

For example, the values stored in the formfield $form['takealook']['takealook_enabled'] will be stored in the variables table as “takealook_enabled_article” for the article content type (so after I edited the articles content type form).

Benefits

Less coding

Off course, this is nice because it saves you time coding to create a submit function to store the data yourself.

Variable name always in sync with the content type name

The really nice feature is that the variable name will automatically get updated when an admin changes the machine readable name of the content type. (In Drupal 7 we can alter the machine readable name of content types).

So when an admin changes the machine readable name of the Article content type from article to articles the variable gets automatically updated from “'takealook_enabled_article” to “takealook_enabled_articles”.