When cloning the blocks it does not assign the correct language to the block.

Created on 11 August 2022, over 2 years ago
Updated 25 September 2023, over 1 year ago

Problem/Motivation

The cloned blocks from a library section aren't in the correct language. They are in the language that you created the library section item in.

This can cause all sorts of issues down the line with access checks on language. (as we have for a certain project)

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium nielsaers

Live updates comments and jobs are added and updated live.
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.

  • Merge request !14Update src/DeepCloningTrait.php β†’ (Open) created by nielsaers
  • πŸ‡§πŸ‡ͺBelgium weseze

    2 remarks on this approach:

    Templates that are stored in the library should have the language from the host entity from which they were cloned.
    With the changes you are proposing this could differ. Assume you are editing a page in a certain language (Chinese), while keeping the admin interface in another language (English). The current language would be English, but the template you are storing in the library should remain in the Chinese language, because it was cloned from a Chinese language page.

    Templates cloned into a page from the library should take on the language of the entity on which they are cloned.
    This is basically the same as the above, but in reverse.
    It also applies when you are simply translating your website and using templates created in another language.

    I suggest passing the host entity to the clone functions and getting the language from there.
    But I don't see anyway of doing that with the current code as it does not contain the host entity anywhere...

  • πŸ‡§πŸ‡ͺBelgium Thomas Cys

    Thomas Cys β†’ made their first commit to this issue’s fork.

  • Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    2 fail
  • πŸ‡§πŸ‡ͺBelgium kriboogh

    "But I don't see anyway of doing that with the current code as it does not contain the host entity anywhere..."

    I was looking at the code for this a bit. From the submitForm methods you could pass the section_storage context 'entity' which is assigned to $layout_entity (this is the node being edited) and pass that to the deepClone methods. There you can then use the $layout_entity->getLanguage()->getId() to set the langcode.

    Also then set the langcode on the section being create or updated before the $section->save in the submitForm code after the cloning has been done.
    $section->set('langcode', $layout_entity->language()->getId());

    I will try to cook up some patch.

  • πŸ‡ΊπŸ‡ΈUnited States jastraat

    I agree that it makes sense to save the templates to the library in their original language, but it also makes sense for the sections inserted into a node to have the same language as the rest of their host page.

    Are there any circumstances where someone might NOT want an inserted block to have the same language?

    The current MR would simply set the templates to use the site default language and when they were inserted into a node, continue to use the default site language on those blocks.

    If we want to only change the language to match when the blocks in a template are used in a new node, then the key place we need the host entity context is when cloneAndReplaceSectionComponents is called from ImportSectionFromLibraryController. We wouldn't need the context when adding the template to the library. I think we'd have to adjust the cloneAndReplaceSectionComponents function to take an optional language argument and then use something like:

         try {
            $layout_entity = $this->sectionStorage->getContextValue('entity');
            if ($layout_entity instanceof ContentEntityInterface) {
              $language = $layout_entity->language()->getId();
            }
          }
          catch (ContextException $e) {
            // Not inserting into an entity
          }
         if (!isset($language)) {
           $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
         }
    
         $deep_cloned_section = $this->cloneAndReplaceSectionComponents($cloned_section, $language);
    
  • πŸ‡ΊπŸ‡ΈUnited States jastraat

    I think we need a clearer idea of the desired behavior from a few users who are actively using the multi-lingual capabilities of Drupal.

  • πŸ‡§πŸ‡ͺBelgium weseze

    Everyone working with me (4 people) agree on my approach described in #3.

    1/ Export the templates in the language of the host entity they were created from
    2/ Give templates the language of the host entity on which they are imported (this one prevents serious technical issues also)

  • πŸ‡ΊπŸ‡ΈUnited States jastraat

    Alrighty - that makes sense. In that case, the current MR doesn't accomplish that, so if someone who is using language wants to tackle....?

  • πŸ‡ΊπŸ‡ΈUnited States jastraat
Production build 0.71.5 2024