diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 6ef0cd29e9..4c72e9a298 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -25,7 +25,9 @@ class ModerationStateFieldItemList extends FieldItemList { */ protected function computeValue() { $moderation_state = $this->getModerationStateId(); - // Do not store NULL values in the static cache. + // Do not store NULL values, in the case where an entity does not have a + // moderation workflow associated with it, we do not create list items for + // the computed field. if ($moderation_state) { // An entity can only have a single moderation state. $this->list[0] = $this->createItem(0, $moderation_state); @@ -38,7 +40,7 @@ protected function computeValue() { protected function ensureComputedValue() { // If the moderation state field is set to an empty value, always recompute // the state. Empty is not a valid moderation state value, when none is - // preset the default state is used. + // present the default state is used. if (!isset($this->list[0]) || $this->list[0]->isEmpty()) { $this->valueComputed = FALSE; } diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index 25baeba17c..7d95054f0c 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -43,6 +43,10 @@ protected function setUp() { $this->installEntitySchema('content_moderation_state'); $this->installConfig('content_moderation'); + NodeType::create([ + 'type' => 'unmoderated', + ])->save(); + $node_type = NodeType::create([ 'type' => 'example', ]); @@ -109,6 +113,21 @@ public function testSetEmptyState() { $this->assertEquals('draft', $this->testNode->moderation_state->value); } + /** + * Test the list class with a non moderated entity. + */ + public function testNonModeratedEntity() { + $unmoderated_node = Node::create([ + 'type' => 'unmoderated', + 'title' => 'Test title', + ]); + $unmoderated_node->save(); + $this->assertEquals(0, $unmoderated_node->moderation_state->count()); + + $unmoderated_node->moderation_state = NULL; + $this->assertEquals(0, $unmoderated_node->moderation_state->count()); + } + /** * Tests that moderation state changes also change the related entity state. */