diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 8ac1736..86354b4 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. * @@ -86,49 +101,6 @@ protected function loadContentModerationStateRevision(ContentEntityInterface $en /** * {@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(); - } - - /** - * {@inheritdoc} - */ - public function getValue($include_computed = FALSE) { - $this->computeModerationFieldItemList(); - return parent::getValue($include_computed); - } - - /** - * 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} - */ public function onChange($delta) { $this->updateModeratedEntity($this->list[$delta]->value);