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

Use getTranslationFromContext in getEntity to ensure blocks are always translation aware.

Remaining tasks

Review https://git.drupalcode.org/project/drupal/-/merge_requests/11297

User interface changes

None

API changes

None

Data model changes

None

Issue fork drupal-3175985

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

szeidler created an issue. See original summary.

larowlan’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Does the node module do the same thing?

szeidler’s picture

Status: Postponed (maintainer needs more info) » Active

The node module does the same thing via entity classes. E.g. EntityViewBuilder https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21... . But also for node-specific controllers like revisionShow() in https://api.drupal.org/api/drupal/core%21modules%21node%21src%21Controll...

When grepping the Drupal code base for $this->entityRepository->getTranslationFromContext there are a couple of examples.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Issue tags: +Needs tests

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley made their first commit to this issue’s fork.

acbramley’s picture

Status: Active » Needs work

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 :)

acbramley’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

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!

catch’s picture

Status: Reviewed & tested by the community » Needs work

One comment on the MR about constructor bc.

acbramley’s picture

Status: Needs work » Needs review

Done

smustgrave’s picture

Status: Needs review » Needs work

1 small comment on the MR. Feedback appears to be addressed so probably good to self RTBC @acbramely

acbramley’s picture

Status: Needs work » Reviewed & tested by the community

Thanks!

  • catch committed 053a7872 on 11.x
    Issue #3175985 by szeidler, larowlan, acbramley, smustgrave, catch:...

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • catch committed 574ac019 on 11.3.x
    Issue #3175985 by szeidler, larowlan, acbramley, smustgrave, catch:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.