Block is not showing the contextual links

2 minutes

You may have encountered a situation where the contextual links for a block are not rendered. But why does this occur, and why are contextual links so important? Let's dive in and explore this topic.

What are Contextual Links?

Contextual links are a powerful feature in Drupal that provide quick access to edit and configure options for various elements on your website. These links appear as small pencil icons or gear icons when you hover over certain areas of your site while logged in as an administrator or user with appropriate permissions.

Drupal Contextual Links Pencil Icon at the top right corner of a block

For blocks, contextual links typically offer options such as:

  • Edit the block content.
  • Configure block settings.
  • Delete the block.

Why are Contextual Links Important?

  • They allow site administrators and content editors to quickly access editing options without navigating through multiple menus.
  • These links are specific to the element you're interacting with, providing relevant options based on your current context.
  • They make site management more intuitive, especially for non-technical users who might struggle with Drupal's administrative interface.

The Role of "title_suffix" in Displaying Contextual Links

Now, let's address the main issue: why does the absence of the "title_suffix" variable cause contextual links to disappear?

In Drupal, the "title_suffix" variable is a crucial part of the block template and it is coming from the contextual module in the Drupal core, here is a link to the contextual_preprocess.

Here's a simplified example of how a block template might look:

<div{{ attributes.addClass(classes) }}>
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes.addClass('block__title') }}>{{ label }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    <div{{ content_attributes.addClass('block__content') }}>
      {{ content }}
    </div>
  {% endblock %}
</div>

So now you know that if you are going to override a block template, you should never remove the title_suffix.

Jidrone Drupal Developer
J. Ivan Duarte
Drupal Senior Developer

Share