diff --git a/core/modules/content_moderation/src/ModerationInformation.php b/core/modules/content_moderation/src/ModerationInformation.php index c20be52..1294bc1 100644 --- a/core/modules/content_moderation/src/ModerationInformation.php +++ b/core/modules/content_moderation/src/ModerationInformation.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\TypedData\TranslatableInterface; /** * General service for moderation-related questions about Entity API. @@ -140,6 +141,29 @@ public function isLiveRevision(ContentEntityInterface $entity) { /** * {@inheritdoc} */ + public function isDefaultRevisionPublished(ContentEntityInterface $entity) { + $workflow = $this->getWorkflowForEntity($entity); + $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id()); + + // Ensure we are checking all translations of the default revision. + if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) { + // Loop through each language that has a translation. + foreach ($default_revision->getTranslationLanguages() as $language) { + // Load the translated revision. + $language_revision = $default_revision->getTranslation($language->getId()); + // Return TRUE if a translation with a published state is found. + if ($workflow->getState($language_revision->moderation_state->value)->isPublishedState()) { + return TRUE; + } + } + } + + return $workflow->getState($default_revision->moderation_state->value)->isPublishedState(); + } + + /** + * {@inheritdoc} + */ public function getWorkflowForEntity(ContentEntityInterface $entity) { $bundles = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId()); if (isset($bundles[$entity->bundle()]['workflow'])) { diff --git a/core/modules/content_moderation/src/ModerationInformationInterface.php b/core/modules/content_moderation/src/ModerationInformationInterface.php index 862987f..cf3adce 100644 --- a/core/modules/content_moderation/src/ModerationInformationInterface.php +++ b/core/modules/content_moderation/src/ModerationInformationInterface.php @@ -127,6 +127,21 @@ public function hasForwardRevision(ContentEntityInterface $entity); public function isLiveRevision(ContentEntityInterface $entity); /** + * Determines if the default revision for the given entity is published. + * + * The default revision is the same as the entity retrieved by "default" from + * the storage handler. If the entity is translated, check if any of the + * translations are published. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity being saved. + * + * @return bool + * TRUE if the default revision is published. FALSE otherwise. + */ + public function isDefaultRevisionPublished(ContentEntityInterface $entity); + + /** * Gets the workflow for the given content entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity diff --git a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php index f98cbd9..0a9e1bb 100644 --- a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php +++ b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php @@ -162,27 +162,6 @@ public static function updateStatus($entity_type_id, ContentEntityInterface $ent $element = $form_state->getTriggeringElement(); if (isset($element['#moderation_state'])) { $entity->moderation_state->value = $element['#moderation_state']; - - $workflow = \Drupal::service('content_moderation.moderation_information')->getWorkflowForEntity($entity); - /** @var \Drupal\content_moderation\ContentModerationState $current_state */ - $current_state = $workflow->getState($entity->moderation_state->value); - - // This entity is default if it is new, a new translation, the default - // revision state, or the default revision is not published. - $update_default_revision = $entity->isNew() - || $entity->isNewTranslation() - || $current_state->isDefaultRevisionState() - || !static::isDefaultRevisionPublished($entity, $workflow); - - // This is probably not necessary if configuration is setup correctly. - $entity->setNewRevision(TRUE); - $entity->isDefaultRevision($update_default_revision); - - // Update publishing status if it can be updated and if it needs updating. - $published_state = $current_state->isPublishedState(); - if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) { - $published_state ? $entity->setPublished() : $entity->setUnpublished(); - } } } @@ -237,38 +216,4 @@ public static function isApplicable(FieldDefinitionInterface $field_definition) return $field_definition->getName() === 'moderation_state'; } - /** - * Check if the default revision for the given entity is published. - * - * The default revision is the same as the entity retrieved by "default" from - * the storage handler. If the entity is translated, check if any of the - * translations are published. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being saved. - * @param \Drupal\workflows\WorkflowInterface $workflow - * The workflow being applied to the entity. - * - * @return bool - * TRUE if the default revision is published. FALSE otherwise. - */ - public static function isDefaultRevisionPublished(EntityInterface $entity, WorkflowInterface $workflow) { - $default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id()); - - // Ensure we are checking all translations of the default revision. - if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) { - // Loop through each language that has a translation. - foreach ($default_revision->getTranslationLanguages() as $language) { - // Load the translated revision. - $language_revision = $default_revision->getTranslation($language->getId()); - // Return TRUE if a translation with a published state is found. - if ($workflow->getState($language_revision->moderation_state->value)->isPublishedState()) { - return TRUE; - } - } - } - - return $workflow->getState($default_revision->moderation_state->value)->isPublishedState(); - } - } diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 2d93f5a..7bcc57a 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -3,6 +3,7 @@ namespace Drupal\content_moderation\Plugin\Field; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityPublishedInterface; use Drupal\Core\Field\FieldItemList; /** @@ -117,4 +118,37 @@ protected function computeModerationFieldItemList() { } } + /** + * {@inheritdoc} + */ + public function onChange($delta) { + $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); + + /** @var \Drupal\content_moderation\ContentModerationState $current_state */ + $current_state = $workflow->getState($entity->moderation_state->value); + + // This entity is default if it is new, a new translation, the default + // revision state, or the default revision is not published. + $update_default_revision = $entity->isNew() + || $entity->isNewTranslation() + || $current_state->isDefaultRevisionState() + || !$content_moderation_info->isDefaultRevisionPublished($entity, $workflow); + + // This is probably not necessary if configuration is setup correctly. + $entity->setNewRevision(TRUE); + $entity->isDefaultRevision($update_default_revision); + + // Update publishing status if it can be updated and if it needs updating. + $published_state = $current_state->isPublishedState(); + if (($entity instanceof EntityPublishedInterface) && $entity->isPublished() !== $published_state) { + $published_state ? $entity->setPublished() : $entity->setUnpublished(); + } + + parent::onChange($delta); + } + }