diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 0c9ad70..113549b 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -580,6 +580,50 @@ field.value.entity_reference: type: string label: 'Target UUID' +# Schema for the configuration of the Entity reference field type. + +field.storage_settings.dynamic_entity_reference: + type: mapping + label: 'Dynamic entity reference settings' + mapping: + target_type: + type: string + label: 'Type of item to reference' + exclude_entity_types: + type: boolean + label: 'Exclude the selected items' + entity_type_ids: + type: sequence + label: 'Entity Type IDs' + sequence: + - type: string + label: 'Entity Type ID' + +field.field_settings.dynamic_entity_reference: + type: sequence + label: 'Dynamic entity reference settings' + sequence: + - type: mapping + label: 'Entity Type ID' + mapping: + handler: + type: string + label: 'Reference method' + handler_settings: + type: entity_reference.[%parent.handler].handler_settings + label: 'Reference method settings' + +field.value.dynamic_entity_reference: + type: mapping + label: 'Default value' + mapping: + target_id: + type: string + label: 'Entity ID' + target_type: + type: string + label: 'Type of item to reference' + # Schema for the configuration of the Boolean field type. field.storage_settings.boolean: diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index 9ffed0a..38ad835 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -294,30 +294,3 @@ field.formatter.settings.entity_reference_label: link: type: boolean label: 'Link label to the referenced entity' - -field.storage_settings.dynamic_entity_reference: - type: mapping - label: 'Dynamic entity reference settings' - mapping: - target_type: - type: string - label: 'Type of item to reference' - exclude_entity_types: - type: boolean - label: 'Exclude the selected items' - entity_type_ids: - type: sequence - label: 'Entity Type IDs' - sequence: - - type: string - label: 'Entity Type ID' -field.value.dynamic_entity_reference: - type: mapping - label: 'Default value' - mapping: - target_id: - type: string - label: 'Entity ID' - target_type: - type: string - label: 'Type of item to reference' 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 646de04..6f8ca1e 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DynamicEntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DynamicEntityReferenceItem.php @@ -47,9 +47,31 @@ public static function defaultStorageSettings() { /** * {@inheritdoc} */ + public static function defaultFieldSettings() { + $default_settings = array(); + $labels = \Drupal::entityManager()->getEntityTypeLabels(TRUE); + $options = $labels['Content']; + // Field storage settings are not accessible here so we are assuming that + // all the entity types are referenceable by default. + // See https://www.drupal.org/node/2346273#comment-9385179 for more details. + foreach (array_keys($options) as $entity_type_id) { + $default_settings[$entity_type_id] = array( + 'handler' => 'default', + 'handler_settings' => array(), + ); + } + return $default_settings; + } + + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { - $properties['target_id'] = DataDefinition::create('string') - ->setLabel(t('Entity ID')); + // Only supports content entities. + $properties['target_id'] = DataDefinition::create('integer') + ->setLabel(t('Entity ID')) + ->setSetting('unsigned', TRUE) + ->setRequired(TRUE); $properties['target_type'] = DataDefinition::create('string') ->setLabel(t('Target Entity Type')); $properties['entity'] = DataDynamicReferenceDefinition::create('entity') @@ -76,8 +98,8 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) $columns = array( 'target_id' => array( 'description' => 'The ID of the target entity.', - 'type' => 'varchar', - 'length' => '255', + 'type' => 'int', + 'unsigned' => TRUE, ), 'target_type' => array( 'description' => 'The Entity Type ID of the target entity.', @@ -100,12 +122,14 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) * {@inheritdoc} */ public function onChange($property_name, $notify = TRUE) { - if ($property_name == 'target_type') { - $this->get('entity')->getDataDefinition()->getTargetDefinition()->setEntityTypeId($this->get('target_type')->getValue()); + /* @var \Drupal\dynamic_entity_reference\Plugin\DataType\DynamicEntityReference $entity_property */ + $entity_property = $this->get('entity'); + if ($property_name == 'target_type' && !$entity_property->getValue()) { + $entity_property->getTargetDefinition()->setEntityTypeId($this->get('target_type')->getValue()); } // Make sure that the target type and the target property stay in sync. elseif ($property_name == 'entity') { - $this->writePropertyValue('target_type', $this->get('entity')->getValue()->getEntityTypeId()); + $this->writePropertyValue('target_type', $entity_property->getValue()->getEntityTypeId()); } parent::onChange($property_name, $notify); } @@ -168,15 +192,12 @@ public function getValue($include_computed = FALSE) { */ public function preSave() { if ($this->hasNewEntity()) { - $this->entity->save(); - $this->target_id = $this->entity->id(); - $this->target_type = $this->entity->getEntityTypeId(); - } - // Handle the case where an unsaved entity was directly set using the public - // 'entity' property and then saved before this entity. In this case - // ::hasNewEntity() will return FALSE but $this->target_id will still be - // empty. - if ((empty($this->target_id) || empty($this->target_type)) && $this->entity) { + // 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(); $this->target_type = $this->entity->getEntityTypeId(); } @@ -217,10 +238,9 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def 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() + // @see \Drupal\Core\Field\DynamicEntityReferenceFieldItemList::processDefaultValue() if ($entity) { - $key = $entity->getConfigDependencyKey(); - $dependencies[$key][] = $entity->getConfigDependencyName(); + $dependencies[$entity->getConfigDependencyKey()][] = $entity->getConfigDependencyName(); } } } diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml index 657ebe55..2ae82cb 100644 --- a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml @@ -5,4 +5,4 @@ core: 8.x package: Testing version: VERSION dependencies: - - entity_reference \ No newline at end of file + - entity_reference diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml index 84bf73b..f9708fa 100644 --- a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml @@ -1,3 +1,3 @@ administer dynamic_entity_test content: title: 'Administer dynamic_entity_test content' - description: 'Manage dynamic_entity_test content' \ No newline at end of file + description: 'Manage dynamic_entity_test content' diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml index be0e0a8..6041ca0 100644 --- a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml @@ -20,4 +20,4 @@ entity.dynamic_entity_test.add_form: _title: 'Add' _controller: '\Drupal\dynamic_entity_reference_test\Controller\DynamicEntityController::addForm' requirements: - _permission: 'administer dynamic_entity_test content' \ No newline at end of file + _permission: 'administer dynamic_entity_test content' diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php index 292ac70..1c5ffb9 100644 --- a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php @@ -25,4 +25,4 @@ public function addForm() { return $this->entityFormBuilder()->getForm($entity); } -} \ No newline at end of file +} diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Entity/DynamicEntityTest.php b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Entity/DynamicEntityTest.php index 2f5faea..3b00556 100644 --- a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Entity/DynamicEntityTest.php +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Entity/DynamicEntityTest.php @@ -118,7 +118,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['dynamic_references'] = BaseFieldDefinition::create('dynamic_entity_reference') ->setLabel(t('References')) ->setDescription(t('Reference another entity.')) - ->setSetting('handler', 'default') ->setRequired(FALSE) ->setDisplayOptions('form', [ 'type' => 'dynamic_entity_reference_default',