function EntityReferenceItem::generateSampleValue

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::generateSampleValue()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::generateSampleValue()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::generateSampleValue()
1 method overrides EntityReferenceItem::generateSampleValue()
FileItem::generateSampleValue in core/modules/file/src/Plugin/Field/FieldType/FileItem.php

File

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

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
  // An associative array keyed by the reference type, target type, and
  // bundle.
  static $recursion_tracker = [];
  $manager = \Drupal::service('plugin.manager.entity_reference_selection');
  // Instead of calling $manager->getSelectionHandler($field_definition)
  // replicate the behavior to be able to override the sorting settings.
  $options = [
    'target_type' => $field_definition->getFieldStorageDefinition()
      ->getSetting('target_type'),
    'handler' => $field_definition->getSetting('handler'),
    'entity' => NULL,
  ] + $field_definition->getSetting('handler_settings') ?: [];
  $entity_type = \Drupal::entityTypeManager()->getDefinition($options['target_type']);
  $options['sort'] = [
    'field' => $entity_type->getKey('id'),
    'direction' => 'DESC',
  ];
  $selection_handler = $manager->getInstance($options);
  // Select a random number of references between the last 50 referenceable
  // entities created.
  if ($referenceable = $selection_handler->getReferenceableEntities(NULL, 'CONTAINS', 50)) {
    $group = array_rand($referenceable);
    $values['target_id'] = array_rand($referenceable[$group]);
    return $values;
  }
  // Attempt to create a sample entity, avoiding recursion.
  $entity_storage = \Drupal::entityTypeManager()->getStorage($options['target_type']);
  if ($entity_storage instanceof ContentEntityStorageInterface) {
    $bundle = static::getRandomBundle($entity_type, $options);
    // Track the generated entity by reference type, target type, and bundle.
    $key = $field_definition->getTargetEntityTypeId() . ':' . $options['target_type'] . ':' . $bundle;
    // If entity generation was attempted but did not finish, do not continue.
    if (isset($recursion_tracker[$key])) {
      return [];
    }
    // Mark this as an attempt at generation.
    $recursion_tracker[$key] = TRUE;
    // Mark the sample entity as being a preview.
    $values['entity'] = $entity_storage->createWithSampleValues($bundle, [
      'in_preview' => TRUE,
    ]);
    // Remove the indicator once the entity is successfully generated.
    unset($recursion_tracker[$key]);
    return $values;
  }
}

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