Skip to content
Snippets Groups Projects
Commit 1029c23e authored by catch's avatar catch
Browse files

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

parent 9c59104c
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
use Drupal\content_moderation\Entity\ContentModerationState; use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
...@@ -77,32 +76,6 @@ public static function create(ContainerInterface $container) { ...@@ -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. * Acts on an entity and set published status based on the moderation state.
* *
...@@ -170,8 +143,8 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) { ...@@ -170,8 +143,8 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
$moderation_state = $entity->moderation_state->target_id; $moderation_state = $entity->moderation_state->target_id;
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if (!$moderation_state) { if (!$moderation_state) {
$moderation_state = $this->moderationInfo $moderation_state = $this->entityTypeManager
->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle()) ->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle())
->getThirdPartySetting('content_moderation', 'default_moderation_state'); ->getThirdPartySetting('content_moderation', 'default_moderation_state');
} }
......
...@@ -49,21 +49,15 @@ public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type ...@@ -49,21 +49,15 @@ public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type
return $entity_type->hasHandlerClass('moderation'); 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} * {@inheritdoc}
*/ */
public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) { public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle) {
if ($bundle_entity = $this->loadBundleEntity($entity_type->getBundleEntityType(), $bundle)) { if ($this->canModerateEntitiesOfEntityType($entity_type)) {
return $bundle_entity->getThirdPartySetting('content_moderation', 'enabled', FALSE); $bundle_entity = $this->entityTypeManager->getStorage($entity_type->getBundleEntityType())->load($bundle);
if ($bundle_entity) {
return $bundle_entity->getThirdPartySetting('content_moderation', 'enabled', FALSE);
}
} }
return FALSE; return FALSE;
} }
......
...@@ -11,19 +11,6 @@ ...@@ -11,19 +11,6 @@
*/ */
interface ModerationInformationInterface { 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. * Determines if an entity is moderated.
* *
......
...@@ -57,8 +57,9 @@ protected function getModerationState() { ...@@ -57,8 +57,9 @@ protected function getModerationState() {
// It is possible that the bundle does not exist at this point. For example, // 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. // the node type form creates a fake Node entity to get default values.
// @see \Drupal\node\NodeTypeForm::form() // @see \Drupal\node\NodeTypeForm::form()
$bundle_entity = \Drupal::service('content_moderation.moderation_information') $bundle_entity = \Drupal::entityTypeManager()
->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle()); ->getStorage($entity->getEntityType()->getBundleEntityType())
->load($entity->bundle());
if ($bundle_entity && ($default = $bundle_entity->getThirdPartySetting('content_moderation', 'default_moderation_state'))) { if ($bundle_entity && ($default = $bundle_entity->getThirdPartySetting('content_moderation', 'default_moderation_state'))) {
return ModerationState::load($default); return ModerationState::load($default);
} }
......
...@@ -97,8 +97,8 @@ public function validate($value, Constraint $constraint) { ...@@ -97,8 +97,8 @@ public function validate($value, Constraint $constraint) {
$new_state_id = $entity->moderation_state->target_id; $new_state_id = $entity->moderation_state->target_id;
} }
else { else {
$new_state_id = $default = $this->moderationInformation $new_state_id = $default = $this->entityTypeManager
->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle()) ->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle())
->getThirdPartySetting('content_moderation', 'default_moderation_state'); ->getThirdPartySetting('content_moderation', 'default_moderation_state');
} }
if ($new_state_id) { if ($new_state_id) {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\content_moderation\Unit; namespace Drupal\Tests\content_moderation\Unit;
use Drupal\content_moderation\Entity\Handler\ModerationHandler;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\ContentEntityType;
...@@ -70,6 +71,7 @@ public function testIsModeratedEntity($status) { ...@@ -70,6 +71,7 @@ public function testIsModeratedEntity($status) {
$entity_type = new ContentEntityType([ $entity_type = new ContentEntityType([
'id' => 'test_entity_type', 'id' => 'test_entity_type',
'bundle_entity_type' => 'entity_test_bundle', 'bundle_entity_type' => 'entity_test_bundle',
'handlers' => ['moderation' => ModerationHandler::class],
]); ]);
$entity = $this->prophesize(ContentEntityInterface::class); $entity = $this->prophesize(ContentEntityInterface::class);
$entity->getEntityType()->willReturn($entity_type); $entity->getEntityType()->willReturn($entity_type);
...@@ -104,6 +106,7 @@ public function testShouldModerateEntities($status) { ...@@ -104,6 +106,7 @@ public function testShouldModerateEntities($status) {
$entity_type = new ContentEntityType([ $entity_type = new ContentEntityType([
'id' => 'test_entity_type', 'id' => 'test_entity_type',
'bundle_entity_type' => 'entity_test_bundle', 'bundle_entity_type' => 'entity_test_bundle',
'handlers' => ['moderation' => ModerationHandler::class],
]); ]);
$moderation_information = new ModerationInformation($this->setupModerationEntityManager($status), $this->getUser()); $moderation_information = new ModerationInformation($this->setupModerationEntityManager($status), $this->getUser());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment