diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 3d258b2..8073b18 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -16,6 +16,7 @@ use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\TypedData\DataReferenceDefinition; +use Drupal\entity_reference\Exception\MissingDefaultValueException; /** * Defines the 'entity_reference' entity field type. @@ -255,7 +256,12 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def 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']); - $dependencies[$key][] = $entity->getConfigDependencyName(); + if ($entity) { + $dependencies[$key][] = $entity->getConfigDependencyName(); + } + else { + throw new MissingDefaultValueException($target_entity_type->id(), $default_value['target_uuid'], $field_definition->getName()); + } } } } diff --git a/core/modules/config/src/Tests/AssertConfigEntityImportTrait.php b/core/modules/config/src/Tests/AssertConfigEntityImportTrait.php new file mode 100644 index 0000000..0f8ad2b --- /dev/null +++ b/core/modules/config/src/Tests/AssertConfigEntityImportTrait.php @@ -0,0 +1,42 @@ +uuid(); + $entity_type_id = $entity->getEntityTypeId(); + $original_data = $entity->toArray(); + // Copy everything to staging. + $this->copyConfig(\Drupal::service('config.storage'), \Drupal::service('config.storage.staging')); + // Delete the configuration from active. Don't worry about side effects of + // deleting config like fields cleaning up field storages. The coming import + // should recreate everything as necessary. + $entity->delete(); + $this->configImporter()->reset()->import(); + $imported_entity = \Drupal::entityManager()->loadEntityByUuid($entity_type_id, $entity_uuid); + $this->assertIdentical($original_data, $imported_entity->toArray()); + } + +} diff --git a/core/modules/entity_reference/src/Exception/MissingDefaultValueException.php b/core/modules/entity_reference/src/Exception/MissingDefaultValueException.php new file mode 100644 index 0000000..2070e27 --- /dev/null +++ b/core/modules/entity_reference/src/Exception/MissingDefaultValueException.php @@ -0,0 +1,33 @@ +assertTrue(in_array($referenced_entities[0]->getConfigDependencyName(), $config_entity['dependencies'][$key]), String::format('Expected @type dependency @name found', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()])); + // Ensure that the field can be imported without change even after the + // default value deleted. + $referenced_entities[0]->delete(); + $this->assertConfigEntityImport(FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName)); + + // Test re-saving the field now that the default value does not exist. + $msg = 'Expected MissingDefaultValueException exception thrown.'; + try { + FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName)->save(); + $this->fail($msg); + } + catch (MissingDefaultValueException $e) { + $this->pass($msg); + $this->assertEqual($e->getMessage(), sprintf("Default value '%s' of entity type '%s' for '%s' field was not found.", $referenced_entities[0]->uuid(), $referenced_entities[0]->getEntityTypeId(), $this->fieldName)); + } } }