I am searching for a solution to display contextual links instead of tabs for a node (like View and Edit tab). While it would be easy to just hide the tabs in CSS, so it's not a problem, I can't find out how to display contextual links for the node.

I installed ccl but it works only for nodes contained in blocks or in views and not for regular nodes accessible by standard url mysite.com/node/5, which have tabs displayed. I configured in ccl "Link type: Node" and "Show link for: All nodes" but it doesn't display contextual links for regular nodes.

This is checked for theme Seven.

Comments

bkoether’s picture

Status: Active » Postponed

Hi raincloud,

The short answer to your question is "no". CCL is build on top of the core contextual links module and you will only be able to add your own links in places where the core module is providing contextual links.

But I like the idea that you have and will revisit it after I finished the tasks that are on the current roadmap. This could be a very nice feature.

rvolk’s picture

Hi raincloud,
i had the same problem and spend some time with searching for a nice and clean solution for my current project. I guess there will be other people with similar requests, so i share my solution here.

Problem:

  • Contextual links aren't disabled in node 'full' view mode by default. (see http://api.drupal.org/api/drupal/modules--node--node.module/function/nod... )
  • Even if the contextual array item is given, the node.tpl.php wouldn't display it.
  • Not every tab (MENU_LOCAL_TASK) will be available as MENU_CONTEXT_INLINE / MENU_CONTEXT_PAGE.
  • The contextual menu is displayed as $title_suffix in node.tpl.php, but not in 'full' view mode.

Solution:

  1. You have to "re-enable" the contextual_links item in the $build array. (see code example)
  2. The MENU_LOCAL_TASK link tabs should be copied to the contextual menu.
  3. The $title_suffix must be rendered, even if the node.tpl.php didn't show the title.

Sources:
YOURTHEME/template.php

/**
 * Implementation of hook_node_view_alter().
 */
function YOURTHEME_node_view_alter(&$build) {
  // allow contextual links on node pages
  $build['#contextual_links']['node'] = array('node', array($build['#node']->nid));
}

/*
 * Implementation of hook_menu_contextual_links_alter().
 * Adding items from the local tasks tabs menu to the contextual links on node full view.
 */
function YOURTHEME_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  if ($root_path == 'node/%') {
    $ignore = array('node/%/view','node/%/display');
    $paths = array();
    foreach ($links as $link) {
      $paths[$link['path']] = true;
    }   

    $local_tasks = menu_local_tasks();
    foreach($local_tasks['tabs']['output'] as $k=>$item) {
      $link = $item['#link'];
      if (!in_array($link['path'], $ignore) && !array_key_exists($link['path'],$paths)){
        array_push($links, $link);
        $paths[$link['path']] = true;
      }   
    }   
  }
}

/**
 * Implementation of hook_preprocess_hook().
 */
function YOURTHEME_preprocess_page(&$variables) {
  $variables['tabs'] = array();
}

YOURTHEME/templates/node.tpl.php

  if ($page && isset($title_suffix['contextual_links'])) print render($title_suffix['contextual_links']);

Your have to replace "YOURTHEME" with your theme name.

The $tabs in page.tpl.php should be hidden in hook_preprocess_page() or your template configuration. (e.g. as option in Omega Template).

Kind regards,
rvolk

bkoether’s picture

Status: Postponed » Closed (works as designed)

Thanks rvolk for sharing your solution.

I decided not to implement a solution for this particular request at this time. CCL is a support module for the core functionality of contextual links and the idea was just to add links where the widget shows up regardless. So I will mark this as works as designed.
raincloud, I hope the above shown solution can help you with your current project.

tevans’s picture

Nice recipe. Just wanted to say it works perfectly in case anyone else comes across this thread looking for a similar solution. Thanks rvolk!

krash’s picture

Thanks rvolk!

itaine’s picture

Thank you rvolk for the solution!

I'm getting the following bark after clearing the theme registry:

Notice: Undefined index: path in YOURTHEME_menu_contextual_links_alter()

the issue appears to be with this part of the code:

    foreach ($links as $link) {
      $paths[$link['path']] = true;
    }   

Anyone else experienced this?

NB: It works a treat without it :-D, I don't notice any lost in functionality without it. Which raises the question what is it doing?

Chipie’s picture

Thank you for this great solution.

Is this a different approach to this one:

http://drupal.org/sandbox/onkeltem/1730244

brightbold’s picture

Awesome thanks. The solution in #2 works for me, and I did not have the problem mentioned in #6.