0

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?
1

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 :)

1
  • 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 '13 at 15:23
0

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';
  }
}
1
  • Important note: this doesn't work on Drupal 6! I guess because theme('link', ...) is not called from the l() function. – LVA Nov 17 '13 at 13:15

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.