0

imagine that we have a list if items in $items array that it's theme function is list_item in specific page.

code is

theme('item_list', array('items' => $items));

but in my module, I want to alter the parameter 'item_list' and I want to use my custom render function 'custom_item_list'

I know that I must register and define custom_item_list function but I don't know in what hook I am able to alter the theme_function parameter of specific element.

0

You can use hook_theme() function to use custom item list.

Hook theme:

function mymodule_theme($existing, $type, $theme, $path)
{
  if ($type == 'module')
  {
    return array(
      'custom_item_list' => array(
        'variables' => array('var_name'=>NULL),
        'template' => 'custom-item-list',
      ),
    );
  }
  return array(); //will raise fatal error if void
}

Call it as usual:

theme('custom_item_list', array('var_name' => mymodule_variable()));
  • I agree with your idea. but as I wrote in my question, I have defined my custom theme function but I don't know in which hook I can change the theme function of specific element. for example a block that is created by another module makes list items that is rendered by item_list, now I want to alter this list and render it by my custom theme function. – Mehrdad201 Apr 29 '16 at 11:02
0

You need to implement hook_theme_registry_alter() which is the hook modules can use to alter a theme function implemented by other modules. hook_theme() is for theme functions your module defines itself.

Just set $theme_registry['item_list']['function'] to 'theme_custom_item_list', and theme_custom_item_list() will be called instead of theme_item_list().
Keep in mind that, if you need to alter the content of the $variables parameter passed to a theme function, and not its output, you can implement hook_preprocess_item_list(), since every theme function has its own preprocess functions.

function mymodule_preprocess_item_list(&$variables) {
  // Change the title for the list.
  $variables['title'] = 'My own list title';
}
  • but if I alter $theme_registry['item_list'] function , all element that are themed by item_list will be affected. I want to define a new custom theme and set the theme function of specific item, not alter the body of item_list theme function. – Mehrdad201 Apr 29 '16 at 12:47
  • If the theme function is only used by your module, then just implement hook_theme() to create a new theme function your module will call with (for example) theme('custom_item_list', $variables). if you want to use a new theme function that is used from every module on specific pages, that is a little more harder to implement. – kiamlaluno Apr 29 '16 at 14:34
  • i want to know, If I want to set a new theme_function wrapper for specific element, in which hook I am able to do that before the element rendering. – Mehrdad201 Apr 29 '16 at 15:42

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.