An easier way to add contextual links in Drupal 7

Contextual links are a great new feature of Drupal 7. Basically, links are added to various elements of the page to make it easy for site editers to make changes to whatever content they currently see.

By default, the contextual module smartly adds links that it finds in the normal Drupal menu system. But what if you want to add some links of your own? You have a few options: First, you could add contextual links using the normal menu system or similarly you could use a hook_menu_alter function as described in this blog post.

But there is an easier way...

The contextual module provides an alter hook that can be used to add links whereever you need them. Here is an example:

function MYMODULE_contextual_links_view_alter(&$element, &$items) {
  // This hook is called for EVERY set of contextual links
  // on a page.  We first want to check the $element to make
  // sure we are adding a link to the correct list of contextual
  // links. In this case we only want to add a link to blocks
  // created by the "search" module. We just add links using the
  // same array structure as is used by theme_links.
  if (isset($element['#element']['#block']) && $element['#element']['#block']->delta == "site_tweets-site_tweets_block") {
    $element['#links']['social-networks'] = array(
      'title' => 'Edit social network links',
      'href' => url('/admin/config/system/site-information', array('absolute' => TRUE)),
    );
  }
} 

And voila...

I had to add another element

Troy Dohner (not verified)
I had to add another element to the array to get the IF statement to work... I changed this... if (isset($element['#block']) && $element['#block']->module == "search"){ to this... if (isset($element['#element']['#block']) && $element['#element']['#block']->module == "search"){ This is great! thanks!

Thanks Troy ... good catch.

bleen
Thanks Troy ... good catch. I fixed the post above.

Very handy, thanks.

Isaac Freeman (not verified)
Very handy, thanks.

How do you add contextual

Joe (not verified)
How do you add contextual links to a node? I want edit in place links??? Thanks

@Joe, re: adding contextual

Psykoral (not verified)
@Joe, re: adding contextual links to a node: Because I went looking around for this FOREVER, hopefully it'll help someone still digging up this thread. I searched for 3 days, found all kinds of lengthy methods, custom modules that claimed to do this, yada yada, all to no avail (or more than I even wanted to try). Basically what I found was that the PHP that builds contextual links specifically looks for it NOT to be it's own page node, or in full view mode. Well jeebs, thanks, that's exactly where I wanted contextual links! How awesome of you to hard code it and decide not to give me them on pages I want to be able to edit without going into the Admin interface. Anyway, bitterness aside (can you tell I've been looking for this answer for a while?)... it's time to brute force remove that so we can enable it for all full page nodes in one shot. Edit /modules/node/node.module About line 1303 you should see this line: if (!empty($node->nid) && !($view_mode == 'full' && node_is_page($node))) Comment it out and make it this instead: if (!empty($node->nid)) Save, upload, and dance! Basically, what this does is make contextual links available to every block, view, or node, when previously the code was specifically blocking them from 'full view mode' and if the node is being viewed as an entire page. Then beyond that if you'd like to add more custom links to the contextual menus on specific pages you can use the Custom Contextual Links module at http://drupal.org/project/ccl . And of course, this doesn't "blindly" enable them for all users, you still have to manage what roles get to see contextual links through the normal Permissions. It just bluntly enables them on all types of content blocks/views/nodes for users that have permissions to view them. May not be the most 'official' way to do it via the Drupal UI or module method, but it does what I want it to do anyway. Hope it helps someone. Going to glue my hair back to my head now.

@bleen Thanks for the great

BetoAveiga (not verified)
@bleen Thanks for the great info! It's easier your way. @Psykoral Thanks! Your info is also valuable. I think I'll opt for the module.

Thanks for this!

Elijah Lynn (not verified)
Great write up, thanks for this! Correction: The isset() needs it's closing parentheses.

fixed

bleen
fixed

Great write up, thanks for

Anirban Pathak (not verified)
Great write up, thanks for this! Because I went looking around for this FOREVER, hopefully it'll help someone still digging up this thread. I searched for 3 days, found all kinds of lengthy methods, custom modules that claimed to do this.