HTML in a menu option would be very useful from a module too, i.e. inserting an image as a menu option. Is there a way to do use something like function MODULE_preprocess_menu_link(&$variables) inside a module, and not in template.php?

| improve this question | | | | |
  • A partial solution I've found is to declare function theme_menu_item_link($link) { … }. – Gabriel R. Sep 27 '13 at 11:13

Modules can implement preprocess and process functions for theme functions using a template file; for theme functions not using a template file, Drupal 7 allows module to implement a preprocess function.
For example, the mymodule module could implement mymodule_preprocess_menu_link() which is a Drupal 7 preprocess function for theme_menu_link().

Preprocess and process functions are useful when the variables passed to the theme function need to be changed. If there is the need of changing the output of the theme function, then a module needs to implement hook_theme_registry_alter() and change the function called to serve the output of a theme function.

function mymodule_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['menu_link'])) {
    $theme_registry['menu_link']['function'] = 'theme_mymodule_menu_link';

With this implementation, theme_mymodule_menu_link() would be the function called when a module calls theme('menu_link', /* … */).


| improve this answer | | | | |
  • I haven't tried it yet, but sounds legit. – Gabriel R. Oct 8 '13 at 10:56

There is contrib module menu_html available for adding html to the menu.

If you are trying this in your custom module than you may get some reference from it.

| 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.