From 177b86ec3258dcbfa68563bec98dcec46bc6bb29 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Wed, 14 May 2014 21:20:40 +0100
Subject: [PATCH] Issue #2261369 by Berdir: Introduce a common config entity
 base class for all bundle config entity types.

---
 .../Config/Entity/ConfigEntityBundleBase.php  | 45 +++++++++++++++++++
 .../custom_block/Entity/CustomBlockType.php   | 18 +-------
 .../lib/Drupal/contact/Entity/Category.php    | 28 +-----------
 .../node/lib/Drupal/node/Entity/NodeType.php  | 10 +----
 .../lib/Drupal/taxonomy/Entity/Vocabulary.php | 13 ++----
 5 files changed, 54 insertions(+), 60 deletions(-)
 create mode 100644 core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php

diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
new file mode 100644
index 000000000000..de398da99cc6
--- /dev/null
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
@@ -0,0 +1,45 @@
+<?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());
+    }
+  }
+
+}
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php
index a7674d095c47..65dd97491571 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php
@@ -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());
-    }
   }
 
 }
diff --git a/core/modules/contact/lib/Drupal/contact/Entity/Category.php b/core/modules/contact/lib/Drupal/contact/Entity/Category.php
index 0b35cfe58692..83c296395c07 100644
--- a/core/modules/contact/lib/Drupal/contact/Entity/Category.php
+++ b/core/modules/contact/lib/Drupal/contact/Entity/Category.php
@@ -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());
-    }
-  }
-
 }
diff --git a/core/modules/node/lib/Drupal/node/Entity/NodeType.php b/core/modules/node/lib/Drupal/node/Entity/NodeType.php
index 1c3a83969c62..307e947e9710 100644
--- a/core/modules/node/lib/Drupal/node/Entity/NodeType.php
+++ b/core/modules/node/lib/Drupal/node/Entity/NodeType.php
@@ -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());
-    }
   }
 
   /**
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php
index 00ae49c01ef1..b2b9fa26859f 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Entity/Vocabulary.php
@@ -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));
   }
 
 }
-- 
GitLab