Commit 1029c23e authored by catch's avatar catch

Issue #2784357 by alexpott: Remove ModerationInformationInterface::loadBundleEntity()

parent 9c59104c
......@@ -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');
}
......
......@@ -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;
}
......
......@@ -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.
*
......
......@@ -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);
}
......
......@@ -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) {
......
......@@ -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());
......
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