I'm the developer and maintainer of a module on the Drupal website. It implements an API so that a certain third party service can be easily integrated in Drupal.

I'm currently having the following problem: I need to add a class-element to the logout link. So my module has to arrange that all the these links <a href="/user/logout">Log out</a> becomes the following: <a href="/user/logout" class="mymodule">Log out</a>

For the logout link in a menu it's easy, I have to implement hook_menu_link_alter().

However, I also want to catch all occurences where somebody is using l("Log out", "/user/logout"). The latter function calls theme_link, so if I would implement that, I think it could solve my problem.

So my questions are:

  1. Can I implement theme_link in a module? If so, can somebody tell me how to do this?
  2. Is there another way in which the above could be accomplished by means of a module?

2 Answers 2


What I understand is that you want to have a class in every link that comes on the site. I believe, you can achieve this by skinr module - https://drupal.org/project/skinr, which not only adds up classes but also provides you UI to achieve the same.

Also, in module, you can try this.

 * Implements hook_preprocess_HOOK
function MODULENAME_preprocess_menu_link (&$variables) {
  if ($variables['element']['#original_link']['menu_name'] == 'main-menu') {
    $variables['element']['#title'] = '<i class="icon-' . _sanitize_words($variables['element']['#title']) . '"></i>' . $variables['element']['#title'];

The above could should work, Worked for me atleast :)

  • Thanks for the hint on hook_preprocess_hook! However, your implementation will only change links in menus, I wanted to change ALL links. So it turns out that I needed hook_preprocess_link !!
    – LVA
    Nov 14, 2013 at 15:23

OSSGuys pointed me in the good direction. I needed to implement hook_preprocess_hook, in my case I needed hook_preprocess_link:

The code that finally solved my problem is:

function mymodule_preprocess_link(&$variables) {
  if ($variables['path'] == 'user/logout') {
    $variables['options']['attributes']['class'][] = 'mymodule';
  • Important note: this doesn't work on Drupal 6! I guess because theme('link', ...) is not called from the l() function.
    – LVA
    Nov 17, 2013 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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