Commit 9617c203 authored by alexpott's avatar alexpott

Issue #2319171 by andypost, swentel | yched: Move entity_invoke_bundle_hook() to EntityManager.

parent 61215d04
......@@ -43,44 +43,6 @@ function entity_get_bundles($entity_type = NULL) {
}
}
/**
* Notifies modules about an operation that was performed on a entity bundle.
*
* @param string $hook
* One of 'create', 'rename' or 'delete'.
* @param string $entity_type
* The entity type to which the bundle is bound.
* @param string $bundle
* The name of the bundle on which the operation was performed.
* @param string|null $bundle_new
* The new name of the bundle in case of a 'rename' operation. Defaults to
* NULL.
*/
function entity_invoke_bundle_hook($hook, $entity_type, $bundle, $bundle_new = NULL) {
$entity_manager = \Drupal::entityManager();
$entity_manager->clearCachedBundles();
// Notify the entity storage.
$method = 'onBundle' . ucfirst($hook);
$storage = $entity_manager->getStorage($entity_type);
if (method_exists($storage, $method)) {
$storage->$method($bundle, $bundle_new);
}
// Notify the entity manager.
if (method_exists($entity_manager, $method)) {
$entity_manager->$method($entity_type, $bundle, $bundle_new);
}
// Invoke hook_entity_bundle_*() hooks.
\Drupal::moduleHandler()->invokeAll('entity_bundle_' . $hook, array($entity_type, $bundle, $bundle_new));
// Clear the cached field definitions (not needed in case of 'create').
if ($hook == 'rename' || $hook == 'delete') {
$entity_manager->clearCachedFieldDefinitions();
}
}
/**
* Loads an entity from the database.
*
......
......@@ -68,11 +68,11 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
entity_invoke_bundle_hook('create', $this->getEntityType()->getBundleOf(), $this->id());
$this->entityManager()->onBundleCreate($this->getEntityType()->getBundleOf(), $this->id());
}
elseif ($this->getOriginalId() != $this->id()) {
$this->renameDisplays();
entity_invoke_bundle_hook('rename', $this->getEntityType()->getBundleOf(), $this->getOriginalId(), $this->id());
$this->entityManager()->onBundleRename($this->getEntityType()->getBundleOf(), $this->getOriginalId(), $this->id());
}
}
......@@ -84,7 +84,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
foreach ($entities as $entity) {
$entity->deleteDisplays();
entity_invoke_bundle_hook('delete', $entity->getEntityType()->getBundleOf(), $entity->id());
\Drupal::entityManager()->onBundleDelete($entity->getEntityType()->getBundleOf(), $entity->id());
}
}
......
......@@ -994,19 +994,30 @@ public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
}
/**
* Acts on entity bundle rename.
*
* @param string $entity_type_id
* The entity type to which the bundle is bound.
* @param string $bundle_old
* The previous name of the bundle.
* @param string $bundle_new
* The new name of the bundle.
*
* @see entity_invoke_bundle_hook()
* @see entity_crud
* {@inheritdoc}
*/
public function onBundleCreate($entity_type_id, $bundle) {
$this->clearCachedBundles();
// Notify the entity storage.
$storage = $this->getStorage($entity_type_id);
if ($storage instanceof FieldableEntityStorageInterface) {
$storage->onBundleCreate($bundle);
}
// Invoke hook_entity_bundle_create() hook.
$this->moduleHandler->invokeAll('entity_bundle_create', array($entity_type_id, $bundle));
}
/**
* {@inheritdoc}
*/
public function onBundleRename($entity_type_id, $bundle_old, $bundle_new) {
$this->clearCachedBundles();
// Notify the entity storage.
$storage = $this->getStorage($entity_type_id);
if ($storage instanceof FieldableEntityStorageInterface) {
$storage->onBundleRename($bundle_old, $bundle_new);
}
// Rename existing base field bundle overrides.
$overrides = $this->getStorage('base_field_override')->loadByProperties(array('entity_type' => $entity_type_id, 'bundle' => $bundle_old));
foreach ($overrides as $override) {
......@@ -1015,6 +1026,25 @@ public function onBundleRename($entity_type_id, $bundle_old, $bundle_new) {
$override->allowBundleRename();
$override->save();
}
// Invoke hook_entity_bundle_rename() hook.
$this->moduleHandler->invokeAll('entity_bundle_rename', array($entity_type_id, $bundle_old, $bundle_new));
$this->clearCachedFieldDefinitions();
}
/**
* {@inheritdoc}
*/
public function onBundleDelete($entity_type_id, $bundle) {
$this->clearCachedBundles();
// Notify the entity storage.
$storage = $this->getStorage($entity_type_id);
if ($storage instanceof FieldableEntityStorageInterface) {
$storage->onBundleDelete($bundle);
}
// Invoke hook_entity_bundle_delete() hook.
$this->moduleHandler->invokeAll('entity_bundle_delete', array($entity_type_id, $bundle));
$this->clearCachedFieldDefinitions();
}
}
......@@ -397,4 +397,42 @@ 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);
}
......@@ -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_invoke_bundle_hook('delete', $entity_type_id, $bundle);
$entity_manager->onBundleDelete($entity_type_id, $bundle);
}
}
}
......
......@@ -77,24 +77,7 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update && !$this->isSyncing()) {
entity_invoke_bundle_hook('create', 'block_content', $this->id());
if (!$this->isSyncing()) {
block_content_add_body_field($this->id);
}
}
elseif ($this->getOriginalId() != $this->id) {
entity_invoke_bundle_hook('rename', 'block_content', $this->getOriginalId(), $this->id);
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
entity_invoke_bundle_hook('delete', 'block_content', $entity->id());
block_content_add_body_field($this->id);
}
}
......
......@@ -104,7 +104,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);
entity_invoke_bundle_hook('create', $entity_type, $bundle);
\Drupal::entityManager()->onBundleCreate($entity_type, $bundle);
}
/**
......@@ -124,7 +124,7 @@ function entity_test_rename_bundle($bundle_old, $bundle_new, $entity_type = 'ent
unset($bundles[$bundle_old]);
\Drupal::state()->set($entity_type . '.bundles', $bundles);
entity_invoke_bundle_hook('rename', $entity_type, $bundle_old, $bundle_new);
\Drupal::entityManager()->onBundleRename($entity_type, $bundle_old, $bundle_new);
}
/**
......@@ -141,7 +141,7 @@ function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') {
unset($bundles[$bundle]);
\Drupal::state()->set($entity_type . '.bundles', $bundles);
entity_invoke_bundle_hook('delete', $entity_type, $bundle);
\Drupal::entityManager()->onBundleDelete($entity_type, $bundle);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment