3

How can hook_theme_registry_alter() associate, for example, theme_mymodule_image_output() with the 'image' theme ID?

5

As reported in the documentation, hook_theme_registry_alter() is invoked from Drupal to allow third-party modules to alter the theme registry information returned from hook_theme().

The value returned from the implementations of hook_theme() is an array containing information about the different theme functions implemented by the modules (or themes).

function node_theme() {
  return array(
    'node' => array(
      'arguments' => array('node' => NULL, 'teaser' => FALSE, 'page' => FALSE), 
      'template' => 'node',
    ), 
    'node_list' => array(
      'arguments' => array('items' => NULL, 'title' => NULL),
    ), 
    'node_search_admin' => array(
      'arguments' => array('form' => NULL),
    ), 
    'node_filter_form' => array(
      'arguments' => array('form' => NULL), 
      'file' => 'node.admin.inc',
    ), 
    'node_filters' => array(
      'arguments' => array('form' => NULL), 
      'file' => 'node.admin.inc',
    ), 
    'node_admin_nodes' => array(
      'arguments' => array('form' => NULL), 
      'file' => 'node.admin.inc',
    ), 
    'node_add_list' => array(
      'arguments' => array('content' => NULL), 
      'file' => 'node.pages.inc',
    ), 
    'node_form' => array(
      'arguments' => array('form' => NULL), 
      'file' => 'node.pages.inc',
    ), 
    'node_preview' => array(
      'arguments' => array('node' => NULL), 
      'file' => 'node.pages.inc',
    ), 
    'node_log_message' => array(
      'arguments' => array('log' => NULL),
    ), 
    'node_submitted' => array(
      'arguments' => array('node' => NULL),
    ),
  );
}

The returned array used what in my previous comment I called theme ID, and it is the same value that is passed to theme().
node_theme() doesn't use it, but the documentation for hook_theme() reports that the array can use the function index to report which function needs to be called as theme function.

A module implementing hook_theme_registry_alter() should use the following code to change the function that is invoked when theme('image') is called.

function mymodule_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['image'])) {
    $theme_registry['image']['function'] = 'theme_mymodule_image_output';
  }
}

When a module alters the theme registry using that code, any call to theme('image', ...) will not invoke theme_image() anymore, but it will invoke the function defined from that module.
To notice that it is not necessary to name the function theme_mymodule_image_output(); the code would work the same if the function is renamed mymodule_image_output() and the code of mymodule_theme_registry_alter() is accordingly changed. I usually use the prefix theme_ to make clear the function is a theme function.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.