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, 4 months 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.

  • First commit to issue fork.
  • Pipeline finished with Failed
    4 months ago
    Total: 130s
    #434219
  • Pipeline finished with Success
    4 months ago
    Total: 427s
    #434221
  • πŸ‡¦πŸ‡ΊAustralia acbramley

    Manually played around with this a bit and confirmed it's still an issue. Adding the getTranslationFromContext wrapper on getEntity does work and doesn't look to break anything! We just need tests now :)

  • Pipeline finished with Success
    2 months ago
    Total: 652s
    #483473
  • Status changed to Needs review 12 days ago
  • πŸ‡¦πŸ‡ΊAustralia acbramley
  • Pipeline finished with Success
    12 days ago
    Total: 1114s
    #531930
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Test-only job ran here https://git.drupalcode.org/issue/drupal-3175985/-/jobs/5698563 which outputted

    1) Drupal\Tests\block_content\Functional\BlockContentTranslationTest::testBlockContentTranslationAccess
    Behat\Mink\Exception\ResponseTextException: The text "Spanish block" was not found anywhere in the text of the current page.
    /builds/issue/drupal-3175985/vendor/behat/mink/src/WebAssert.php:907
    /builds/issue/drupal-3175985/vendor/behat/mink/src/WebAssert.php:293
    /builds/issue/drupal-3175985/core/tests/Drupal/Tests/WebAssert.php:981
    /builds/issue/drupal-3175985/core/modules/block_content/tests/src/Functional/BlockContentTranslationTest.php:67
    FAILURES!
    

    Looking at the change return $this->entityRepository->getTranslationFromContext($this->blockContent); seems like a good update!

Production build 0.71.5 2024