Layout builder block cloned wrong revision.

Created on 2 May 2022, about 2 years ago
Updated 24 February 2023, over 1 year ago

Problem/Motivation

After creating the translation I have the wrong blocks.

Steps to reproduce

In Drupal website (9.3.12) create translation
Enable "Copy blocks into translation"
In translated version, It pulls the oldest revision from block_content

Proposed resolution

In cloneEntity function load entity by entity revision instead of entity Id.

      $entity = \Drupal::service('entity_type.manager')->getStorage($entity_type_id)->load($entity_id);
      $clone = $entity->createDuplicate();
πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡¦πŸ‡²Armenia norsargsyan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

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

  • πŸ‡¨πŸ‡³China skyredwang Shanghai

    The problem can be reproduced if the page to be translated has blocks which are not pointing to its latest revision.

    I tested the patch, it fixed the problem, and the code makes sense to me. I have been thinking on writing a test against it. But, I haven't found a way to update an inline block outside the layout builder to create a new revision which is not referenced by the layout overrides. Maybe, there is another easy way to reproduce the problem on a fresh install?

  • πŸ‡¨πŸ‡³China skyredwang Shanghai

    I added one more line null safety check to handle the case that a revisioned reference in a block could be missing (possibily deleted)

                  /** @var \Drupal\Core\Entity\EntityInterface $reference */
                  /** @var \Drupal\Core\Entity\EntityInterface $reference_clone */
                  $reference = \Drupal::service('entity_type.manager')->getStorage($target_type)->loadRevision($value['target_revision_id']);
                  if ($reference) {
                    $reference_clone = $reference->createDuplicate();
                    $reference_clone->save();
                    $new_values[] = [
                      'target_id' => $reference_clone->id(),
                      'target_revision_id' => $reference_clone->getRevisionId(),
                    ];
                  }
    
Production build 0.69.0 2024