Creating Custom Contextual Links in Drupal 7

Drupal Version
7

Contextual Links are options that appear when you click the cog/gear icon that is displayed while hovering over certain elements within your Drupal site. These options will be specific to the 'context' of the element that you are hovering over. For example, clicking the cog that appears when hovering over a block will give you an option to edit that block.

NOTE: To see the cog and action links you will need to have Drupal's code Context Links module enabled and you'll also need the permission "Use contextual links", as well permissions to perform the actions within the context.

Drupal contextual links example

Some modules expand Drupal's contextual links to add specific actions relevant to that module. For example, Views will add context links to your Views pages and Views blocks to provide quick access to edit the View that is responsible for that element's output. But if you want to make your site easy to administer and maintain you'll want add your own custom context links.

With the Custom Contextual Links module you can easily add your own custom context links to:

  • Nodes (custom content types and pages)
  • Views
  • Blocks
  • Actions / Local Actions

Functionality for Panels and Rules is planned to be implemented later.


INSTALLATION & SETUP

Now we'll walk through some basic examples of using the CCL module.

Install and enable the Custom Contextual Links module and set the appropriate permissions for the user(s) roles that will have access to use the context links by granting them the "Use contextual links" permission.

The main Custom Contextual Links module allows adding context links to Nodes and is the minimum required module for adding your custom links. The CCL module comes with additional sub-modules to allow adding your custom links to Actions, Blocks and Views. For this example we'll be adding custom context actions for Views so enable the "Custom Contextual Links - Views" module as well.

CREATING A CONTEXT LINK FOR A VIEW PAGE

NOTE: This example will use the Article content type that is installed by default with Drupal and will also use a View that displays a list of Article Nodes. This View will need to be created. If you do not have a View displaying Articles on your site you will need to create one to follow along. Or, you can use a different content type and View and make adjustments from the examples as necessary.

The Custom Contextual Links configuration is located in Configuration->User Interface->Custom Contextual Links | (URL: YOUR_DOMAIN/admin/config/user-interface/ccl)

Create new custom context link

  • Click "Add new link"
  • You'll see a configuration screen similar to the image below. Enter the following information:
    • Link Title: The title of the link that will be displayed when the cog is hovered over.
    • URL: This will be the URL of the action to perform when your link is clicked. To find the URL you can perform the action manually and take note of the URL in your browser's address bar.
    • Link Type: For this example set this to View.
    • Add This Link To: For this example set this to "A specific display of a view".
    • Views Display: Select the View that you have created to display your Article content type Nodes.

Custom Contextual Link View Settings

Once completed and saved you will now have an additional context link that says "Add new Article" that is displayed when you hover over the cog at the top of your Article View. This will now give you or your editors/admins quick and easy access to create a new Article directly from the Article listing page.

If your View has multiple content types then you can repeat this process to add multiple action links for each content type within the view.

NOTE: You can enter any valid Drupal URL you'd like for the function, including your own custom callbacks. You can also use tokens to make your links dynamic.

CREATING CONTEXT LINKS FOR A VIEWS NODE

For a View's page that is listing Nodes using the View style type of "Content" (either Teaser or Full) then each Node will automatically have Edit and Delete context links (provided you have permission to perform those actions). However, if your View's style type is set to "Fields" then no context links will be output by default. With the following method you can create custom context links for Nodes that are output in a View using the Fields display type.

Either create or edit an existing View that is outputting Nodes or other entities on a page.

Next, add the following new fields to your View:

  • Content: Delete link - Check the "Exclude from display" box and enter your display text in the "Text to Display"field. For example, "Delete this Article"
  • Content: Edit link - SAME AS ABOVE.
  • Global: Contextual Links - It is important that you do not check to exclude this field from the display. Select the Delete and Edit links you just added from the list of fields shown.

CREATING CONTEXT LINKS FOR A VIEWS NODE

Save your View and review the page on your site. You will now have custom context links for your View and the individual Nodes within that View.

Drupal Custom Context Links

CREATING UN/PUBLISH, UN/STICKY & UN/PROMOTE LINKS FOR NODES

Besides the Edit and Delete context actions that are added to Nodes within a View by default (if using the Views display output of Content) you can also easily add Publishing, Stickiness and Promotion links to your Nodes using the Custom Contextual Links module. To do this you'll need to have the Custom Contextual Links - Actions sub-module enabled. If it's not already, enable it now.

First, navigate back to the CCL configuration page and click the "Add new action link" link.

Custom Contextual Links Create Node Action Link

Now select the actions you'd like available in the context for your Node and well as the which Nodes you'd like these actions displayed on.

If you select "Content Type" you can select an individual content type to show your actions on. Or, you can select "All nodes" to show your actions on Nodes of every content type. Or select "Single node" to show your options on one individual Node only.

Edit Custom Context Links for Nodes

Save your new custom link and navigate to a View that is listing Nodes using the Content Display (not fields).

CCL Custom links on Drupal Nodes

Author Information

Written by: Shawn Ostermann

Shawn is a Drupal Specialists with over 12 years of experiencing building and developing Drupal websites including custom module development and e-commerce websites.