Problem/Motivation

Very similar to the original post. Layout builder add custom blocks were not displaying the block types in the translated language.

Original Post

In Layout Builder, when you go to add a block to a section, and click "Create custom block", a list of links of the type of custom block content you want to add as a inline block appears. On a multilingual site, this list of links displays only in one language, which is language the site is using when it caches the derivate plugin definitions.

This is because of line 52 of docroot/core/modules/layout_builder/src/Plugin/Derivative/InlineBlockDeriver.php:
$this->derivatives[$id]['admin_label'] = $type->label();
Which sets the admin_label of the derivate plugin definition to a string instead of TranslatableMarkup.

Issue fork drupal-3038717

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

godotislate created an issue. See original summary.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tim.plunkett’s picture

Title: Links to create custom block to add to Layout Builder section are in first language after cache rebuild. » Block derivatives labels are always shown in the first language they are view in after cache rebuild
Component: layout_builder.module » block.module
Priority: Normal » Major
Issue summary: View changes
Issue tags: +Blocks-Layouts, +Needs issue summary update

This is visible using Block UI as well, and not just with custom blocks.
Menus and Views are also affected.

As well as field blocks within Layout Builder.

Still tagging for Layout Builder, even though it is a generic bug.

godotislate’s picture

I don't whether this is the best way for a general solution, but this was my workaround:

  1. Create a class TranslatableEntityLabelMarkup
    1. Class implements \Drupal\Component\Render\MarkupInterface and \Countable
    2. Uses \Drupal\Component\Utility\ToStringTrait
    3. Contructor takes entity type ID and entity ID as arguments
    4. render() method loads $entity from storage using entity type ID and entity ID, and returns $entity->label()
  2. Implement module hook to alter plugin definitions
    1. Set admin_label of relevant definitions to new instance of TranslatableEntityLabelMarkup, passing in the entity type ID (e.g., 'block_content_type') and entity ID (e.g., block content bundle machine name)

Edited to add: This was only for inline blocks for custom block content types. It probably doesn't work for field blocks at least.

tim.plunkett’s picture

StatusFileSize
new2.52 KB

Let's start with a failing test.

Status: Needs review » Needs work

The last submitted patch, 6: 3038717-derivatives-5.patch, failed testing. View results

berdir’s picture

We removed language specific caching of plugin definitions a pretty long time ago, we'll have to revisit that I guess. One reason we did that is that it allowed us to remove quite a few cache tags and tag-based invalidations. I think an alternative option that I posted there was to invalidate all known language suffixes explicitly instead.

godotislate’s picture

Issue tags: +Needs tests
StatusFileSize
new22.04 KB

Patch implementing solution described in #4 for inline blocks, field blocks, menu blocks, view blocks, and block content blocks.
This includes test in #6, but needs more tests to cover everything else besides menu blocks.

godotislate’s picture

StatusFileSize
new22.26 KB

Updated patch with fixes for failing tests.

Note that the placed blocks listed at /admin/structure/block are only shown in the default language, because they are loaded without config overrides. This is separative from the derivatives issue.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ilya.no’s picture

Status: Needs work » Needs review

Thanks for the patch. I have same problem and patch from #10 solves the issue.

s3b0un3t’s picture

Same answer as ilya.no. Tested in 8.8.5.
Thanks a lot !

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

akalam’s picture

Tested and worked as expected on D8.9. Thanks a lot @tim.plunkett and @godotislate for providing the patch.
Can somebody test it on D9 so we can move to RTBC?

berdir’s picture

This is awfully complex. Was there any discussion to instead cache block plugins per language? We used to do that, then removed it again but had a number of bugs due to that over time.

As a similar example, see \Drupal\Core\Entity\EntityFieldManager::getBaseFieldDefinitions().

Note that if we do that, we need a cache tag to invalidate plugin definitions in all languages on a cache clear, that's the downside. in the entity system, we have that already anyway.

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.

unstatu’s picture

Status: Needs review » Reviewed & tested by the community

I confirm it works on 9.2.10. Setting to RTBC.

akalam’s picture

Status: Reviewed & tested by the community » Needs review

I'm setting this back to "Needs review" until the concerns expressed on #18 gets discussed and solved.

ranjith_kumar_k_u’s picture

StatusFileSize
new22.22 KB

Re-rolled # 10 for 9.4.

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.

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: -Needs issue summary update

Getting this error after applying patch

Deprecated function: Return type of Drupal\Core\Entity\TranslatableEntityLabelMarkup::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in include() (line 21 of core/lib/Drupal/Core/Entity/TranslatableEntityLabelMarkup.php).

Did need to clear cache after applying patch but I imagine that's to be expected.

Configured after clearing cache the block type links were showing as translated.

Also would be nice ot have a tests-only patch so we can confirm that worked

Did update the issue summary but didn't need it much.

ravi.shankar’s picture

StatusFileSize
new22.29 KB
new1.83 KB

Fixed Drupal CS issues of patch #22, still needs work for #24.

smustgrave’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests +Bug Smash Initiative
StatusFileSize
new420 bytes
new22.32 KB

Status: Needs review » Needs work

The last submitted patch, 26: 3038717-26.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new1.06 KB
new22.42 KB

Fixed the test case

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.

penyaskito’s picture

I've seen same issue with menu links provided by modules. IMHO @Berdir suggestion at #18 is the way to go.

m.stenta’s picture

Title: Block derivatives labels are always shown in the first language they are view in after cache rebuild » Block/menu/views derivative labels are always shown in the first language they are view in after cache rebuild
Component: block.module » language system

Does it make sense to update the title and component of this issue since it is not block-specific? @tim.plunkett mentioned that as well in comment #3:

Menus and Views are also affected.

I'm not sure what the "Component" should be. I'll set it to "language system" but please adjust if there's a better fit.

We encountered this with menu link derivatives that are created for each bundle of our entity type using $bundle->label(): #3262752: Record type menu items lose translations

Wrapping $bundle->label() in $this->t() in our module fixes it - but that is not the "correct" solution, as I understand it.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

benjy44’s picture

StatusFileSize
new22.42 KB

reroll for 9.5.x

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.

akalam’s picture

StatusFileSize
new23.55 KB
new1.02 KB

I've found when you have a menu link derivative, and you are using an TranslatableEntityLabelMarkup as title, the title gets overridden by the static_menu_link_overrides, transformed into a string and not displaying the proper translated text.

I'm adding a patch with a fix so the title override skips in case it has been defined as a TranslatableEntityLabelMarkup.

Note: I've found the patch doens't apply anymore for the 11.x branch, so I'm uploading it as it is, just introducing the change mentioned above. The patch is applying on 10.1.x

akalam’s picture

StatusFileSize
new23.69 KB

Rerolled against 10.2 and introduced a change to make it compatible with the change introduced on #3340159: Prevent empty block_content info fields from causing php deprecation notices when placing blocks with no label.

fromme’s picture

StatusFileSize
new23.69 KB

EntityType does not have label() method, that why i get:
Error: Call to undefined method Drupal\Core\Entity\ContentEntityType::label() in Drupal\Core\Entity\TranslatableEntityLabelMarkup->getTranslatedLabel() (line 233 of /var/www/html/web/core/lib/Drupal/Core/Entity/TranslatableEntityLabelMarkup.php).

Updated patch: replaced label() with getLabel()

tonibarbera’s picture

StatusFileSize
new20.59 KB

Rerolled against 10.3.1. Tested only with 10.3.1

akalam’s picture

I've created a MR based on #38, but incorporating the changes introduced on #37 on getTranslatedLabel() method

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.