EntityReferenceRevisionsItem:generateSampleValue can break when all bundle types are allowed

Created on 17 February 2022, over 3 years ago
Updated 28 May 2025, 9 days ago

Problem/Motivation

If a paragraph includes an ERR field that allows all paragraph types by selecting the "Include the selected below" radio button and not checking any of the paragraph types, and you use layout builder on the parent entity, attempting to manage layout results in an EntityStorageException. Just prior to this, watchdog logs a php warning: array_rand(): Array is empty in Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem::generateSampleValue() (line 496 of .../entity_reference_revisions/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php)

The entire issue can be worked around by checking all the bundle types.

Steps to reproduce

Create an entity (Node, e.g.; my use case was a Group) with an ERR field referencing a specific paragraph bundle.
Give that paragraph bundle an ERR that can reference all Paragraph types:

  1. select the "Include the selected below" radio button
  2. do not check any of the paragraph types listed ("Selection of Paragraph types for this field. Select none to allow all Paragraph types.")

Use layout builder to manage the entity display

Proposed resolution

The following code snippet from \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem:generateSampleValue() needs to modified to properly handle the situation where $handler_settings['target_bundles'] is empty, and come back with an array of all bundles

    // Determine referenceable bundles.
    $bundle_manager = \Drupal::service('entity_type.bundle.info');
    if (isset($handler_settings['target_bundles']) && is_array($handler_settings['target_bundles'])) {
      if (empty($handler_settings['negate'])) {
        $bundles = $handler_settings['target_bundles'];
      }
      else {
        $bundles = array_filter($bundle_manager->getBundleInfo($target_type_id), function ($bundle) use ($handler_settings) {
          return !in_array($bundle, $handler_settings['target_bundles'], TRUE);
        });
      }
    }
    else {
      $bundles = $bundle_manager->getBundleInfo($target_type_id);
    }
    $bundle = array_rand($bundles);

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.9

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States msypes

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.

  • πŸ‡΅πŸ‡·Puerto Rico rubenvarela

    This is still an issue on latest version. Can recreate when using devel generate.

    Patch still works, just on line 477 on 8.x-1.12

Production build 0.71.5 2024