diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 4270a1fb53..8c769cbdf6 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Field\FieldItemList; +use Drupal\Core\TypedData\ComputedItemListTrait; /** * A computed field that provides a content entity's moderation state. @@ -14,6 +15,20 @@ */ class ModerationStateFieldItemList extends FieldItemList { + use ComputedItemListTrait; + + /** + * {@inheritdoc} + */ + protected function computeValue() { + $moderation_state = $this->getModerationStateId(); + // Do not store NULL values in the static cache. + if ($moderation_state) { + // An entity can only have a single moderation state. + $this->list[0] = $this->createItem(0, $moderation_state); + } + } + /** * Gets the moderation state ID linked to a content entity revision. * @@ -83,41 +98,6 @@ protected function loadContentModerationStateRevision(ContentEntityInterface $en return $content_moderation_state; } - /** - * {@inheritdoc} - */ - public function get($index) { - if ($index !== 0) { - throw new \InvalidArgumentException('An entity can not have multiple moderation states at the same time.'); - } - $this->computeModerationFieldItemList(); - return isset($this->list[$index]) ? $this->list[$index] : NULL; - } - - /** - * {@inheritdoc} - */ - public function getIterator() { - $this->computeModerationFieldItemList(); - return parent::getIterator(); - } - - /** - * Recalculate the moderation field item list. - */ - protected function computeModerationFieldItemList() { - // Compute the value of the moderation state. - $index = 0; - if (!isset($this->list[$index]) || $this->list[$index]->isEmpty()) { - - $moderation_state = $this->getModerationStateId(); - // Do not store NULL values in the static cache. - if ($moderation_state) { - $this->list[$index] = $this->createItem($index, $moderation_state); - } - } - } - /** * {@inheritdoc} */ @@ -134,6 +114,7 @@ public function setValue($values, $notify = TRUE) { parent::setValue($values, $notify); if (isset($this->list[0])) { + $this->valueComputed = TRUE; $this->updateModeratedEntity($this->list[0]->value); } } diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index a0d46da428..a1d44ec212 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -79,6 +79,13 @@ public function testArrayIteration() { $this->assertEquals(['draft'], $states); } + /** + * Test getting the moderation_state with getValue. + */ + public function testGetValue() { + $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue()); + } + /** * Tests that moderation state changes also change the related entity state. */