diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php index c6618a6..eb1cb81 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php @@ -68,11 +68,11 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); if (!$update) { - $this->entityManager()->onBundleCreate($this->getEntityType()->getBundleOf(), $this->id()); + $this->entityManager()->onBundleCreate($this->id(), $this->getEntityType()->getBundleOf()); } elseif ($this->getOriginalId() != $this->id()) { $this->renameDisplays(); - $this->entityManager()->onBundleRename($this->getEntityType()->getBundleOf(), $this->getOriginalId(), $this->id()); + $this->entityManager()->onBundleRename($this->getOriginalId(), $this->id(), $this->getEntityType()->getBundleOf()); } } @@ -84,7 +84,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti foreach ($entities as $entity) { $entity->deleteDisplays(); - \Drupal::entityManager()->onBundleDelete($entity->getEntityType()->getBundleOf(), $entity->id()); + \Drupal::entityManager()->onBundleDelete($entity->id(), $entity->getEntityType()->getBundleOf()); } } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 6d75946..cf97d49 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -12,7 +12,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -abstract class ContentEntityStorageBase extends EntityStorageBase implements FieldableEntityStorageInterface { +abstract class ContentEntityStorageBase extends EntityStorageBase implements DynamicallyFieldableEntityStorageInterface { /** * The entity bundle key. @@ -106,21 +106,6 @@ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definiti /** * {@inheritdoc} */ - public function onBundleCreate($bundle) { } - - /** - * {@inheritdoc} - */ - public function onBundleRename($bundle, $bundle_new) { } - - /** - * {@inheritdoc} - */ - public function onBundleDelete($bundle) { } - - /** - * {@inheritdoc} - */ public function purgeFieldData(FieldDefinitionInterface $field_definition, $batch_size) { $items_by_entity = $this->readFieldItemsToPurge($field_definition, $batch_size); diff --git a/core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php b/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php similarity index 76% rename from core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php rename to core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php index aaab687..6d12c98 100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityStorageInterface.php +++ b/core/lib/Drupal/Core/Entity/DynamicallyFieldableEntityStorageInterface.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\Entity\FieldableEntityStorageInterface. + * Contains \Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface. */ namespace Drupal\Core\Entity; @@ -11,8 +11,16 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionListenerInterface; -interface FieldableEntityStorageInterface extends EntityStorageInterface, FieldStorageDefinitionListenerInterface { - +/** + * A storage that supports entity types with dynamic field definitions. + * + * A storage that implements this interface can react to the entity type's field + * definitions changing, due to modules being installed or uninstalled, or via + * field UI, or via code changes to the entity class. + * + * For example, configurable fields defined and exposed by field.module. + */ +interface DynamicallyFieldableEntityStorageInterface extends EntityStorageInterface, FieldStorageDefinitionListenerInterface { /** * Reacts to the creation of a field. * @@ -45,36 +53,6 @@ public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definiti public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition); /** - * Reacts to a bundle being created. - * - * @param string $bundle - * The name of the bundle created. - */ - public function onBundleCreate($bundle); - - /** - * Reacts to a bundle being renamed. - * - * This method runs before fields are updated with the new bundle name. - * - * @param string $bundle - * The name of the bundle being renamed. - * @param string $bundle_new - * The new name of the bundle. - */ - public function onBundleRename($bundle, $bundle_new); - - /** - * Reacts to a bundle being deleted. - * - * This method runs before fields are deleted. - * - * @param string $bundle - * The name of the bundle being deleted. - */ - public function onBundleDelete($bundle); - - /** * Purges a batch of field data. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition diff --git a/core/lib/Drupal/Core/Entity/EntityBundleListenerInterface.php b/core/lib/Drupal/Core/Entity/EntityBundleListenerInterface.php new file mode 100644 index 0000000..cbe96b3 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityBundleListenerInterface.php @@ -0,0 +1,53 @@ +entityManager->getStorage($entity_type_id) instanceof FieldableEntityStorageInterface) { + if ($this->entityManager->getStorage($entity_type_id) instanceof DynamicallyFieldableEntityStorageInterface) { $field_changes = array(); $storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id); $original_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id); diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index af25647..87dd552 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -1078,12 +1078,12 @@ public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $ /** * {@inheritdoc} */ - public function onBundleCreate($entity_type_id, $bundle) { + public function onBundleCreate($bundle, $entity_type_id) { $this->clearCachedBundles(); // Notify the entity storage. $storage = $this->getStorage($entity_type_id); - if ($storage instanceof FieldableEntityStorageInterface) { - $storage->onBundleCreate($bundle); + if ($storage instanceof DynamicallyFieldableEntityStorageInterface) { + $storage->onBundleCreate($bundle, $entity_type_id); } // Invoke hook_entity_bundle_create() hook. $this->moduleHandler->invokeAll('entity_bundle_create', array($entity_type_id, $bundle)); @@ -1092,12 +1092,12 @@ public function onBundleCreate($entity_type_id, $bundle) { /** * {@inheritdoc} */ - public function onBundleRename($entity_type_id, $bundle_old, $bundle_new) { + public function onBundleRename($bundle_old, $bundle_new, $entity_type_id) { $this->clearCachedBundles(); // Notify the entity storage. $storage = $this->getStorage($entity_type_id); - if ($storage instanceof FieldableEntityStorageInterface) { - $storage->onBundleRename($bundle_old, $bundle_new); + if ($storage instanceof DynamicallyFieldableEntityStorageInterface) { + $storage->onBundleRename($bundle_old, $bundle_new, $entity_type_id); } // Rename existing base field bundle overrides. @@ -1117,12 +1117,12 @@ public function onBundleRename($entity_type_id, $bundle_old, $bundle_new) { /** * {@inheritdoc} */ - public function onBundleDelete($entity_type_id, $bundle) { + public function onBundleDelete($bundle, $entity_type_id) { $this->clearCachedBundles(); // Notify the entity storage. $storage = $this->getStorage($entity_type_id); - if ($storage instanceof FieldableEntityStorageInterface) { - $storage->onBundleDelete($bundle); + if ($storage instanceof DynamicallyFieldableEntityStorageInterface) { + $storage->onBundleDelete($bundle, $entity_type_id); } // Invoke hook_entity_bundle_delete() hook. $this->moduleHandler->invokeAll('entity_bundle_delete', array($entity_type_id, $bundle)); diff --git a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php index 7a13fff..daea71f 100644 --- a/core/lib/Drupal/Core/Entity/EntityManagerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityManagerInterface.php @@ -13,7 +13,7 @@ /** * Provides an interface for entity type managers. */ -interface EntityManagerInterface extends PluginManagerInterface, EntityTypeListenerInterface, FieldStorageDefinitionListenerInterface { +interface EntityManagerInterface extends PluginManagerInterface, EntityTypeListenerInterface, EntityBundleListenerInterface, FieldStorageDefinitionListenerInterface { /** * Builds a list of entity type labels suitable for a Form API options list. @@ -465,42 +465,4 @@ public function loadEntityByUuid($entity_type_id, $uuid); */ public function getEntityTypeFromClass($class_name); - /** - * Reacts to the creation of a entity bundle. - * - * @param string $entity_type_id - * The entity type to which the bundle is bound; e.g. 'node' or 'user'. - * @param string $bundle - * The name of the bundle. - * - * @see entity_crud - */ - public function onBundleCreate($entity_type_id, $bundle); - - /** - * Reacts to the rename of a entity bundle. - * - * @param string $entity_type_id - * The entity type to which the bundle is bound; e.g. 'node' or 'user'. - * @param string $bundle_old - * The previous name of the bundle. - * @param string $bundle_new - * The new name of the bundle. - * - * @see entity_crud - */ - public function onBundleRename($entity_type_id, $bundle_old, $bundle_new); - - /** - * Reacts to the deletion of a entity bundle. - * - * @param string $entity_type_id - * The entity type to which the bundle is bound; e.g. 'node' or 'user'. - * @param string $bundle - * The bundle that was just deleted. - * - * @see entity_crud - */ - public function onBundleDelete($entity_type_id, $bundle); - } diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 58f537e..3136847 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -13,6 +13,7 @@ use Drupal\Core\Database\Database; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityStorageBase; +use Drupal\Core\Entity\EntityBundleListenerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageException; @@ -38,7 +39,7 @@ * * @ingroup entity_api */ -class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEntityStorageInterface, FieldableEntityStorageSchemaInterface { +class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEntityStorageInterface, FieldableEntityStorageSchemaInterface, EntityBundleListenerInterface { /** * The mapping of field columns to SQL tables. @@ -1544,7 +1545,17 @@ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definiti /** * {@inheritdoc} */ - public function onBundleRename($bundle, $bundle_new) { + public function onBundleCreate($bundle, $entity_type_id) { } + + /** + * {@inheritdoc} + */ + public function onBundleDelete($bundle, $entity_type_id) { } + + /** + * {@inheritdoc} + */ + public function onBundleRename($bundle, $bundle_new, $entity_type_id) { // The method runs before the field definitions are updated, so we use the // old bundle name. $field_definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle); diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index f0b06dd..ae2f890 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -944,7 +944,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { foreach ($entity_manager->getDefinitions() as $entity_type_id => $entity_type) { if ($entity_type->getProvider() == $module) { foreach (array_keys($entity_manager->getBundleInfo($entity_type_id)) as $bundle) { - $entity_manager->onBundleDelete($entity_type_id, $bundle); + $entity_manager->onBundleDelete($bundle, $entity_type_id); } } } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 12632e0..95675a6 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -6,7 +6,7 @@ use Drupal\Core\Config\ConfigImporter; use Drupal\Core\Entity\EntityTypeInterface; -use Drupal\Core\Entity\FieldableEntityStorageInterface; +use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface; use Drupal\Core\Extension\Extension; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; @@ -153,7 +153,7 @@ function field_system_info_alter(&$info, Extension $file, $type) { * Implements hook_entity_field_storage_info(). */ function field_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) { - if (\Drupal::entityManager()->getStorage($entity_type->id()) instanceof FieldableEntityStorageInterface) { + if (\Drupal::entityManager()->getStorage($entity_type->id()) instanceof DynamicallyFieldableEntityStorageInterface) { // Query by filtering on the ID as this is more efficient than filtering // on the entity_type property directly. $ids = \Drupal::entityQuery('field_storage_config') @@ -174,7 +174,7 @@ function field_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface * Implements hook_entity_bundle_field_info(). */ function field_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { - if (\Drupal::entityManager()->getStorage($entity_type->id()) instanceof FieldableEntityStorageInterface) { + if (\Drupal::entityManager()->getStorage($entity_type->id()) instanceof DynamicallyFieldableEntityStorageInterface) { // Query by filtering on the ID as this is more efficient than filtering // on the entity_type property directly. $ids = \Drupal::entityQuery('field_config') diff --git a/core/modules/field/src/Tests/FieldDataCountTest.php b/core/modules/field/src/Tests/FieldDataCountTest.php index 7f78601..7b175e7 100644 --- a/core/modules/field/src/Tests/FieldDataCountTest.php +++ b/core/modules/field/src/Tests/FieldDataCountTest.php @@ -20,7 +20,7 @@ class FieldDataCountTest extends FieldUnitTestBase { /** - * @var \Drupal\Core\Entity\FieldableEntityStorageInterface + * @var \Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface */ protected $storage; diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index bf0cded..06ec1dc 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -117,7 +117,7 @@ function entity_test_create_bundle($bundle, $text = NULL, $entity_type = 'entity $bundles += array($bundle => array('label' => $text ? $text : $bundle)); \Drupal::state()->set($entity_type . '.bundles', $bundles); - \Drupal::entityManager()->onBundleCreate($entity_type, $bundle); + \Drupal::entityManager()->onBundleCreate($bundle, $entity_type); } /** @@ -137,7 +137,7 @@ function entity_test_rename_bundle($bundle_old, $bundle_new, $entity_type = 'ent unset($bundles[$bundle_old]); \Drupal::state()->set($entity_type . '.bundles', $bundles); - \Drupal::entityManager()->onBundleRename($entity_type, $bundle_old, $bundle_new); + \Drupal::entityManager()->onBundleRename($bundle_old, $bundle_new, $entity_type); } /** @@ -154,7 +154,7 @@ function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') { unset($bundles[$bundle]); \Drupal::state()->set($entity_type . '.bundles', $bundles); - \Drupal::entityManager()->onBundleDelete($entity_type, $bundle); + \Drupal::entityManager()->onBundleDelete($bundle, $entity_type); } /**