Commit 82f16686 authored by alexpott's avatar alexpott

Issue #2799785 by amateescu, timmillwood, dawehner, alexpott: Entity types...

Issue #2799785 by amateescu, timmillwood, dawehner, alexpott: Entity types with non-config bundles can not be moderated
parent 3900e343
......@@ -123,9 +123,16 @@ public static function create(ContainerInterface $container) {
* @see hook_entity_type_alter()
*/
public function entityTypeAlter(array &$entity_types) {
foreach ($this->filterNonRevisionableEntityTypes($entity_types) as $type_name => $type) {
$entity_types[$type_name] = $this->addModerationToEntityType($type);
$entity_types[$type->get('bundle_of')] = $this->addModerationToEntity($entity_types[$type->get('bundle_of')]);
foreach ($entity_types as $entity_type_id => $entity_type) {
// The ContentModerationState entity type should never be moderated.
if ($entity_type->isRevisionable() && $entity_type_id != 'content_moderation_state') {
$entity_types[$entity_type_id] = $this->addModerationToEntityType($entity_type);
// Add additional moderation support to entity types whose bundles are
// managed by a config entity type.
if ($entity_type->getBundleEntityType()) {
$entity_types[$entity_type->getBundleEntityType()] = $this->addModerationToBundleEntityType($entity_types[$entity_type->getBundleEntityType()]);
}
}
}
}
......@@ -141,7 +148,7 @@ public function entityTypeAlter(array &$entity_types) {
* @return \Drupal\Core\Entity\ContentEntityTypeInterface
* The modified content entity definition.
*/
protected function addModerationToEntity(ContentEntityTypeInterface $type) {
protected function addModerationToEntityType(ContentEntityTypeInterface $type) {
if (!$type->hasHandlerClass('moderation')) {
$handler_class = !empty($this->moderationHandlers[$type->id()]) ? $this->moderationHandlers[$type->id()] : ModerationHandler::class;
$type->setHandlerClass('moderation', $handler_class);
......@@ -175,7 +182,7 @@ protected function addModerationToEntity(ContentEntityTypeInterface $type) {
* @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
* The modified config entity definition.
*/
protected function addModerationToEntityType(ConfigEntityTypeInterface $type) {
protected function addModerationToBundleEntityType(ConfigEntityTypeInterface $type) {
if ($type->hasLinkTemplate('edit-form') && !$type->hasLinkTemplate('moderation-form')) {
$type->setLinkTemplate('moderation-form', $type->getLinkTemplate('edit-form') . '/moderation');
}
......@@ -398,21 +405,4 @@ public static function bundleFormRedirect(array &$form, FormStateInterface $form
}
}
/**
* Filters entity type lists to return only revisionable entity types.
*
* @param EntityTypeInterface[] $entity_types
* The master entity type list filter.
*
* @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface[]
* An array of revisionable entity types which are configuration entities.
*/
protected function filterNonRevisionableEntityTypes(array $entity_types) {
return array_filter($entity_types, function (EntityTypeInterface $type) use ($entity_types) {
return ($type instanceof ConfigEntityTypeInterface)
&& ($bundle_of = $type->get('bundle_of'))
&& $entity_types[$bundle_of]->isRevisionable();
});
}
}
......@@ -4,6 +4,7 @@
use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\entity_test\Entity\EntityTestWithBundle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
......@@ -44,6 +45,7 @@ protected function setUp() {
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test_with_bundle');
$this->installEntitySchema('entity_test_rev');
$this->installEntitySchema('content_moderation_state');
$this->installConfig('content_moderation');
}
......@@ -314,6 +316,32 @@ public function testNonLangcodeEntityTypeModeration() {
$this->assertEquals('published', EntityTestWithBundle::load($entity_test_with_bundle->id())->moderation_state->value);
}
/**
* Tests that entity types without config bundles can be moderated.
*/
public function testNonBundleConfigEntityTypeModeration() {
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
$workflow->save();
// Check that the tested entity type does not have bundles managed by a
// config entity type.
$entity_type = \Drupal::entityTypeManager()->getDefinition('entity_test_rev');
$this->assertNull($entity_type->getBundleEntityType(), 'The test entity type does not have config bundles.');
// Create a test entity.
$entity_test = EntityTestRev::create([
'type' => 'entity_test_rev'
]);
$entity_test->save();
$this->assertEquals('draft', $entity_test->moderation_state->value);
$entity_test->moderation_state->value = 'published';
$entity_test->save();
$this->assertEquals('published', EntityTestRev::load($entity_test->id())->moderation_state->value);
}
/**
* Reloads the node after clearing the static cache.
*
......
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