Skip to content

Instantly share code, notes, and snippets.

@lsolesen
Created June 24, 2011 18:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsolesen/1045424 to your computer and use it in GitHub Desktop.
Save lsolesen/1045424 to your computer and use it in GitHub Desktop.
Change tabs to contextual links in Drupal 7
/**
* 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;
}
@micahredding
Copy link

This apparently only works when wrapping the tabs and title section with this:

in page.tpl

@micahredding
Copy link

Sorry, I meant:

<div class="contextual-links-region">

in page.tpl

@GibyHost
Copy link

This solution does not work if views is active. Can you fix it or do i something wrong? I placed the code in the template.php file an add a

in my page--content.tpl.php. Then i delete the "tabs"-code from page--content.tpl.php. All seems work fine, but when i activate views its crached. I dont understand that, because the block is placed on frontpage only with views.

Please help!

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment