Drupal contextual links: Power to the admin

Drupal contextual links magic

An under-utilized gem of Drupal 7

With D7, there was some great innovation with the contextual links system, where you can apply contextual links which sists in the top right corner of the node. It is indeed a positive thing for admins that really don't know the inner workings of Drupal, where they can perform action as they see the posts in views displays around the site. But this is indeed one of the lesser exploited gems of D7. By default, a node have only the basic edit & delete links, but what if we can expand this to include even more useful content commands? In Drupal, of course we can.
Say you have a regular node which you want to create custom commands for. Having a view of nodes, you want to link to a geoproximity map with coordinates, and you want to be able to send the author a cake. And you want to link to a kitten. Well, you can easily do that with no code.
Enter Custom contextual links. What you can do is easily configure contextual links, with tokens, allowing you to link to existing content with links & queries based on the node's content. And not just nodes, but views, blocks local actions as well.
So in this case we have a node, and want to call these links:

  • node/[node:id]/show-kitten - Existing subtab of the node.
  • user/[node:uid]/send-cake - Action under the user object
  • openlayersmap/[node:field_geofield:lat]/[node:field_geofield:lat]?zoom=5&lat=[node:field_geofield:lat]&lon=[node:field_geofield:lon] - A more complex one, which will fire a geoproximity search view based on fields for the node, and then configure a openlayers map.

You can configure them to be displayed per-nodetype, for all nodetypes, or just for one single node.

Screenshot contextual link


For an anonymous user, these links would look like this:

Screenshot contextual link anon


Of course this is just an example, and Im sure you can think of lots of useful commands. As mentioned before, it also works for views, blocks and other stuff, just remember to enable the submodules for the parts that you want.

Best of all, the links are exportable, so you can add them into features. Easy peasy. :)