You are here

Blog

Drupal 7 convert node edit tabs to contextual links

Add the following code to your template.php in your theme: (without the php tags, and replace YOURTHEME with your theme name)

<?php
function YOURTHEME_menu_local_task($variables) {
 
$link = $variables['element']['#link'];
 
$link['localized_options']['html'] = TRUE;
  return
'<li>'.l($link['title'], $link['href'], $link['localized_options']).'</li>'."\n";
}
function
YOURTHEME_menu_local_tasks($variables) {
 
// Add contextual links js and css library
 
drupal_add_library('contextual', 'contextual-links');
 
$output = '';
  if (!empty(
$variables['primary'])) {
   
$variables['primary']['#prefix'] = '<div class="contextual-links-wrapper"><ul class="contextual-links">';
   
$variables['primary']['#suffix'] = '</ul></div>';
   
$output .= drupal_render($variables['primary']);
  }
  if (!empty(
$variables['secondary'])) {
   
$variables['secondary']['#prefix'] = '<ul class="tabs secondary clearfix">';
   
$variables['secondary']['#suffix'] = '</ul>';
   
$output .= drupal_render($variables['secondary']);
  }
  return
$output;
}
?>

Then add a div with the class contextual-links-region around your main content /node area <div class="contextual-links-region">

We've also noticed that there must be at least one block on the node pages to make sure contextual links are loaded. UPDATE: The drupal_add_library line should fix this, but it may need to go in the menu_local_task hook for some themes (or for some reason) as shown in the comment below.

Drupal
PHP
Contextual Links

Need A Developer? Contact Us