\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::entityManager()->getDefinition($settings['target_type']); $target_id_data_type = 'string'; if ($target_type_info->entityClassImplements(FieldableEntityInterface::class)) { $id_definition = \Drupal::entityManager()->getBaseFieldDefinitions($settings['target_type'])[$target_type_info->getKey('id')]; if ($id_definition->getType() === 'integer') { $target_id_data_type = 'integer'; } } 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')) // The entity object is computed out of the entity ID. ->setComputed(TRUE) ->setReadOnly(FALSE) ->setTargetDefinition(EntityDataDefinition::create($settings['target_type'])) // We can add a constraint for the target entity type. The list of // referenceable bundles is a field setting, so the corresponding // constraint is added dynamically in ::getConstraints(). ->addConstraint('EntityType', $settings['target_type']); return $properties; } /** * {@inheritdoc} */ public static function mainPropertyName() { return 'target_id'; } /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { $target_type = $field_definition->getSetting('target_type'); $target_type_info = \Drupal::entityManager()->getDefinition($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'), 'handler_settings' => $field_definition->getSetting('handler_settings') ?: [], 'entity' => NULL, ]; $entity_type = \Drupal::entityManager()->getDefinition($options['target_type']); $options['handler_settings']['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['handler_settings']); // 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 ($bundle_key = $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' => t('Type of item to reference'), '#options' => \Drupal::entityManager()->getEntityTypeLabels(TRUE), '#default_value' => $this->getSetting('target_type'), '#required' => TRUE, '#disabled' => $has_data, '#size' => 1, ]; 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' => [[get_class($this), 'fieldSettingsAjaxProcess']], '#element_validate' => [[get_class($this), 'fieldSettingsFormValidate']], ]; $form['handler'] = [ '#type' => 'details', '#title' => t('Reference type'), '#open' => TRUE, '#tree' => TRUE, '#process' => [[get_class($this), 'formProcessMergeParent']], ]; $form['handler']['handler'] = [ '#type' => 'select', '#title' => t('Reference method'), '#options' => $handlers_options, '#default_value' => $field->getSetting('handler'), '#required' => TRUE, '#ajax' => TRUE, '#limit_validation_errors' => [], ]; $form['handler']['handler_submit'] = [ '#type' => 'submit', '#value' => t('Change handler'), '#limit_validation_errors' => [], '#attributes' => [ 'class' => ['js-hide'], ], '#submit' => [[get_class($this), '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); $manager = \Drupal::entityManager(); $target_entity_type = $manager->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type')); // Depend on default values entity types configurations. if ($default_value = $field_definition->getDefaultValueLiteral()) { foreach ($default_value as $value) { if (is_array($value) && isset($value['target_uuid'])) { $entity = \Drupal::entityManager()->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 = $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::entityManager()->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_manager = \Drupal::entityManager(); $target_entity_type = $entity_manager->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type')); // Try to update the default value config dependency, if possible. if ($default_value = $field_definition->getDefaultValueLiteral()) { foreach ($default_value as $key => $value) { if (is_array($value) && isset($value['target_uuid'])) { $entity = $entity_manager->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_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'] = NULL; $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::entityManager()->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 and allows access to * the form state. * * @see static::fieldSettingsForm() */ public static function fieldSettingsAjaxProcess($form, FormStateInterface $form_state) { static::fieldSettingsAjaxProcessElement($form, $form); return $form; } /** * Adds entity_reference specific properties to AJAX form elements from the * field settings form. * * @see static::fieldSettingsAjaxProcess() */ public static function fieldSettingsAjaxProcessElement(&$element, $main_form) { if (!empty($element['#ajax'])) { $element['#ajax'] = [ 'callback' => [get_called_class(), 'settingsAjax'], 'wrapper' => $main_form['#id'], 'element' => $main_form['#array_parents'], ]; } foreach (Element::children($element) as $key) { static::fieldSettingsAjaxProcessElement($element[$key], $main_form); } } /** * Render API callback: Moves entity_reference specific Form API elements * (i.e. 'handler_settings') up a level 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; } /** * Ajax callback for the handler settings form. * * @see static::fieldSettingsForm() */ public static function settingsAjax($form, FormStateInterface $form_state) { return NestedArray::getValue($form, $form_state->getTriggeringElement()['#ajax']['element']); } /** * Submit handler for the non-JS case. * * @see static::fieldSettingsForm() */ public static function settingsAjaxSubmit($form, FormStateInterface $form_state) { $form_state->setRebuild(); } /** * {@inheritdoc} */ public static function getPreconfiguredOptions() { $options = []; // Add all the commonly referenced entity types as distinct pre-configured // options. $entity_types = \Drupal::entityManager()->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(), 'field_storage_config' => [ 'settings' => [ 'target_type' => $entity_type->id(), ], ], ]; } return $options; } }