function EntityReferenceItem::getRandomBundle

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::getRandomBundle()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::getRandomBundle()
  3. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::getRandomBundle()

Gets a bundle for a given entity type and selection options.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type.

array $selection_settings: An array of selection settings.

Return value

string|null Either the bundle string, or NULL if there is no bundle.

1 call to EntityReferenceItem::getRandomBundle()
EntityReferenceItem::generateSampleValue in core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 400

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

protected static function getRandomBundle(EntityTypeInterface $entity_type, array $selection_settings) {
    if ($entity_type->getKey('bundle')) {
        if (!empty($selection_settings['target_bundles'])) {
            $bundle_ids = $selection_settings['target_bundles'];
        }
        else {
            $bundle_ids = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id());
        }
        return array_rand($bundle_ids);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.