diff --git a/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php index 5037628..496bb1b 100644 --- a/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Entity; use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldDefinitionListenerInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionListenerInterface; @@ -20,7 +21,7 @@ * * For example, configurable fields defined and exposed by field.module. */ -interface DynamicallyFieldableEntityStorageInterface extends FieldableEntityStorageInterface, FieldStorageDefinitionListenerInterface { +interface DynamicallyFieldableEntityStorageInterface extends FieldableEntityStorageInterface, FieldStorageDefinitionListenerInterface, FieldDefinitionListenerInterface { /** * Determines if the storage contains any data. @@ -31,37 +32,6 @@ public function hasData(); /** - * Reacts to the creation of a field. - * - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition created. - */ - public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definition); - - /** - * Reacts to the update of a field. - * - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition being updated. - * @param \Drupal\Core\Field\FieldDefinitionInterface $original - * The original field definition; i.e., the definition before the update. - */ - public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original); - - /** - * Reacts to the deletion of a field. - * - * Stored values should not be wiped at once, but marked as 'deleted' so that - * they can go through a proper purge process later on. - * - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition being deleted. - * - * @see purgeFieldData() - */ - public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition); - - /** * Purges a batch of field data. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 467448c..9e113e0 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -693,7 +693,11 @@ public function getFieldMapByFieldType($field_type) { return $this->fieldMapByFieldType[$field_type]; } + /** + * {@inheritdoc} + */ public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definition) { + $this->getStorage($field_definition->getTargetEntityTypeId())->onFieldDefinitionCreate($field_definition); $entity_field_bundle_map = \Drupal::state()->get('entity_field_bundle_map'); if (!isset($entity_field_bundle_map[$field_definition->getTargetEntityTypeId()][$field_definition->getName()])) { $entity_field_bundle_map[$field_definition->getTargetEntityTypeId()][$field_definition->getName()] = [ @@ -708,6 +712,29 @@ public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definiti } /** + * {@inheritdoc} + */ + public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original) { + $this->getStorage($field_definition->getTargetEntityTypeId())->onFieldDefinitionUpdate($field_definition, $original); + } + + /** + * {@inheritdoc} + */ + public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) { + $this->getStorage($field_definition->getTargetEntityTypeId())->onFieldDefinitionDelete($field_definition); + $entity_field_bundle_map = \Drupal::state()->get('entity_field_bundle_map'); + $key = array_search($field_definition->getTargetBundle(), $entity_field_bundle_map[$field_definition->getTargetEntityTypeId()][$field_definition->getName()]['bundles']); + unset($entity_field_bundle_map[$field_definition->getTargetEntityTypeId()][$field_definition->getName()]['bundles'][$key]); + if (empty($entity_field_bundle_map[$field_definition->getTargetEntityTypeId()][$field_definition->getName()]['bundles'])) { + unset($entity_field_bundle_map[$field_definition->getTargetEntityTypeId()][$field_definition->getName()]['bundles']); + } + \Drupal::state()->set('entity_field_bundle_map', $entity_field_bundle_map); + $this->cacheBackend->delete('entity_field_map'); + $this->fieldMap = []; + } + + /** * Builds field storage definitions for an entity type. * * @param string $entity_type_id diff --git a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php index b3bda8b..8cc8460 100644 --- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -9,12 +9,14 @@ use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldDefinitionListenerInterface; use Drupal\Core\Field\FieldStorageDefinitionListenerInterface; /** * Provides an interface for entity type managers. */ -interface EntityManagerInterface extends PluginManagerInterface, EntityTypeListenerInterface, EntityBundleListenerInterface, FieldStorageDefinitionListenerInterface, CachedDiscoveryInterface { +interface EntityManagerInterface extends PluginManagerInterface, EntityTypeListenerInterface, EntityBundleListenerInterface, FieldStorageDefinitionListenerInterface, FieldDefinitionListenerInterface, CachedDiscoveryInterface { /** * Builds a list of entity type labels suitable for a Form API options list. diff --git a/core/lib/Drupal/Core/Field/FieldDefinitionListenerInterface.php b/core/lib/Drupal/Core/Field/FieldDefinitionListenerInterface.php new file mode 100644 index 0000000..c9c611e --- /dev/null +++ b/core/lib/Drupal/Core/Field/FieldDefinitionListenerInterface.php @@ -0,0 +1,47 @@ +isNew()) { // Notify the entity storage. - $entity_manager->getStorage($this->entity_type)->onFieldDefinitionCreate($this); + $entity_manager->onFieldDefinitionCreate($this); } else { // Some updates are always disallowed. @@ -160,7 +160,7 @@ public function preSave(EntityStorageInterface $storage) { throw new FieldException("Cannot change an existing field's storage."); } // Notify the entity storage. - $entity_manager->getStorage($this->entity_type)->onFieldDefinitionUpdate($this, $this->original); + $entity_manager->onFieldDefinitionUpdate($this, $this->original); } parent::preSave($storage); @@ -169,16 +169,6 @@ public function preSave(EntityStorageInterface $storage) { /** * {@inheritdoc} */ - public function postSave(EntityStorageInterface $storage, $update = TRUE) { - parent::postSave($storage, $update); - if (!$update) { - $this->entityManager()->onFieldDefinitionCreate($this); - } - } - - /** - * {@inheritdoc} - */ public function calculateDependencies() { parent::calculateDependencies(); // Mark the field_storage_config as a a dependency. @@ -217,7 +207,7 @@ public static function postDelete(EntityStorageInterface $storage, array $fields // Notify the entity storage. foreach ($fields as $field) { if (!$field->deleted) { - \Drupal::entityManager()->getStorage($field->entity_type)->onFieldDefinitionDelete($field); + \Drupal::entityManager()->onFieldDefinitionDelete($field); } }