Published access check for block_content fails for translations which have separate values

Created on 9 October 2020, over 4 years ago
Updated 26 February 2025, 9 days ago

Problem/Motivation

Since Drupal 8.7 you can publish/unpublish block_content via the UI. I have enabled translation for my block_content.

Now I came into the situation, that I wanted to have different "Published" states for the english and second language. So I made the "Published" property translatable which seems to work fine UI backend-wise. But in the frontend the access check for the block_content entity fails. It checks for the published state of the source language, but does not respect if the default language is set to "Unpublished" and the second language is set to "Published".

The reason is the access check in core/modules/block_content/src/Plugin/Block/BlockContentBlock.php It checks the access for the entity which is retrieved by $this->getEntity(). In this method you can see that it's loading the block_content by id, which will not be language aware and explains, why the access check fails.

protected function getEntity() {
    if (!isset($this->blockContent)) {
      $uuid = $this->getDerivativeId();
      if ($id = $this->uuidLookup->get($uuid)) {
        $this->blockContent = $this->entityTypeManager->getStorage('block_content')->load($id);
      }
    }
    return $this->blockContent;
  }

I fixed the problem by using

\Drupal::service('entity.repository')->getTranslationFromContext($this->blockContent);

to load the correct translated entity and therefore check for the correct translated published state.

Steps to reproduce

1. Install Drupal with two languages
2. Create a translatable block_content type
3. Configure the "Published" property to be translatable
4. Create a block content with your default language and set it to unpublished
5. Translate this block content into your secondary language and set it to published
6. Place the block somewhere with block UI.

You will see, that the the block is not visible in both language frontends, although your secondary language translation have been set to "Published".

By adding getTranslationFromContext as described above, the access check will respect the languages and show the expected results.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

block_content.module

Created by

🇩🇪Germany szeidler Berlin

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024