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:
/**
* 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. We'd love to partner with you on your next project!
Since we’re big on relationships, we’re all about finding the right fit. Will you take the next step with us to see if we’re a match?