Null issue in aray_rand() in EntityReferenceItem

Created on 23 October 2023, about 1 year ago
Updated 30 October 2023, about 1 year ago

Issue give WSOD and half of issue in in core before it connect down in line to proper place

Message:
Message TypeError: array_rand(): Argument #1 ($array) must be of type array, bool given w array_rand() (line 357 in .../public_html/web/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php)

Description:
There is somehow possible to send empty $entity_type->getKey('bundle') which broke next call.

Solution is to patch this function with provided code: .../public_html/web/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php:

  protected static function getRandomBundle(EntityTypeInterface $entity_type, array $selection_settings) {

    if ($entity_type->getKey('bundle')) {
      $bundle_ids = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id());

      if (!empty($selection_settings['target_bundles'])) {
        $bundle_ids = $selection_settings['target_bundles'];
      }

      if(!empty($bundle_ids)){
        $bundle_ids = array_rand($bundle_ids);
        return $bundle_ids;
       }
    }
  }

Note:
I change order so when there is something it always return and I don't return anything if there isn't provided data to align with expectation before patch

Releated issue is what cause in first place to test it.
In my example I don't have any attribute fields. Creating placeholder one (example attribute with one value and not used anywhere) fix issue from commerce perspective.

Need only review, test and apply as patch :)

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Field 

Last updated 1 day ago

Created by

🇵🇱Poland Patryk Padus

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024