Entity view (Content)

Using Contexture links in Drupal 6 & 7 Part 2

By kwu
Sep. 14, 2012

To continue my last post, I will demonstrate the implementation of contextual links in Drupal 7. It comes with Drupal core and is very simple. I will show 2 ways of adding contextual links.

1 Add contextual links to block

1.1 Customizing the #contextual_link property in hook_block_view or hook_block_view_alter

The #contextual_links property is an array where each key is the name of the module implementing the contextual links and each value is an array of 2 values combined to form a path which Drupal will fetch contextual links from.


What happened is that all the links with a type MENU_LOCAL_TASK (as defined in their respective hook_menu) and a context MENU_CONTEXT_INLINE underlying the path node/%node are fetched. Please note that only the local tasks with a context inline will work for contextual links. There are 2 contextual links that have been declared under node/%node in Drupal 7 by default.

 'Edit',
    'page callback' => 'node_page_edit',
    'page arguments' => array(1),
    'access callback' => 'node_access',
    'access arguments' => array('update', 1),
    'weight' => 0,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'node.pages.inc',
  );

  $items['node/%node/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('node_delete_confirm', 1),
    'access callback' => 'node_access',
    'access arguments' => array('delete', 1),
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'file' => 'node.pages.inc',
  );
?>

1.2 Implements hook_menu to create your own contextual links.

If we want to create our own contextual links, we need to define our own items as local tasks with an inline context. Here is the code to implement a “Add To Cart” contextual link at node/%node/add-to-cart.

 'Add to cart',
    'type' => MENU_LOCAL_ACTION,
    'context' => MENU_CONTEXT_INLINE,
    'page callback' => 'contextual_example_add_to_cart',
    'page arguments' => array(1),
    'access callback' => TRUE,
  );
?>

What it looked like for the final outcome:

2 Add contextual links to a views row

2.1 Create function to render contextual links.

 $output,
        '#theme_wrappers' => array('contextual_container'),
        '#contextual_links' => array(
          'contextual_example' => array('node', array($nid)),
        ),
  );
  return render($render_array);
}

2.2 Implement the views_view_field preprocess hook and we wrapped the contextual links around.

field_info['field_name']) && $vars['field']->field_info['field_name'] == 'field_slide_image') {
    $vars['output'] = contextual_example_make_contextual_links($vars['output'], $vars['row']->nid);
  }
}
?>

As we can see from above, the implementation is really flexible. If you have better ideas how to customize it, please post them in comments. Happy coding!

Post Tags: