Drupal 6: What argument names *not* to use in hook_theme

When defining hook_theme the choice of argument names that are passed to the theme’s template is important. These argument names share a namespace with some default argument names that are added by template_preprocess. Using any of these names will conflict with the default ones and a potentially wrong argument will be accessed in the preprocessing function and template.
In this example the argument name ‘user’ is in potential conflict:

/**
 * hook_theme
 */
function my_module_theme() {
  return array(
    'some_theme' => array(
      'template' => 'some_template',
      'path' => drupal_get_path('module', 'my_module').DIRECTORY_SEPARATOR.'templates',
      'arguments' => array('user' => NULL)
    )
  );
}

To avoid this potential conflict the arguments added by the module can be prefixed with the module’s name so instead of:

      'arguments' => array('user' => NULL)

the argument can be named:

      'arguments' => array('my_module_user' => NULL)

Alternatively, when there are more than one argument, all the arguments can be encapsulated in one array argument that server as a namespace:

      'arguments' => array('my_module_args' => NULL)

where the theme arguments are added as items in an array:

     theme('some_theme', array('user' => $my_user));

and then accessed:

function template_preprocess_some_theme(&$variables) {
  $default_user_arg = $variables['user'];
  $my_user_arg = $variables['my_module_args']['user'];
This entry was posted in Drupal and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *