class EntityReferenceItem
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem
Defines the 'entity_reference' entity field type.
Supported settings (below the definition's 'settings' key) are:
- target_type: The entity type to reference. Required.
Attributes
#[FieldType(id: "entity_reference", label: new TranslatableMarkup("Entity reference"), description: [ new TranslatableMarkup("Choose any content or configuration type to reference on the next screen"),], category: "reference", default_widget: "entity_reference_autocomplete", default_formatter: "entity_reference_label", list_class: EntityReferenceFieldItemList::class)]
Hierarchy
- class \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\TypedDataInterface, \Drupal\Component\Plugin\PluginInspectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\TypedData\TypedDataTrait
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface extends \Drupal\Core\TypedData\TypedData
- class \Drupal\Core\Field\FieldItemBase implements \Drupal\Core\Field\FieldItemInterface extends \Drupal\Core\TypedData\Plugin\DataType\Map
- class \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemBase implements \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemInterface extends \Drupal\Core\Field\FieldItemBase
- class \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem implements \Drupal\Core\TypedData\OptionsProviderInterface, \Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface extends \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemBase
- class \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemBase implements \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItemInterface extends \Drupal\Core\Field\FieldItemBase
- class \Drupal\Core\Field\FieldItemBase implements \Drupal\Core\Field\FieldItemInterface extends \Drupal\Core\TypedData\Plugin\DataType\Map
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface extends \Drupal\Core\TypedData\TypedData
Expanded class hierarchy of EntityReferenceItem
19 files declare their use of EntityReferenceItem
- DefaultSelection.php in core/
lib/ Drupal/ Core/ Entity/ Plugin/ EntityReferenceSelection/ DefaultSelection.php - EntityReferenceFieldItemNormalizer.php in core/
modules/ serialization/ src/ Normalizer/ EntityReferenceFieldItemNormalizer.php - EntityReferenceFieldItemNormalizerTest.php in core/
modules/ serialization/ tests/ src/ Unit/ Normalizer/ EntityReferenceFieldItemNormalizerTest.php - EntityReferenceFieldItemNormalizerTrait.php in core/
modules/ serialization/ src/ Normalizer/ EntityReferenceFieldItemNormalizerTrait.php - EntityReferenceFormatterBase.php in core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldFormatter/ EntityReferenceFormatterBase.php
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldType/ EntityReferenceItem.php, line 35
Namespace
Drupal\Core\Field\Plugin\Field\FieldTypeView source
class EntityReferenceItem extends EntityReferenceItemBase implements OptionsProviderInterface, PreconfiguredFieldUiOptionsInterface { /** * {@inheritdoc} */ public static function defaultStorageSettings() { return [ 'target_type' => \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user', ] + parent::defaultStorageSettings(); } /** * {@inheritdoc} */ public static function defaultFieldSettings() { return [ 'handler' => 'default', 'handler_settings' => [], ] + parent::defaultFieldSettings(); } /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $settings = $field_definition->getSettings(); $target_type_info = \Drupal::entityTypeManager()->getDefinition($settings['target_type']); // If the target entity type doesn't have an ID key, we cannot determine // the target_id data type. if (!$target_type_info->hasKey('id')) { throw new FieldException('Entity type "' . $target_type_info->id() . '" has no ID key and cannot be targeted by entity reference field "' . $field_definition->getName() . '"'); } $target_id_data_type = $target_type_info->hasIntegerId() ? 'integer' : 'string'; if ($target_id_data_type === 'integer') { $target_id_definition = DataReferenceTargetDefinition::create('integer')->setLabel(new TranslatableMarkup('@label ID', [ '@label' => $target_type_info->getLabel(), ])) ->setSetting('unsigned', TRUE); } else { $target_id_definition = DataReferenceTargetDefinition::create('string')->setLabel(new TranslatableMarkup('@label ID', [ '@label' => $target_type_info->getLabel(), ])); } $target_id_definition->setRequired(TRUE); $properties['target_id'] = $target_id_definition; $properties['entity'] = DataReferenceDefinition::create('entity')->setLabel($target_type_info->getLabel()) ->setDescription(new TranslatableMarkup('The referenced entity')) ->setComputed(TRUE) ->setReadOnly(FALSE) ->setTargetDefinition(EntityDataDefinition::create($settings['target_type'])) ->addConstraint('EntityType', [ 'type' => $settings['target_type'], ]); return $properties; } /** * {@inheritdoc} */ public static function storageSettingsSummary(FieldStorageDefinitionInterface $storage_definition) : array { $summary = parent::storageSettingsSummary($storage_definition); $target_type = $storage_definition->getSetting('target_type'); $target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type); if (!empty($target_type_info)) { $summary[] = new TranslatableMarkup('Reference type: @entity_type', [ '@entity_type' => $target_type_info->getLabel(), ]); } return $summary; } /** * {@inheritdoc} */ public static function fieldSettingsSummary(FieldDefinitionInterface $field_definition) : array { $summary = parent::fieldSettingsSummary($field_definition); $target_type = $field_definition->getFieldStorageDefinition() ->getSetting('target_type'); $handler_settings = $field_definition->getSetting('handler_settings'); if (!isset($handler_settings['target_bundles'])) { return $summary; } /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_bundle_information */ $entity_bundle_information = \Drupal::service('entity_type.bundle.info'); $bundles = array_intersect_key($entity_bundle_information->getBundleLabels($target_type), $handler_settings['target_bundles']); $bundle_label = \Drupal::entityTypeManager()->getDefinition($target_type) ->getBundleLabel(); if (!empty($bundles)) { $summary[] = new FormattableMarkup('@bundle: @entity_type', [ '@bundle' => $bundle_label ?: new TranslatableMarkup('Bundle'), '@entity_type' => implode(', ', $bundles), ]); } return $summary; } /** * {@inheritdoc} */ public static function mainPropertyName() { return 'target_id'; } /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { $target_type = $field_definition->getSetting('target_type'); try { $target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type); } catch (PluginNotFoundException) { throw new FieldException(sprintf("Field '%s' on entity type '%s' references a target entity type '%s' which does not exist.", $field_definition->getName(), $field_definition->getTargetEntityTypeId(), $target_type)); } $properties = static::propertyDefinitions($field_definition)['target_id']; if ($target_type_info->entityClassImplements(FieldableEntityInterface::class) && $properties->getDataType() === 'integer') { $columns = [ 'target_id' => [ 'description' => 'The ID of the target entity.', 'type' => 'int', 'unsigned' => TRUE, ], ]; } else { $columns = [ 'target_id' => [ 'description' => 'The ID of the target entity.', 'type' => 'varchar_ascii', // If the target entities act as bundles for another entity type, // their IDs should not exceed the maximum length for bundles. 'length' => $target_type_info->getBundleOf() ? EntityTypeInterface::BUNDLE_MAX_LENGTH : 255, ], ]; } $schema = [ 'columns' => $columns, 'indexes' => [ 'target_id' => [ 'target_id', ], ], ]; return $schema; } /** * {@inheritdoc} */ public function getConstraints() { $constraints = parent::getConstraints(); // Remove the 'AllowedValuesConstraint' validation constraint because entity // reference fields already use the 'ValidReference' constraint. foreach ($constraints as $key => $constraint) { if ($constraint instanceof AllowedValuesConstraint) { unset($constraints[$key]); } } return $constraints; } /** * {@inheritdoc} */ public function setValue($values, $notify = TRUE) { if (isset($values) && !is_array($values)) { // If either a scalar or an object was passed as the value for the item, // assign it to the 'entity' property since that works for both cases. $this->set('entity', $values, $notify); } else { parent::setValue($values, FALSE); // Support setting the field item with only one property, but make sure // values stay in sync if only property is passed. // NULL is a valid value, so we use array_key_exists(). if (is_array($values) && array_key_exists('target_id', $values) && !isset($values['entity'])) { $this->onChange('target_id', FALSE); } elseif (is_array($values) && !array_key_exists('target_id', $values) && isset($values['entity'])) { $this->onChange('entity', FALSE); } elseif (is_array($values) && array_key_exists('target_id', $values) && isset($values['entity'])) { // If both properties are passed, verify the passed values match. The // only exception we allow is when we have a new entity: in this case // its actual id and target_id will be different, due to the new entity // marker. $entity_id = $this->get('entity') ->getTargetIdentifier(); // If the entity has been saved and we're trying to set both the // target_id and the entity values with a non-null target ID, then the // value for target_id should match the ID of the entity value. The // entity ID as returned by $entity->id() might be a string, but the // provided target_id might be an integer - therefore we have to do a // non-strict comparison. if (!$this->entity ->isNew() && $values['target_id'] !== NULL && $entity_id != $values['target_id']) { throw new \InvalidArgumentException('The target id and entity passed to the entity reference item do not match.'); } } // Notify the parent if necessary. if ($notify && $this->parent) { $this->parent ->onChange($this->getName()); } } } /** * {@inheritdoc} */ public function getValue() { $values = parent::getValue(); // If there is an unsaved entity, return it as part of the field item values // to ensure idempotency of getValue() / setValue(). if ($this->hasNewEntity()) { $values['entity'] = $this->entity; } return $values; } /** * {@inheritdoc} */ public function onChange($property_name, $notify = TRUE) { // Make sure that the target ID and the target property stay in sync. if ($property_name == 'entity') { $property = $this->get('entity'); $target_id = $property->isTargetNew() ? NULL : $property->getTargetIdentifier(); $this->writePropertyValue('target_id', $target_id); } elseif ($property_name == 'target_id') { $this->writePropertyValue('entity', $this->target_id); } parent::onChange($property_name, $notify); } /** * {@inheritdoc} */ public function isEmpty() { // Avoid loading the entity by first checking the 'target_id'. if ($this->target_id !== NULL) { return FALSE; } if ($this->entity && $this->entity instanceof EntityInterface) { return FALSE; } return TRUE; } /** * {@inheritdoc} */ public function preSave() { if ($this->hasNewEntity()) { // Save the entity if it has not already been saved by some other code. if ($this->entity ->isNew()) { $this->entity ->save(); } // Make sure the parent knows we are updating this property so it can // react properly. $this->target_id = $this->entity ->id(); } if (!$this->isEmpty() && $this->target_id === NULL) { $this->target_id = $this->entity ->id(); } } /** * {@inheritdoc} */ 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; } } /** * Gets a bundle for a given entity type and selection options. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * @param array $selection_settings * An array of selection settings. * * @return string|null * Either the bundle string, or NULL if there is no bundle. */ 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); } } /** * {@inheritdoc} */ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { $element['target_type'] = [ '#type' => 'select', '#title' => $this->t('Type of item to reference'), '#default_value' => $this->getSetting('target_type'), '#required' => TRUE, '#disabled' => $has_data, '#size' => 1, ]; // Only allow the field to target entity types that have an ID key. This // is enforced in ::propertyDefinitions(). $entity_type_manager = \Drupal::entityTypeManager(); $filter = function (string $entity_type_id) use ($entity_type_manager) : bool { return $entity_type_manager->getDefinition($entity_type_id) ->hasKey('id'); }; $options = \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE); foreach ($options as $group_name => $group) { $element['target_type']['#options'][$group_name] = array_filter($group, $filter, ARRAY_FILTER_USE_KEY); } return $element; } /** * {@inheritdoc} */ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { $field = $form_state->getFormObject() ->getEntity(); // Get all selection plugins for this entity type. $selection_plugins = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionGroups($this->getSetting('target_type')); $handlers_options = []; foreach (array_keys($selection_plugins) as $selection_group_id) { // We only display base plugins (e.g. 'default', 'views', ...) and not // entity type specific plugins (e.g. 'default:node', 'default:user', // ...). if (array_key_exists($selection_group_id, $selection_plugins[$selection_group_id])) { $handlers_options[$selection_group_id] = Html::escape($selection_plugins[$selection_group_id][$selection_group_id]['label']); } elseif (array_key_exists($selection_group_id . ':' . $this->getSetting('target_type'), $selection_plugins[$selection_group_id])) { $selection_group_plugin = $selection_group_id . ':' . $this->getSetting('target_type'); $handlers_options[$selection_group_plugin] = Html::escape($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']); } } $form = [ '#type' => 'container', '#process' => [ [ static::class, 'fieldSettingsAjaxProcess', ], ], '#element_validate' => [ [ static::class, 'fieldSettingsFormValidate', ], ], ]; $form['handler'] = [ '#type' => 'details', '#title' => $this->t('Reference type'), '#open' => TRUE, '#tree' => TRUE, '#process' => [ [ static::class, 'formProcessMergeParent', ], ], ]; $form['handler']['handler'] = [ '#type' => 'select', '#title' => $this->t('Reference method'), '#options' => $handlers_options, '#default_value' => $field->getSetting('handler'), '#required' => TRUE, // Use a form process callback to build #ajax property properly and also // to avoid code duplication. // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsAjaxProcess() '#ajax' => TRUE, '#limit_validation_errors' => [], ]; $form['handler']['handler_submit'] = [ '#type' => 'submit', '#name' => 'handler_settings_submit', '#value' => $this->t('Change handler'), '#limit_validation_errors' => [], '#attributes' => [ 'class' => [ 'js-hide', ], ], '#submit' => [ [ static::class, 'settingsAjaxSubmit', ], ], ]; $form['handler']['handler_settings'] = [ '#type' => 'container', '#attributes' => [ 'class' => [ 'entity_reference-settings', ], ], ]; $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field); $form['handler']['handler_settings'] += $handler->buildConfigurationForm([], $form_state); return $form; } /** * Form element validation handler; Invokes selection plugin's validation. * * @param array $form * The form where the settings form is being included in. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state of the (entire) configuration form. */ public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) { $field = $form_state->getFormObject() ->getEntity(); $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field); $handler->validateConfigurationForm($form, $form_state); } /** * Determines whether the item holds an unsaved entity. * * This is notably used for "autocreate" widgets, and more generally to * support referencing freshly created entities (they will get saved * automatically as the hosting entity gets saved). * * @return bool * TRUE if the item holds an unsaved entity. */ public function hasNewEntity() { return !$this->isEmpty() && $this->target_id === NULL && $this->entity ->isNew(); } /** * {@inheritdoc} */ public static function calculateDependencies(FieldDefinitionInterface $field_definition) { $dependencies = parent::calculateDependencies($field_definition); $entity_type_manager = \Drupal::entityTypeManager(); $target_entity_type = $entity_type_manager->getDefinition($field_definition->getFieldStorageDefinition() ->getSetting('target_type')); // Depend on default values entity types configurations. if ($default_value = $field_definition->getDefaultValueLiteral()) { $entity_repository = \Drupal::service('entity.repository'); foreach ($default_value as $value) { if (is_array($value) && isset($value['target_uuid'])) { $entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']); // If the entity does not exist do not create the dependency. // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue() if ($entity) { $dependencies[$target_entity_type->getConfigDependencyKey()][] = $entity->getConfigDependencyName(); } } } } // Depend on target bundle configurations. Dependencies for 'target_bundles' // also covers the 'auto_create_bundle' setting, if any, because its value // is included in the 'target_bundles' list. $handler = $field_definition->getSetting('handler_settings'); if (!empty($handler['target_bundles'])) { if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) { if ($storage = $entity_type_manager->getStorage($bundle_entity_type_id)) { foreach ($storage->loadMultiple($handler['target_bundles']) as $bundle) { $dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName(); } } } } return $dependencies; } /** * {@inheritdoc} */ public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) { $dependencies = parent::calculateStorageDependencies($field_definition); $target_entity_type = \Drupal::entityTypeManager()->getDefinition($field_definition->getSetting('target_type')); $dependencies['module'][] = $target_entity_type->getProvider(); return $dependencies; } /** * {@inheritdoc} */ public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) { $changed = parent::onDependencyRemoval($field_definition, $dependencies); $entity_type_manager = \Drupal::entityTypeManager(); $target_entity_type = $entity_type_manager->getDefinition($field_definition->getFieldStorageDefinition() ->getSetting('target_type')); // Try to update the default value config dependency, if possible. if ($default_value = $field_definition->getDefaultValueLiteral()) { $entity_repository = \Drupal::service('entity.repository'); foreach ($default_value as $key => $value) { if (is_array($value) && isset($value['target_uuid'])) { $entity = $entity_repository->loadEntityByUuid($target_entity_type->id(), $value['target_uuid']); // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue() if ($entity && isset($dependencies[$entity->getConfigDependencyKey()][$entity->getConfigDependencyName()])) { unset($default_value[$key]); $changed = TRUE; } } } if ($changed) { $field_definition->setDefaultValue($default_value); } } // Update the 'target_bundles' handler setting if a bundle config dependency // has been removed. $bundles_changed = FALSE; $handler_settings = $field_definition->getSetting('handler_settings'); if (!empty($handler_settings['target_bundles'])) { if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) { if ($storage = $entity_type_manager->getStorage($bundle_entity_type_id)) { foreach ($storage->loadMultiple($handler_settings['target_bundles']) as $bundle) { if (isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { unset($handler_settings['target_bundles'][$bundle->id()]); // If this bundle is also used in the 'auto_create_bundle' // setting, disable the auto-creation feature completely. $auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE; if ($auto_create_bundle && $auto_create_bundle == $bundle->id()) { $handler_settings['auto_create'] = FALSE; $handler_settings['auto_create_bundle'] = NULL; } $bundles_changed = TRUE; } } } } } if ($bundles_changed) { $field_definition->setSetting('handler_settings', $handler_settings); } $changed |= $bundles_changed; return $changed; } /** * {@inheritdoc} */ public function getPossibleValues(?AccountInterface $account = NULL) { return $this->getSettableValues($account); } /** * {@inheritdoc} */ public function getPossibleOptions(?AccountInterface $account = NULL) { return $this->getSettableOptions($account); } /** * {@inheritdoc} */ public function getSettableValues(?AccountInterface $account = NULL) { // Flatten options first, because "settable options" may contain group // arrays. $flatten_options = OptGroup::flattenOptions($this->getSettableOptions($account)); return array_keys($flatten_options); } /** * {@inheritdoc} */ public function getSettableOptions(?AccountInterface $account = NULL) { $field_definition = $this->getFieldDefinition(); if (!$options = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field_definition, $this->getEntity()) ->getReferenceableEntities()) { return []; } // Rebuild the array by changing the bundle key into the bundle label. $target_type = $field_definition->getSetting('target_type'); $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($target_type); $return = []; foreach ($options as $bundle => $entity_ids) { // The label does not need sanitizing since it is used as an optgroup // which is only supported by select elements and auto-escaped. $bundle_label = (string) $bundles[$bundle]['label']; $return[$bundle_label] = $entity_ids; } return count($return) == 1 ? reset($return) : $return; } /** * Render API callback: Processes the field settings form. * * @see static::fieldSettingsForm() */ public static function fieldSettingsAjaxProcess($form, FormStateInterface $form_state) { static::fieldSettingsAjaxProcessElement($form, $form); return $form; } /** * Adds the field settings to AJAX form elements. * * @see static::fieldSettingsAjaxProcess() */ public static function fieldSettingsAjaxProcessElement(&$element, $main_form) { // Elements are marked as TRUE ('#ajax' => TRUE,), so not empty. if (!empty($element['#ajax'])) { $element['#ajax'] = [ 'trigger_as' => [ 'name' => 'handler_settings_submit', ], 'wrapper' => 'field-combined', 'element' => $main_form['#array_parents'], ]; } foreach (Element::children($element) as $key) { static::fieldSettingsAjaxProcessElement($element[$key], $main_form); } } /** * Render API callback that moves entity reference elements up a level. * * The elements (i.e. 'handler_settings') are moved for easier processing by * the validation and submission handlers. * * @see _entity_reference_field_settings_process() */ public static function formProcessMergeParent($element) { $parents = $element['#parents']; array_pop($parents); $element['#parents'] = $parents; return $element; } /** * Submit handler for the non-JS case. * * @see static::fieldSettingsForm() */ public static function settingsAjaxSubmit($form, FormStateInterface $form_state) { $form_storage =& $form_state->getStorage(); unset($form_storage['default_value_widget']); $form_state->setRebuild(); } /** * {@inheritdoc} */ public static function getPreconfiguredOptions() { $options = []; // Add all the commonly referenced entity types as distinct pre-configured // options. $entity_types = \Drupal::entityTypeManager()->getDefinitions(); $common_references = array_filter($entity_types, function (EntityTypeInterface $entity_type) { return $entity_type->isCommonReferenceTarget(); }); /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ foreach ($common_references as $entity_type) { $options[$entity_type->id()] = [ 'label' => $entity_type->getLabel(), 'description' => new TranslatableMarkup('A reference to a(n) @item', [ '@item' => $entity_type->getSingularLabel(), ]), 'field_storage_config' => [ 'settings' => [ 'target_type' => $entity_type->id(), ], ], ]; } return $options; } /** * {@inheritdoc} */ public static function getReferenceableBundles(FieldDefinitionInterface $field_definition) : array { $settings = $field_definition->getSettings(); $target_type_id = $settings['target_type']; $handler_settings = $settings['handler_settings']; $has_target_bundles = isset($handler_settings['target_bundles']) && !empty($handler_settings['target_bundles']); $target_bundles = $has_target_bundles ? $handler_settings['target_bundles'] : array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo($target_type_id)); return [ $target_type_id => $target_bundles, ]; } }Members
Title Sort descending Modifiers Object type Summary Overriden Title Overrides DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages. DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization. DependencySerializationTrait::__sleep public function 2 DependencySerializationTrait::__wakeup public function 2 EntityReferenceItem::calculateDependencies public static function Calculates dependencies for field items. Overrides FieldItemBase::calculateDependencies EntityReferenceItem::calculateStorageDependencies public static function Calculates dependencies for field items on the storage level. Overrides FieldItemBase::calculateStorageDependencies EntityReferenceItem::defaultFieldSettings public static function Defines the field-level settings for this plugin. Overrides FieldItemBase::defaultFieldSettings 1 EntityReferenceItem::defaultStorageSettings public static function Defines the storage-level settings for this plugin. Overrides FieldItemBase::defaultStorageSettings 1 EntityReferenceItem::fieldSettingsAjaxProcess public static function Render API callback: Processes the field settings form. EntityReferenceItem::fieldSettingsAjaxProcessElement public static function Adds the field settings to AJAX form elements. EntityReferenceItem::fieldSettingsForm public function Returns a form for the field-level settings. Overrides FieldItemBase::fieldSettingsForm 1 EntityReferenceItem::fieldSettingsFormValidate public static function Form element validation handler; Invokes selection plugin's validation. EntityReferenceItem::fieldSettingsSummary public static function Returns a short summary of the field's field-level settings. Overrides FieldItemBase::fieldSettingsSummary EntityReferenceItem::formProcessMergeParent public static function Render API callback that moves entity reference elements up a level. EntityReferenceItem::generateSampleValue public static function Generates placeholder field values. Overrides FieldItemBase::generateSampleValue 2 EntityReferenceItem::getConstraints public function Gets a list of validation constraints. Overrides TypedData::getConstraints EntityReferenceItem::getPossibleOptions public function Returns an array of possible values with labels for display. Overrides OptionsProviderInterface::getPossibleOptions EntityReferenceItem::getPossibleValues public function Returns an array of possible values. Overrides OptionsProviderInterface::getPossibleValues EntityReferenceItem::getPreconfiguredOptions public static function Returns preconfigured field options for a field type. Overrides PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions 1 EntityReferenceItem::getRandomBundle protected static function Gets a bundle for a given entity type and selection options. EntityReferenceItem::getReferenceableBundles public static function Returns the referenceable entity types and bundles. Overrides EntityReferenceItemInterface::getReferenceableBundles EntityReferenceItem::getSettableOptions public function Returns an array of settable values with labels for display. Overrides OptionsProviderInterface::getSettableOptions EntityReferenceItem::getSettableValues public function Returns an array of settable values. Overrides OptionsProviderInterface::getSettableValues EntityReferenceItem::getValue public function Gets the data value. Overrides Map::getValue EntityReferenceItem::hasNewEntity public function Determines whether the item holds an unsaved entity. 1 EntityReferenceItem::isEmpty public function Determines whether the data structure is empty. Overrides Map::isEmpty 1 EntityReferenceItem::mainPropertyName public static function Returns the name of the main property, if any. Overrides FieldItemBase::mainPropertyName 1 EntityReferenceItem::onChange public function React to changes to a child property or item. Overrides Map::onChange 1 EntityReferenceItem::onDependencyRemoval public static function Informs the plugin that a dependency of the field will be deleted. Overrides FieldItemBase::onDependencyRemoval EntityReferenceItem::preSave public function Defines custom presave behavior for field values. Overrides FieldItemBase::preSave 2 EntityReferenceItem::propertyDefinitions public static function Defines field item properties. Overrides FieldItemInterface::propertyDefinitions 2 EntityReferenceItem::schema public static function Returns the schema for the field. Overrides FieldItemInterface::schema 2 EntityReferenceItem::settingsAjaxSubmit public static function Submit handler for the non-JS case. EntityReferenceItem::setValue public function Overrides \Drupal\Core\TypedData\TypedData::setValue(). Overrides FieldItemBase::setValue 1 EntityReferenceItem::storageSettingsForm public function Returns a form for the storage-level settings. Overrides FieldItemBase::storageSettingsForm 1 EntityReferenceItem::storageSettingsSummary public static function Returns a short summary of the field's storage-level settings. Overrides FieldItemBase::storageSettingsSummary 1 EntityReferenceItemBase::__get public function Magic method: Gets a property value. Overrides FieldItemBase::__get FieldItemBase::delete public function Defines custom delete behavior for field values. Overrides FieldItemInterface::delete 2 FieldItemBase::deleteRevision public function Defines custom revision delete behavior for field values. Overrides FieldItemInterface::deleteRevision FieldItemBase::fieldSettingsFromConfigData public static function Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface::fieldSettingsFromConfigData 1 FieldItemBase::fieldSettingsToConfigData public static function Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface::fieldSettingsToConfigData 1 FieldItemBase::getEntity public function Gets the entity that field belongs to. Overrides FieldItemInterface::getEntity FieldItemBase::getFieldDefinition public function Gets the field definition. Overrides FieldItemInterface::getFieldDefinition FieldItemBase::getLangcode public function Gets the langcode of the field values held in the object. Overrides FieldItemInterface::getLangcode FieldItemBase::getSetting protected function Returns the value of a field setting. FieldItemBase::getSettings protected function Returns the array of field settings. FieldItemBase::postSave public function Defines custom post-save behavior for field values. Overrides FieldItemInterface::postSave 2 FieldItemBase::storageSettingsFromConfigData public static function Returns a settings array in the field type's canonical representation. Overrides FieldItemInterface::storageSettingsFromConfigData 2 FieldItemBase::storageSettingsToConfigData public static function Returns a settings array that can be stored as a configuration value. Overrides FieldItemInterface::storageSettingsToConfigData 2 FieldItemBase::view public function Returns a renderable array for a single field item. Overrides FieldItemInterface::view FieldItemBase::writePropertyValue protected function Different to the parent Map class, we avoid creating property objects as
far as possible in order to optimize performance. Thus we just update
$this->values if no property object has been created yet.Overrides Map::writePropertyValue FieldItemBase::__construct public function Constructs a TypedData object given its definition and context. Overrides TypedData::__construct 3 FieldItemBase::__isset public function Magic method: Determines whether a property is set. Overrides FieldItemInterface::__isset FieldItemBase::__set public function Magic method: Sets a property value. Overrides FieldItemInterface::__set 1 FieldItemBase::__unset public function Magic method: Unsets a property. Overrides FieldItemInterface::__unset Map::$definition protected property The data definition. Overrides TypedData::$definition Map::$properties protected property The array of properties. Map::$values protected property An array of values for the contained properties. Map::applyDefaultValue public function Applies the default value. Overrides TypedData::applyDefaultValue 4 Map::get public function Gets a property object. Overrides ComplexDataInterface::get Map::getIterator public function Retrieves the iterator for the object. Map::getProperties public function Gets an array of property objects. Overrides ComplexDataInterface::getProperties Map::getString public function Returns a string representation of the data. Overrides TypedData::getString Map::set public function Sets a property value. Overrides ComplexDataInterface::set Map::toArray public function Returns an array of all property values. Overrides ComplexDataInterface::toArray 1 Map::__clone public function Magic method: Implements a deep clone. StringTranslationTrait::$stringTranslation protected property The string translation service. 3 StringTranslationTrait::formatPlural protected function Formats a string containing a count of items. StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language. StringTranslationTrait::getStringTranslation protected function Gets the string translation service. StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2 StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1 TypedData::$name protected property The property name. TypedData::$parent protected property The parent typed data object. TypedData::createInstance public static function Constructs a TypedData object given its definition and context. Overrides TypedDataInterface::createInstance TypedData::getDataDefinition public function Gets the data definition. Overrides TypedDataInterface::getDataDefinition TypedData::getName public function Returns the name of a property or item. Overrides TypedDataInterface::getName TypedData::getParent public function Returns the parent data structure; i.e. either complex data or a list. Overrides TypedDataInterface::getParent TypedData::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition TypedData::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId TypedData::getPropertyPath public function Returns the property path of the data. Overrides TypedDataInterface::getPropertyPath TypedData::getRoot public function Returns the root of the typed data tree. Overrides TypedDataInterface::getRoot TypedData::setContext public function Sets the context of a property or item via a context aware parent. Overrides TypedDataInterface::setContext TypedData::validate public function Validates the currently set data value. Overrides TypedDataInterface::validate TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types. TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2 TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2 Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
- class \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\TypedDataInterface, \Drupal\Component\Plugin\PluginInspectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\TypedData\TypedDataTrait