diff --git a/core/lib/Drupal/Core/Field/DataDynamicReferenceDefinition.php b/core/lib/Drupal/Core/Field/DataDynamicReferenceDefinition.php index f0a95be..9ae150f 100644 --- a/core/lib/Drupal/Core/Field/DataDynamicReferenceDefinition.php +++ b/core/lib/Drupal/Core/Field/DataDynamicReferenceDefinition.php @@ -11,6 +11,9 @@ /** * A typed data definition class for defining dynamic references. + * + * This is different from the base class in that the entity type being + * referenced is part of the data, rather than being a setting. */ class DataDynamicReferenceDefinition extends DataReferenceDefinition { @@ -29,7 +32,7 @@ class DataDynamicReferenceDefinition extends DataReferenceDefinition { */ public static function create($target_data_type) { $definition['type'] = 'dynamic_' . $target_data_type . '_reference'; - /* @var $definition \Drupal\Core\TypedData\DataReferenceDefinition; */ + /** @var \Drupal\Core\TypedData\DataReferenceDefinition $definition */ $definition = new static($definition); return $definition->setTargetDefinition(\Drupal::typedDataManager()->createDataDefinition($target_data_type)); } diff --git a/core/lib/Drupal/Core/Field/DynamicEntityReferenceFieldItemList.php b/core/lib/Drupal/Core/Field/DynamicEntityReferenceFieldItemList.php index 3635267..ba7d15b 100644 --- a/core/lib/Drupal/Core/Field/DynamicEntityReferenceFieldItemList.php +++ b/core/lib/Drupal/Core/Field/DynamicEntityReferenceFieldItemList.php @@ -12,6 +12,9 @@ /** * Defines a item list class for dynamic entity reference fields. + * + * This is different from the base class in that the entity type being + * referenced is part of the field data, rather than being a setting. */ class DynamicEntityReferenceFieldItemList extends EntityReferenceFieldItemList { @@ -20,12 +23,12 @@ class DynamicEntityReferenceFieldItemList extends EntityReferenceFieldItemList { */ public function referencedEntities() { if (empty($this->list)) { - return array(); + return []; } // Collect the IDs of existing entities to load, and directly grab the // "autocreate" entities that are already populated in $item->entity. - $target_entities = $ids = array(); + $target_entities = $ids = []; foreach ($this->list as $delta => $item) { if ($item->hasNewEntity()) { $target_entities[$delta] = $item->entity; @@ -64,14 +67,14 @@ public static function processDefaultValue($default_value, FieldableEntityInterf if ($default_value) { // Convert UUIDs to numeric IDs. - $all_uuids = array(); + $all_uuids = []; foreach ($default_value as $delta => $properties) { if (isset($properties['target_uuid'])) { $target_type = $properties['target_type']; $all_uuids[$target_type][$delta] = $properties['target_uuid']; } } - $entity_uuids = array(); + $entity_uuids = []; foreach ($all_uuids as $target_type => $uuids) { if ($uuids) { $entity_ids = \Drupal::entityQuery($target_type) @@ -80,7 +83,7 @@ public static function processDefaultValue($default_value, FieldableEntityInterf $entities = \Drupal::entityManager() ->getStorage($target_type) ->loadMultiple($entity_ids); - $entity_uuids[$target_type] = array(); + $entity_uuids[$target_type] = []; foreach ($entities as $id => $entity) { $entity_uuids[$target_type][$entity->uuid()] = $id; } @@ -107,15 +110,19 @@ public static function processDefaultValue($default_value, FieldableEntityInterf * {@inheritdoc} */ public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) { - // We want to bypass the EntityReferenceItem::defaultValuesFormSubmit() - $default_value = FieldItemList::defaultValuesFormSubmit($element, $form, $form_state); + // We can not call EntityReferenceItem::defaultValuesFormSubmit(), so + // copy the code from FieldItemList::defaultValuesFormSubmit(). Extracts + // the submitted value as an array. + $widget = $this->defaultValueWidget($form_state); + $widget->extractFormValues($this, $element, $form_state); + $default_value = $this->getValue(); // Convert numeric IDs to UUIDs to ensure config deployability. - $all_ids = array(); + $all_ids = []; foreach ($default_value as $delta => $properties) { $all_ids[$properties['target_type']][] = $properties['target_id']; } - $entities = array(); + $entities = []; foreach ($all_ids as $target_type => $ids) { $entities[$target_type] = \Drupal::entityManager() ->getStorage($target_type) diff --git a/core/lib/Drupal/Core/Field/Plugin/DataType/DynamicEntityReference.php b/core/lib/Drupal/Core/Field/Plugin/DataType/DynamicEntityReference.php index 65741fd..a383a9e 100644 --- a/core/lib/Drupal/Core/Field/Plugin/DataType/DynamicEntityReference.php +++ b/core/lib/Drupal/Core/Field/Plugin/DataType/DynamicEntityReference.php @@ -39,7 +39,7 @@ public function getTarget() { // If we have a valid reference, return the entity object which is typed // data itself. $target_type = $this->parent->getValue()['target_type']; - $entity = entity_load($target_type, $this->id); + $entity = \Drupal::entityManager()->getStorage($target_type)->load($this->id); $this->target = isset($entity) ? $entity->getTypedData() : NULL; } return $this->target; diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DynamicEntityReferenceItem.php index 0b710af..646de04 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DynamicEntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DynamicEntityReferenceItem.php @@ -19,7 +19,8 @@ * Defines the 'dynamic_entity_reference' entity field type. * * Supported settings (below the definition's 'settings' key) are: - * - exclude_entity_types: Allow user to include or exclude entity_types. + * - exclude_entity_types: If TRUE, exclude entity the types from the list. If + * FALSE, only allow those entities types in the list. * - entity_type_ids: The entity type ids that can or cannot be referenced. * * @FieldType( @@ -81,7 +82,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'target_type' => array( 'description' => 'The Entity Type ID of the target entity.', 'type' => 'varchar', - 'length' => EntityTypeInterface::BUNDLE_MAX_LENGTH, + 'length' => EntityTypeInterface::ID_MAX_LENGTH, ), ); @@ -212,13 +213,13 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def if (is_array($field_definition->default_value) && count($field_definition->default_value)) { $target_entity_types = static::getAllEntityTypeIds($field_definition->getFieldStorageDefinition()->getSettings()); foreach ($target_entity_types as $target_entity_type) { - $key = $target_entity_type instanceof ConfigEntityType ? 'config' : 'content'; foreach ($field_definition->default_value as $default_value) { if (is_array($default_value) && isset($default_value['target_uuid'])) { $entity = \Drupal::entityManager()->loadEntityByUuid($target_entity_type->id(), $default_value['target_uuid']); // If the entity does not exist do not create the dependency. // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue() if ($entity) { + $key = $entity->getConfigDependencyKey(); $dependencies[$key][] = $entity->getConfigDependencyName(); } } diff --git a/core/modules/entity_reference/src/EntityReferenceController.php b/core/modules/entity_reference/src/EntityReferenceController.php index ce81a33..8b4bd3b 100644 --- a/core/modules/entity_reference/src/EntityReferenceController.php +++ b/core/modules/entity_reference/src/EntityReferenceController.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Defines route controller for entity reference. @@ -65,13 +66,13 @@ public static function create(ContainerInterface $container) { * Defaults to ''. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException - * Throws access denied when either the field storage or field does not - * exists or the user does not have access to edit the field. + * When either the field is not an entity reference field or the user does + * not have access to view and edit the field. * * @return \Symfony\Component\HttpFoundation\JsonResponse * The matched labels as json. */ - public function handleAutocomplete(Request $request, $type, $field_name, $entity_type, $bundle_name, $entity_id) { + public function handleAutocomplete(Request $request, $type, $field_name, $entity_type, $bundle_name, $entity_id = '') { $definitions = $this->entityManager()->getFieldDefinitions($entity_type, $bundle_name); if (!isset($definitions[$field_name])) { @@ -118,6 +119,12 @@ public function handleAutocomplete(Request $request, $type, $field_name, $entity * (optional) The entity ID the entity reference field is attached to. * Defaults to ''. * + * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException + * If the field does not exist. + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * When either the field is not a dynamic entity reference field or the user + * does not have access to view and edit the field. + * * @return \Symfony\Component\HttpFoundation\JsonResponse * The matched labels as json. */ @@ -125,7 +132,7 @@ public function handleDynamicAutocomplete(Request $request, $field_name, $entity $definitions = $this->entityManager()->getFieldDefinitions($entity_type, $bundle_name); if (!isset($definitions[$field_name])) { - throw new AccessDeniedHttpException(); + throw new NotFoundHttpException; } /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ $field_definition = $definitions[$field_name];