Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

There's a hook for that

Parent Feed: 

In keeping with our attention to detail, we wished to streamline the administrative experience on the Coyote Point home page. Using the nodequeue module allowed us to provide a simple drag and drop interface for selecting and ordering slideshow items and call to action panes. However, the workflow felt clunky, as with so many things created with the powerful but sometimes disjointed "There's a module for that" approach. Rather than providing instructions with 4 or 5 steps, I thought "why not simply provide a link accessible directly from the home page"? Although the Drupal contextual links system isn't smart enough to guess what links we may want, happily it's extensible enough to allow us to place the links we need.

As is usually the case with Drupal, there's a hook that allows us to modify the behavior we need to without hacking core code and decreasing the maintainability of the system. In this case, our entry point is hook_contextual_links_view_alter(). The basic idea is to find the existing link for the area we want to add links to, and add our links before they are rendered. Here's an example for the slideshow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * Implements hook_contextual_links_view_alter().
 */
function cp_homepage_contextual_links_view_alter(&$element, &$items) {
  if (isset($element['#links']) && isset($element['#links']['views-ui-edit'])) {
    if ($element['#links']['views-ui-edit']['href'] == 'admin/structure/views/view/homepage_slideshow/edit') {
      // Get queue id.
      $q = nodequeue_load_queue_by_name('home_page_marquee');
      if ($element['#links']['views-ui-edit']['query']['destination'] == 'home') {
        // Add contextual link for re-ordering items if we're on the homepage.
        $element['#links']['cp_homepage_0'] = array(
          'title' => 'Re-order items',
          'href' => 'admin/structure/nodequeue/' . $q->qid . '/view',
        );
      }
      // Add contextual link for adding a new item.
      $element['#links']['cp_homepage_1'] = array(
        'title' => 'Add new marquee item',
        'href' => 'node/add/marquee',
      );
      // Remove the edit view link to reduce confusion.
      unset($element['#links']['views-ui-edit']);
    }
  }
}

With relatively few lines of code, we've improved the administrative experience quite a bit. You can see that the contextual link to edit the slideshow view is removed, since the site administrators should have no need to edit the view on a day-to-day basis, if ever. Here's what it looks like in action:

This approach gives a big usability win with little effort while utilizing core APIs and thus reducing the maintenance burden on our codebase. In addition, the hook used remains unmodified in Drupal 8, rendering the approach valid for the foreseeable future.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web