It's possible a referenced paragraph does not exist, resulting in a WSOD

Created on 18 August 2025, about 2 months ago

Problem/Motivation

It's possible a referenced paragraph does not exist, resulting in a WSOD with this error:
Error: Call to a member function createDuplicate() on null in Drupal\layout_builder_at\Plugin\Field\FieldWidget\LayoutBuilderCopyWidget->cloneEntity() (line 344 of /.../web/modules/contrib/layout_builder_at/src/Plugin/Field/FieldWidget/LayoutBuilderCopyWidget.php).

Steps to reproduce

I'm not sure how this came to be, but one of our nodes had a block with a referenced paragraph that doesn't seem to exist.

Proposed resolution

Lines 338 to 344 of the LayoutBuilderCopyWidget contain this code:

          $values = $clone->get($definition->getName())->getValue();
          if (!empty($values)) {
            foreach ($values as $value) {
              /** @var \Drupal\Core\Entity\EntityInterface $reference */
              /** @var \Drupal\Core\Entity\EntityInterface $reference_clone */
              $reference = \Drupal::service('entity_type.manager')->getStorage($target_type)->load($value['target_id']);
              $reference_clone = $reference->createDuplicate();

I would suggest replacing it with:

          /** @var \Drupal\Core\Entity\EntityInterface[] $references */
          $references = $clone->get($definition->getName())->referencedEntities();
          if (!empty($references)) {
            foreach ($references as $reference) {
              $reference_clone = $reference->createDuplicate();

This ensures only existing references can be cloned, and `createDuplicate` can't be called on a null-value.

Remaining tasks

I'll create a patch/MR

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇧🇪Belgium RandalV

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024