I know this question already has some "kind" of answer in documentations but they are too abstract for me. Most documentation I have seen on Drupal doesn't explain all the details of how to properly override a theme function, or maybe I don't understand.

My problem is that I need to change the output of theme function theme('links__system_main_menu', ...), which is used in page.tpl.php to print the content of the main menu. I don't understand how to make it done. Please explain me every step you take and why did you take that step.

  • Welcome to Drupal Answers! What exactly didn't you understand? You just need to implement a hook. and a theme function.
    – apaderno
    Oct 23 '15 at 7:24
  • I just need to see an example.
    – Bora Semiz
    Oct 23 '15 at 7:28
  • It depends from where you need to override a theme function. There are many examples in the documentation; we cannot duplicate the documentation here. If you have a specific question about something you don't understand, that could be an acceptable question, but asking for examples is too generic. If you don't understand from how to start, you need to read documentation, tutorials, and other online resources. Once you know how to start writing code, you are ready to ask a question, if there is anything that doesn't work in your code.
    – apaderno
    Oct 23 '15 at 8:07

To override the output of a theme function in a theme, you simply take the name of your theme, and the name of the theme function, and join them together with an underscore.

So for your example, the function would be called:

function MYTHEME_links__system_main_menu($variables) {
  $html = '<ul>';

  // Loop over links

  $html .= '</ul>';

  return $html;

Your function should return the HTML string that you want to be rendered when this theme is used, any variables that have been passed to the theme() function will be available to you in $variables.

Usually it's enough to look at the original theme function to find out what's available to build your HTML, but sometimes you'll need to get the Devel module installed and inspect vars with dpm to properly see what's going on.

  • There should be also to explain how to do it from a module, which is the most of the cases, and explain that overriding a theme function is not necessary all the times, since every theme function has preprocess functions too.
    – apaderno
    Oct 23 '15 at 8:13

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