diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index e2955b0..2f07e1b 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -122,19 +122,44 @@ protected function computeModerationFieldItemList() { * {@inheritdoc} */ public function onChange($delta) { + $this->updateModeratedEntity($this->list[$delta]->value); + + parent::onChange($delta); + } + + /** + * {@inheritdoc} + */ + public function setValue($values, $notify = TRUE) { + // Support passing in only the value of the first item, either as a literal + // (value of the first property) or as an array of properties. + if (isset($values) && (!is_array($values) || (!empty($values) && !is_numeric(current(array_keys($values)))))) { + $values = [0 => $values]; + } + + $this->updateModeratedEntity($values[0]['value']); + + parent::setValue($values, $notify); + } + + /** + * Updates the default revision flag and the publishing status of the entity. + * + * @param string $moderation_state_id + * The ID of the new moderation state. + */ + protected function updateModeratedEntity($moderation_state_id) { $entity = $this->getEntity(); /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */ $content_moderation_info = \Drupal::service('content_moderation.moderation_information'); $workflow = $content_moderation_info->getWorkflowForEntity($entity); - $current_state_id = $this->list[0]->value; - // Change the entity's default revision flag and the publishing status only // if the new workflow state is a valid one. - if ($workflow->hasState($current_state_id)) { + if ($workflow->hasState($moderation_state_id)) { /** @var \Drupal\content_moderation\ContentModerationState $current_state */ - $current_state = $workflow->getState($current_state_id); + $current_state = $workflow->getState($moderation_state_id); // This entity is default if it is new, a new translation, the default // revision state, or the default revision is not published. @@ -151,8 +176,6 @@ public function onChange($delta) { $published_state ? $entity->setPublished() : $entity->setUnpublished(); } } - - parent::onChange($delta); } }