Commit 177b86ec authored by catch's avatar catch

Issue #2261369 by Berdir: Introduce a common config entity base class for all...

Issue #2261369 by Berdir: Introduce a common config entity base class for all bundle config entity types.
parent 120a1da3
<?php
/**
* @file
* Contains Drupal\Core\Config\Entity\ConfigEntityBundleBase.
*/
namespace Drupal\Core\Config\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
/**
* A base class for config entity types that act as bundles.
*
* Entity types that want to use this base class must use bundle_of in their
* annotation to specify for which entity type they are providing bundles for.
*/
abstract class ConfigEntityBundleBase extends ConfigEntityBase {
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
entity_invoke_bundle_hook('create', $this->getEntityType()->getBundleOf(), $this->id());
}
elseif ($this->getOriginalId() != $this->id()) {
entity_invoke_bundle_hook('rename', $this->getEntityType()->getBundleOf(), $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', $entity->getEntityType()->getBundleOf(), $entity->id());
}
}
}
......@@ -8,6 +8,7 @@
namespace Drupal\custom_block\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\custom_block\CustomBlockTypeInterface;
......@@ -39,7 +40,7 @@
* }
* )
*/
class CustomBlockType extends ConfigEntityBase implements CustomBlockTypeInterface {
class CustomBlockType extends ConfigEntityBundleBase implements CustomBlockTypeInterface {
/**
* The custom block type ID.
......@@ -76,25 +77,10 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update && !$this->isSyncing()) {
entity_invoke_bundle_hook('create', 'custom_block', $this->id());
if (!$this->isSyncing()) {
custom_block_add_body_field($this->id);
}
}
elseif ($this->getOriginalId() != $this->id) {
entity_invoke_bundle_hook('rename', 'custom_block', $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', 'custom_block', $entity->id());
}
}
}
......@@ -8,6 +8,7 @@
namespace Drupal\contact\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\contact\CategoryInterface;
......@@ -39,7 +40,7 @@
* }
* )
*/
class Category extends ConfigEntityBase implements CategoryInterface {
class Category extends ConfigEntityBundleBase implements CategoryInterface {
/**
* The category ID.
......@@ -76,29 +77,4 @@ class Category extends ConfigEntityBase implements CategoryInterface {
*/
public $weight = 0;
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
entity_invoke_bundle_hook('create', 'contact_message', $this->id());
}
elseif ($this->original->id() != $this->id()) {
entity_invoke_bundle_hook('rename', 'contact_message', $this->original->id(), $this->id());
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
entity_invoke_bundle_hook('delete', 'contact_message', $entity->id());
}
}
}
......@@ -8,7 +8,7 @@
namespace Drupal\node\Entity;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\node\NodeTypeInterface;
......@@ -41,7 +41,7 @@
* }
* )
*/
class NodeType extends ConfigEntityBase implements NodeTypeInterface {
class NodeType extends ConfigEntityBundleBase implements NodeTypeInterface {
/**
* The machine name of this node type.
......@@ -156,8 +156,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
entity_invoke_bundle_hook('create', 'node', $this->id());
// Create a body if the create_body property is true and we're not in
// the syncing process.
if ($this->get('create_body') && !$this->isSyncing()) {
......@@ -176,7 +174,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
'%type' => $this->id(),
)));
}
entity_invoke_bundle_hook('rename', 'node', $this->getOriginalId(), $this->id());
}
}
......@@ -188,9 +185,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
// Clear the node type cache to reflect the removal.
$storage->resetCache(array_keys($entities));
foreach ($entities as $entity) {
entity_invoke_bundle_hook('delete', 'node', $entity->id());
}
}
/**
......
......@@ -7,7 +7,7 @@
namespace Drupal\taxonomy\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\field\Field;
use Drupal\taxonomy\VocabularyInterface;
......@@ -44,7 +44,7 @@
* }
* )
*/
class Vocabulary extends ConfigEntityBase implements VocabularyInterface {
class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
/**
* The taxonomy vocabulary ID.
......@@ -99,10 +99,7 @@ public function id() {
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
entity_invoke_bundle_hook('create', 'taxonomy_term', $this->id());
}
elseif ($this->getOriginalId() != $this->id() && !$this->isSyncing()) {
if ($update && $this->getOriginalId() != $this->id() && !$this->isSyncing()) {
// Reflect machine name changes in the definitions of existing 'taxonomy'
// fields.
$field_ids = array();
......@@ -131,8 +128,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
$field->save();
}
}
// Update bundles.
entity_invoke_bundle_hook('rename', 'taxonomy_term', $this->getOriginalId(), $this->id());
}
$storage->resetCache($update ? array($this->getOriginalId()) : array());
}
......@@ -187,8 +182,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
}
}
}
// Reset caches.
$storage->resetCache(array_keys($vocabularies));
}
}
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