From 1029c23ef12b35e531c24ae805536e2a2dfd6328 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Fri, 14 Oct 2016 17:37:36 +0100
Subject: [PATCH] Issue #2784357 by alexpott: Remove
 ModerationInformationInterface::loadBundleEntity()

---
 .../src/EntityOperations.php                  | 31 ++-----------------
 .../src/ModerationInformation.php             | 16 +++-------
 .../src/ModerationInformationInterface.php    | 13 --------
 .../Field/ModerationStateFieldItemList.php    |  5 +--
 .../ModerationStateConstraintValidator.php    |  4 +--
 .../src/Unit/ModerationInformationTest.php    |  3 ++
 6 files changed, 15 insertions(+), 57 deletions(-)

diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php
index 3685b0dea8dc..51ad7d685d48 100644
--- a/core/modules/content_moderation/src/EntityOperations.php
+++ b/core/modules/content_moderation/src/EntityOperations.php
@@ -4,7 +4,6 @@
 
 use Drupal\content_moderation\Entity\ContentModerationState;
 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
-use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -77,32 +76,6 @@ public static function create(ContainerInterface $container) {
     );
   }
 
-  /**
-   * Determines the default moderation state on load for an entity.
-   *
-   * This method is only applicable when an entity is loaded that has
-   * no moderation state on it, but should. In those cases, failing to set
-   * one may result in NULL references elsewhere when other code tries to check
-   * the moderation state of the entity.
-   *
-   * The amount of indirection here makes performance a concern, but
-   * given how Entity API works I don't know how else to do it.
-   * This reliably gets us *A* valid state. However, that state may be
-   * not the ideal one. Suggestions on how to better select the default
-   * state here are welcome.
-   *
-   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
-   *   The entity for which we want a default state.
-   *
-   * @return string
-   *   The default state for the given entity.
-   */
-  protected function getDefaultLoadStateId(ContentEntityInterface $entity) {
-    return $this->moderationInfo
-      ->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())
-      ->getThirdPartySetting('content_moderation', 'default_moderation_state');
-  }
-
   /**
    * Acts on an entity and set published status based on the moderation state.
    *
@@ -170,8 +143,8 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
     $moderation_state = $entity->moderation_state->target_id;
     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
     if (!$moderation_state) {
-      $moderation_state = $this->moderationInfo
-        ->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())
+      $moderation_state = $this->entityTypeManager
+        ->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle())
         ->getThirdPartySetting('content_moderation', 'default_moderation_state');
     }
 
diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php
index 0f5b406ccdce..ed33902a5253 100644
--- a/core/modules/content_moderation/src/ModerationInformation.php
+++ b/core/modules/content_moderation/src/ModerationInformation.php
@@ -49,21 +49,15 @@ public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type
     return $entity_type->hasHandlerClass('moderation');
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function loadBundleEntity($bundle_entity_type_id, $bundle_id) {
-    if ($bundle_entity_type_id) {
-      return $this->entityTypeManager->getStorage($bundle_entity_type_id)->load($bundle_id);
-    }
-  }
-
   /**
    * {@inheritdoc}
    */
   public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
-    if ($bundle_entity = $this->loadBundleEntity($entity_type->getBundleEntityType(), $bundle)) {
-      return $bundle_entity->getThirdPartySetting('content_moderation', 'enabled', FALSE);
+    if ($this->canModerateEntitiesOfEntityType($entity_type)) {
+      $bundle_entity = $this->entityTypeManager->getStorage($entity_type->getBundleEntityType())->load($bundle);
+      if ($bundle_entity) {
+        return $bundle_entity->getThirdPartySetting('content_moderation', 'enabled', FALSE);
+      }
     }
     return FALSE;
   }
diff --git a/core/modules/content_moderation/src/ModerationInformationInterface.php b/core/modules/content_moderation/src/ModerationInformationInterface.php
index e7fa15d2fa14..95a658e65d6c 100644
--- a/core/modules/content_moderation/src/ModerationInformationInterface.php
+++ b/core/modules/content_moderation/src/ModerationInformationInterface.php
@@ -11,19 +11,6 @@
  */
 interface ModerationInformationInterface {
 
-  /**
-   * Loads a specific bundle entity.
-   *
-   * @param string $bundle_entity_type_id
-   *   The bundle entity type ID.
-   * @param string $bundle_id
-   *   The bundle ID.
-   *
-   * @return \Drupal\Core\Config\Entity\ConfigEntityInterface|null
-   *   The bundle entity.
-   */
-  public function loadBundleEntity($bundle_entity_type_id, $bundle_id);
-
   /**
    * Determines if an entity is moderated.
    *
diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
index 74e5ef0c7c71..80820b4b4ebf 100644
--- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
+++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
@@ -57,8 +57,9 @@ protected function getModerationState() {
     // It is possible that the bundle does not exist at this point. For example,
     // the node type form creates a fake Node entity to get default values.
     // @see \Drupal\node\NodeTypeForm::form()
-    $bundle_entity = \Drupal::service('content_moderation.moderation_information')
-      ->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle());
+    $bundle_entity = \Drupal::entityTypeManager()
+      ->getStorage($entity->getEntityType()->getBundleEntityType())
+      ->load($entity->bundle());
     if ($bundle_entity && ($default = $bundle_entity->getThirdPartySetting('content_moderation', 'default_moderation_state'))) {
       return ModerationState::load($default);
     }
diff --git a/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php b/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php
index f70537c6f6bd..ca75604a8547 100644
--- a/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php
+++ b/core/modules/content_moderation/src/Plugin/Validation/Constraint/ModerationStateConstraintValidator.php
@@ -97,8 +97,8 @@ public function validate($value, Constraint $constraint) {
       $new_state_id = $entity->moderation_state->target_id;
     }
     else {
-      $new_state_id = $default = $this->moderationInformation
-        ->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())
+      $new_state_id = $default = $this->entityTypeManager
+        ->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle())
         ->getThirdPartySetting('content_moderation', 'default_moderation_state');
     }
     if ($new_state_id) {
diff --git a/core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php b/core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
index 2b2e6f2320b4..ff46e41585be 100644
--- a/core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
+++ b/core/modules/content_moderation/tests/src/Unit/ModerationInformationTest.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\Tests\content_moderation\Unit;
 
+use Drupal\content_moderation\Entity\Handler\ModerationHandler;
 use Drupal\Core\Config\Entity\ConfigEntityInterface;
 use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\ContentEntityType;
@@ -70,6 +71,7 @@ public function testIsModeratedEntity($status) {
     $entity_type = new ContentEntityType([
       'id' => 'test_entity_type',
       'bundle_entity_type' => 'entity_test_bundle',
+      'handlers' => ['moderation' => ModerationHandler::class],
     ]);
     $entity = $this->prophesize(ContentEntityInterface::class);
     $entity->getEntityType()->willReturn($entity_type);
@@ -104,6 +106,7 @@ public function testShouldModerateEntities($status) {
     $entity_type = new ContentEntityType([
       'id' => 'test_entity_type',
       'bundle_entity_type' => 'entity_test_bundle',
+      'handlers' => ['moderation' => ModerationHandler::class],
     ]);
 
     $moderation_information = new ModerationInformation($this->setupModerationEntityManager($status), $this->getUser());
-- 
GitLab