Hacking contextual links in Drupal 7
Drupal 7 comes with some cool new tools like contextual links
Contextual links are added to blocks using hook_block_view or hook_block_view_alter:
$block = array( 'subject' => t('Blog Archive'), 'content' => array( 'show_all_link' => l(t('All blog posts'), 'blog'), 'archive' => $archive, '#theme' => 'wordpress_blog_archive', '#cache' => DRUPAL_CACHE_PER_PAGE, '#contextual_links' => array( 'wordpress_blog' => array('node/add', array()), ), ), );
'#contextual_links' => array(
'wordpress_blog' => ... // Links from the wordpress_blog module
'node' => ... // Links from the node module
'menu' => ... // Links from the menu module
),-
node/23
'#contextual_links' => array( 'xxx' => array('node', array(23)), )
-
admin/content
'#contextual_links' => array( 'xxx' => array('admin/content', array()), )
- Paths which are implemented in hook_menu using %param - e.g. node/%node - should pass 'node' as the first array value, and the specific node id in the second.
- node/%node/view (default local task) with no 'context' defined
- node/%node/edit with context: MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE
- node/%node/revisions with context: MENU_CONTEXT_PAGE
- node/%node/report-as-spam with context: MENU_CONTEXT_INLINE
Adding links which aren't child menu items
The Wordpress Blog module is a prime example: it provides a 'Blog Archive' block, and adding a contextual link to 'Create Wordpress Blog Post' is a great usability enhancement. The problem is that node/add/wordpress_blog is a normal menu item - it's not a child task of anything. So we need to hack the menu to make this available to contextual links:function wordpress_blog_menu_alter(&$menu) { $menu['node/add/wordpress-blog']['_tab'] = TRUE; $menu['node/add/wordpress-blog']['tab_parent'] = 'node/add'; $menu['node/add/wordpress-blog']['context'] = MENU_CONTEXT_INLINE; }


Comments
Alex Ollivier (not verified)
Tue, 07/19/2011 - 17:17
Permalink
Hey, I just hopped over to your web-site via StumbleUpon. Not somthing I would generally read, but I liked your thoughts none the less. Thanks for creating some thing worth reading.
baptorgo (not verified)
Mon, 08/22/2011 - 20:08
Permalink
I just added your website on my blogroll. I will be sure to bookmark your blog and definitely will come back sometime soon. Great article with very interesting information.
sexy sms hindi (not verified)
Mon, 09/19/2011 - 09:15
Permalink
very good and cool,thank you for your sharing.
cruzeazy (not verified)
Sun, 11/06/2011 - 07:08
Permalink
There's a module that helps with this
http://drupal.org/project/ccl
Add new comment