WSOD in Layout Builder

Created on 6 September 2022, about 2 years ago
Updated 19 January 2023, almost 2 years ago

Problem/Motivation

When I place a paragraph field in the Layout Builder with the formatter "Paragraphs table" and table mode "Bootstrap Table" the layout builder crashes. When reloading the page, the following errors appear:

First this error appears:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getEntityTypeId() on null in Drupal\paragraphs_table\Plugin\Field\FieldFormatter\ParagraphsTableFormatter->getPreparedRenderedEntities() (line 808 of modules/contrib/paragraphs_table/src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php).
Drupal\paragraphs_table\Plugin\Field\FieldFormatter\ParagraphsTableFormatter->getPreparedRenderedEntities('paragraph', 'padde_acoes', Array, Array, Array, 'default', 1) (Line: 359)
Drupal\paragraphs_table\Plugin\Field\FieldFormatter\ParagraphsTableFormatter->viewElements(Object, 'pt-pt') (Line: 89)
Drupal\Core\Field\FormatterBase->view(Object, 'pt-pt') (Line: 76)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->view(Object, NULL) (Line: 263)
...


When fix the above error, this error appears:

Symfony\Component\Routing\Exception\InvalidParameterException: Parameter "entity_id" for route "paragraphs_item.add_page" must match "[^/]++" ("" given) to generate a corresponding URL. in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 203 of core/lib/Drupal/Core/Routing/UrlGenerator.php).
Drupal\Core\Routing\UrlGenerator->getInternalPathFromRoute('paragraphs_item.add_page', Object, Array, Array) (Line: 292)
Drupal\Core\Routing\UrlGenerator->generateFromRoute('paragraphs_item.add_page', Array, Array, 1) (Line: 105)
Drupal\Core\Render\MetadataBubblingUrlGenerator->generateFromRoute('paragraphs_item.add_page', Array, Array, 1) (Line: 765)
...

It's a PHP8.1 + Drupal 9.4.5 installation

Steps to reproduce

  1. Create a paragraph
  2. Create a paragraph field in a content type
  3. Configure the paragraph field with the paragraph created earlier
  4. In the "Manage View" tab activate the Layout Builder in Full view mode
  5. Inside Layout Builder add the paragraph field created earlier
  6. Set the field formatter to "Paragraphs table" and table mode "Bootstrap Table"
  7. Click on save block. (At this point an AJAX will appear, but nothing will happen)
  8. Reload the page. (The WSOD screen will appear with the first error described above.)

Proposed resolution

Make a validation if the entity id exists before using.

For the first error described, we must validate if an entity exists before adding operations:

if ($show_operation && $parent = $entity->getParentEntity()) {
  $entityType = $parent->getEntityTypeId();
  if ($entityType == 'paragraph') {
    $entityType = 'paragraphs_item';
  }
  $destination = implode('/', [
    $entityType,
    $parent->id(),
  ]);
  $operation = $this->paragraphsTableLinksAction($paragraphs_id, $destination);
  $rows[$delta]['data']['operation'] = $operation;
}

Referred Code

For the second reported error, you must validate if the $entityId variable is filled:

if (($hasPermission && $this->customPermissions['create']) || in_array('administrator', $userRoles) && $entityId) {
  $destination = \Drupal::service('path.current')->getPath();
  $dialog_width = 800;
  $addButton = [
    '#type' => 'container',
    'add-button' => [
      '#type' => 'link',
      '#title' => Markup::create('<i class="bi bi-plus" aria-hidden="true"></i> ' . $this->t('Add')),
      '#url' => Url::fromRoute('paragraphs_item.add_page', $params = [
        'paragraph_type' => $targetBundle,
        'entity_type' => $field_definition->getTargetEntityTypeId(),
        'entity_field' => $fieldName,
        'entity_id' => $entityId,
      ],
      ...
  ];
  ...
}

Referred Code

In this approach we guarantee that actions related to an entity only appear when this entity exists.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇵🇹Portugal lolgm

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.

Production build 0.71.5 2024