diff --git a/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php b/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php index b3a9f96..69410b1 100644 --- a/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php +++ b/core/modules/content_moderation/src/Entity/Handler/ModerationHandler.php @@ -31,16 +31,36 @@ public static function createInstance(ContainerInterface $container, EntityTypeI * {@inheritdoc} */ public function onPresave(ContentEntityInterface $entity, $default_revision, $published_state) { - // This is probably not necessary if configuration is setup correctly. - $entity->setNewRevision(TRUE); - $entity->isDefaultRevision($default_revision); + if ($this->shouldModerate($entity, $published_state)) { + // This is probably not necessary if configuration is setup correctly. + $entity->setNewRevision(TRUE); + $entity->isDefaultRevision($default_revision); - if ($entity instanceof EntityPublishedInterface) { - $entity->setPublished($published_state); + if ($entity instanceof EntityPublishedInterface) { + $entity->{$published_state ? 'setPublished' : 'setUnpublished'}(); + } } } /** + * Check if an entity's default revision and/or state needs adjusting. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity to check. + * @param bool $published_state + * Whether the state being transitioned to is a published state or not. + * + * @return bool + * TRUE when either the default revision or the state needs to be updated. + */ + protected function shouldModerate(ContentEntityInterface $entity, $published_state) { + // @todo clarify the first condition, https://www.drupal.org/node/2834953. + // First condition is needed so you can add a translation. + // Second condition checks to see if the published status has changed. + return $entity->isDefaultTranslation() || (!($entity instanceof EntityPublishedInterface) || $entity->isPublished() !== $published_state); + } + + /** * {@inheritdoc} */ public function onBundleModerationConfigurationFormSubmit(ConfigEntityInterface $bundle) { diff --git a/core/modules/content_moderation/src/Entity/Handler/NodeModerationHandler.php b/core/modules/content_moderation/src/Entity/Handler/NodeModerationHandler.php index e569bc2..1bef28f 100644 --- a/core/modules/content_moderation/src/Entity/Handler/NodeModerationHandler.php +++ b/core/modules/content_moderation/src/Entity/Handler/NodeModerationHandler.php @@ -13,15 +13,6 @@ class NodeModerationHandler extends ModerationHandler { /** * {@inheritdoc} */ - public function onPresave(ContentEntityInterface $entity, $default_revision, $published_state) { - if ($this->shouldModerate($entity, $published_state)) { - parent::onPresave($entity, $default_revision, $published_state); - } - } - - /** - * {@inheritdoc} - */ public function enforceRevisionsEntityFormAlter(array &$form, FormStateInterface $form_state, $form_id) { $form['revision']['#disabled'] = TRUE; $form['revision']['#default_value'] = TRUE; @@ -42,22 +33,4 @@ public function enforceRevisionsBundleFormAlter(array &$form, FormStateInterface } } - /** - * Check if an entity's default revision and/or state needs adjusting. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity to check. - * @param bool $published_state - * Whether the state being transitioned to is a published state or not. - * - * @return bool - * TRUE when either the default revision or the state needs to be updated. - */ - protected function shouldModerate(ContentEntityInterface $entity, $published_state) { - // @todo clarify the first condition. - // First condition is needed so you can add a translation. - // Second condition checks to see if the published status has changed. - return $entity->isDefaultTranslation() || $entity->isPublished() !== $published_state; - } - }