Contextual links instead of tabs in Drupal 7

1 minutters læsning

Drupal 7 utilizes contextual links many places. But it still uses tabs on nodes. I wanted my theme to change those tabs to contextual links, so the layout was not altered for the administrative user. I had no clue how to do that, so I asked in the forum, and some helpful people answered. It turned out I needed to find the correct hooks.

<span style="display: none; "> </span>hook_menu_local_tasks() and hook_menu_local_task()<span style="display: none; "> </span>So I added this code to template.php in my theme to make the tabs contextual instead:

<pre type="php">/**
 * Implements hook_menu_local_task() 
 *
 * @param array $variables
 *
 * return string with html
 */
function mytheme_menu_local_task($variables) {
  $link = $variables['element']['#link'];
  // remove the view link when viewing the node
  if ($link['path'] == 'node/%/view') return false;
  $link['localized_options']['html'] = TRUE;
  return '<li>'.l($link['title'], $link['href'], $link['localized_options']).'</li>'."\n"; 
}

/**
 * Implements hook_menu_local_task() 
 *
 * @param array $variables
 *
 * return string with html
 */
function mytheme_menu_local_tasks(&$variables) {
  $output = '';
  $has_access = user_access('access contextual links');
  if (!empty($variables['primary'])) {
    $variables['primary']['#prefix'] = '<h2 class="element-invisible">' . t('Primary tabs') . '</h2>';
   
    // Only display contextual links if the user has the correct permissions enabled.
    // Otherwise, the default primary tabs will be used.   
    $variables['primary']['#prefix'] = ($has_access) ?
      '<div class="contextual-links-wrapper"><ul class="contextual-links">' : '<ul class="tabs primary">';

    $variables['primary']['#suffix'] = ($has_access) ?
      '</ul></div>' : '</ul>';
    $output .= drupal_render($variables['primary']);
  }
  if (!empty($variables['secondary'])) {
    $variables['secondary']['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2>';
    $variables['secondary']['#prefix'] = '<ul class="tabs secondary clearfix">';
    $variables['secondary']['#suffix'] = '</ul>';
    $output .= drupal_render($variables['secondary']);
  }
  return $output;
}

I used the code for this theme, that I am using for vih.dk. I am still struggling a bit with the css for the contextual links (especially in IE, but who is not).

Please feel free to use the comments for suggestions.

Kommentarer

Gábor Hojtsy

Hi, I think you can also just use hook_menu_alter() and change the context values of the tabs to not show up as tabs but only as contextual links :) Much less code, let the system take care of the presentation for you. Maybe I’m misunderstanding your goals here? Gábor

Vik

Hmm…am I missing something or you haven’t added “build” for contextual links in your code above? Looks like your code is incomplete!

Richard Morgan

To make the above code work you also need to add the class ‘contextual-links-region’ to the #content div in page.tpl.php

Skriv en kommentar

Din e-mail bliver ikke offentliggjort. Obligatoriske felter er markeret *

Indlæser...