How to override theme function in drupal?

I searched for this, but found only results related to .tpl files. I want to override the theme function (e.g. theme_user_list) from my module. How can i do this?

+6
source share
4 answers

You will need to modify the registry with hook_theme_registry_alter () and override the function used. As soon as you redefine the function index for this function and, importantly, clear the cache, your module replacement function - mymodule_foo () - should take effect.

See http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_theme_registry_alter/7 for details.

+10
source

According to the documentation at http://api.drupal.org/api/drupal/modules--system--theme.api.php/group/themeable/7 :

threads using the engine will have their well-named theme features automatically registered for them. Although this may vary depending on the theme engine, the standard set by phptemplate is a theme function called THEMENAME_HOOK. For example, for the default Drupal theme (Bartik) to implement the "table", the hook, phptemplate.engine will find bartik_table ().

So, in your case, you can override by creating the function MYTHEMENAME_user_list .

+1
source

You can override the theme function for a single output using sentences. Do this when you call the theme function, add two underscores and a name, then copy the original theme function into your .module file and rename it MYTHEME_original_function_name__ANAME, for example.

 <?php ... $vars['test'] = array( '#theme' => 'image_formatter__SOMENAME', '#item' => $item, '#image_style' => 'large', ); ... ?> <?php function MYTHEME_image_formatter__SOMENAME($variables) { /* my custom code */ } ?> 

After the caches have been cleared, you should be able to see this in the registry so that you can then use the theme_registry_alter theme to replace, using the name of your module instead of the theme name.

0
source

user491844 The answer is correct, although there is a simpler one: edit your theme template.php.

Just copy the entire function of the original theme into template.php, replace the "theme" with the name of your theme in the function declaration. For example, for example:

 function theme_lt_unified_login_page($variables) {[...]} 

Become this:

 function MYTHEMENAME_lt_unified_login_page($variables) {[...]} 

then an empty registration topic (or flushing all caches).

The only drawback is that all your settings will be accumulated in one file (template.php), which makes it not too neat for large projects.

0
source

Source: https://habr.com/ru/post/891862/


All Articles