Block Content entities have no Contextual links when rendered outside of Block config entity

Created on 11 February 2016, over 9 years ago
Updated 26 February 2025, 3 months ago

Problem/Motivation

Currently, if you render a Block Content entity outside of a related Block Entity/Plugin, no Contextual links are rendered.

You can see this in core by creating a View of Custom Blocks (rendered as "Default"), which should have contextual links but do not.

If you have Quickedit enabled as well as Contextual you'll also see an error in the console, as "data-quickedit-field-id" attributes are rendered but there is no matching data-quickedit-entity-id.

This all seems related to two quirks in the BlockViewBuilder and the BlockContentViewBuilder:

In \Drupal\block\BlockViewBuilder::preRender(), this block of code bubbles attributes and contextual links up from the Block Plugin's content into the main render array:

...
foreach (array('#attributes', '#contextual_links') as $property) {
        if (isset($content[$property])) {
          $build[$property] += $content[$property];
          unset($content[$property]);
        }
      }
      $build['content'] = $content;
...

You can already see related ramifications of this in issues like #2658270: BlockViewBuilder::preRender() must not mess with block render arrays β†’ .

In \Drupal\block_content\BlockContentViewBuilder::viewMultiple(), there is also a bit of code that assumes one rendering context:

    // Apply the buildMultiple() #pre_render callback immediately, to make
    // bubbling of attributes and contextual links to the actual block work.
    // @see \Drupal\block\BlockViewBuilder::buildBlock()
    unset($build_list['#pre_render'][0]);
    return $this->buildMultiple($build_list);

So to summarize - Block Content entities should support rendering anywhere, with or without the normal Block layout context.

Steps to reproduce

1. Install the latest Drupal 9.x using the standard profile.
2. Clone following repository into modules/custom directory of your project: https://github.com/drupalranger/block_content_contextual
3. Install this module using drush or UI
4. Navigate to /admin/structure/block/block-content and add some custom blocks
5. Navigate to /examples/block_content_contextual and check the output, the page will show 2 lists of custom blocks
5.1 First section will demonstrate default core behavior - no contextual links available
5.2 Second section will demonstrate a fixed state
Example screenshot is attached

πŸ› Bug report
Status

Closed: won't fix

Version

11.0 πŸ”₯

Component

block_content.module

Created by

πŸ‡ΊπŸ‡ΈUnited States samuel.mortenson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    Given the lack of activity on this issue and all the reasons described previously by @wim leers around block_content being tied to block config I think this is a won't fix.

    I think it would be quite hard to achieve something that wouldn't cause issues for existing sites, and as demonstrated in the demo module it's quite easy to set up your own template to render this out if needed.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Block Content entities have no Contextual links when rendered outside of Block config entity

    around block_content being tied to block config I think this is a won't fix.

    I don't think a dependency on Block Config is currently true, and may not ever have been.

    Yes, Block Content are expected to be rendered inside the block theme hook/template. But not block config. For example Layout Builder renders reusable Block Content with contextual links on the view entity route just fine.

    Perhaps ✨ Ability to display block_content entities independently, also outside of Blocks Postponed: needs info will help with putting all Block Content within a block theme hook/template universally, without needing manual wrappers as the #theme=block in \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray::onBuildRender.

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    For example Layout Builder renders reusable Block Content with contextual links on the view entity route just fine.

    Layout builder does this via a block plugin, no?

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Block plugins, along with manager, annotation, attribute, etc, are provided by core.

Production build 0.71.5 2024