Commit 6b342913 authored by alexpott's avatar alexpott

Issue #2809123 by timmillwood, Sam152: Reverting a revision doesn't keep moderation state

parent 906ff36c
......@@ -2,6 +2,7 @@
namespace Drupal\content_moderation\Plugin\Field;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldItemList;
/**
......@@ -13,12 +14,12 @@
class ModerationStateFieldItemList extends FieldItemList {
/**
* Gets the moderation state entity linked to a content entity revision.
* Gets the moderation state ID linked to a content entity revision.
*
* @return string|null
* The moderation state linked to a content entity revision.
* The moderation state ID linked to a content entity revision.
*/
protected function getModerationState() {
protected function getModerationStateId() {
$entity = $this->getEntity();
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
......@@ -27,43 +28,58 @@ protected function getModerationState() {
return NULL;
}
if ($entity->id() && $entity->getRevisionId()) {
$revisions = \Drupal::service('entity.query')->get('content_moderation_state')
->condition('content_entity_type_id', $entity->getEntityTypeId())
->condition('content_entity_id', $entity->id())
->condition('content_entity_revision_id', $entity->getRevisionId())
->condition('workflow', $moderation_info->getWorkFlowForEntity($entity)->id())
->allRevisions()
->sort('revision_id', 'DESC')
->execute();
if ($revision_to_load = key($revisions)) {
/** @var \Drupal\content_moderation\ContentModerationStateInterface $content_moderation_state */
$content_moderation_state = \Drupal::entityTypeManager()
->getStorage('content_moderation_state')
->loadRevision($revision_to_load);
// Return the correct translation.
if ($entity->getEntityType()->hasKey('langcode')) {
$langcode = $entity->language()->getId();
if (!$content_moderation_state->hasTranslation($langcode)) {
$content_moderation_state->addTranslation($langcode);
}
if ($content_moderation_state->language()->getId() !== $langcode) {
$content_moderation_state = $content_moderation_state->getTranslation($langcode);
}
}
return $content_moderation_state->get('moderation_state')->value;
}
// Existing entities will have a corresponding content_moderation_state
// entity associated with them.
if (!$entity->isNew() && $content_moderation_state = $this->loadContentModerationStateRevision($entity)) {
return $content_moderation_state->moderation_state->value;
}
// It is possible that the bundle does not exist at this point. For example,
// the node type form creates a fake Node entity to get default values.
// @see \Drupal\node\NodeTypeForm::form()
$workflow = $moderation_info->getWorkFlowForEntity($entity);
if ($workflow) {
return $workflow->getInitialState()->id();
return $workflow ? $workflow->getInitialState()->id() : NULL;
}
/**
* Load the content moderation state revision associated with an entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity the content moderation state entity will be loaded from.
*
* @return \Drupal\content_moderation\ContentModerationStateInterface|null
* The content_moderation_state revision or FALSE if none exists.
*/
protected function loadContentModerationStateRevision(ContentEntityInterface $entity) {
$moderation_info = \Drupal::service('content_moderation.moderation_information');
$content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
$revisions = \Drupal::service('entity.query')->get('content_moderation_state')
->condition('content_entity_type_id', $entity->getEntityTypeId())
->condition('content_entity_id', $entity->id())
// Ensure the correct revision is loaded in scenarios where a revision is
// being reverted.
->condition('content_entity_revision_id', $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId())
->condition('workflow', $moderation_info->getWorkFlowForEntity($entity)->id())
->allRevisions()
->sort('revision_id', 'DESC')
->execute();
if (empty($revisions)) {
return NULL;
}
/** @var \Drupal\content_moderation\ContentModerationStateInterface $content_moderation_state */
$content_moderation_state = $content_moderation_storage->loadRevision(key($revisions));
if ($entity->getEntityType()->hasKey('langcode')) {
$langcode = $entity->language()->getId();
if (!$content_moderation_state->hasTranslation($langcode)) {
$content_moderation_state->addTranslation($langcode);
}
if ($content_moderation_state->language()->getId() !== $langcode) {
$content_moderation_state = $content_moderation_state->getTranslation($langcode);
}
}
return $content_moderation_state;
}
/**
......@@ -93,7 +109,7 @@ protected function computeModerationFieldItemList() {
$index = 0;
if (!isset($this->list[$index]) || $this->list[$index]->isEmpty()) {
$moderation_state = $this->getModerationState();
$moderation_state = $this->getModerationStateId();
// Do not store NULL values in the static cache.
if ($moderation_state) {
$this->list[$index] = $this->createItem($index, $moderation_state);
......
......@@ -101,6 +101,20 @@ public function testBasicModeration() {
$this->assertTrue($node->isPublished());
$this->assertEquals(4, $node->getRevisionId());
// Update the node to archived which will then be the default revision.
$node->moderation_state->value = 'archived';
$node->save();
// Revert to the previous (published) revision.
$previous_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision(4);
$previous_revision->isDefaultRevision(TRUE);
$previous_revision->setNewRevision(TRUE);
$previous_revision->save();
// Get the default revision.
$node = $this->reloadNode($node);
$this->assertEquals('published', $node->moderation_state->value);
$this->assertTrue($node->isPublished());
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment