function EntityReferenceUuidItem::generateSampleValue
Same name and namespace in other branches
- main core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Plugin/Field/FieldType/EntityReferenceUuidItem.php \Drupal\jsonapi_test_field_type\Plugin\Field\FieldType\EntityReferenceUuidItem::generateSampleValue()
Overrides EntityReferenceItem::generateSampleValue
File
-
core/
modules/ jsonapi/ tests/ modules/ jsonapi_test_field_type/ src/ Plugin/ Field/ FieldType/ EntityReferenceUuidItem.php, line 199
Class
- EntityReferenceUuidItem
- Defines the 'entity_reference_uuid' entity field type.
Namespace
Drupal\jsonapi_test_field_type\Plugin\Field\FieldTypeCode
public static function generateSampleValue(FieldDefinitionInterface $field_definition) : array {
$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'),
'handler_settings' => $field_definition->getSetting('handler_settings') ?: [],
'entity' => NULL,
];
$entity_type = \Drupal::entityTypeManager()->getDefinition($options['target_type']);
$options['handler_settings']['sort'] = [
'field' => $entity_type->getKey('uuid'),
'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);
return [
'target_uuid' => array_rand($referenceable[$group]),
];
}
return [];
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.