Skip to content

Commit

Permalink
HPC-9466: Fix an issue with duplicated trash revisions for content
Browse files Browse the repository at this point in the history
  • Loading branch information
berliner committed May 14, 2024
1 parent 0820aae commit 29f9fe2
Show file tree
Hide file tree
Showing 24 changed files with 408 additions and 121 deletions.
6 changes: 3 additions & 3 deletions .docksal/commands/post-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ while [ "$1" != "" ]; do
shift
done

# First make sure that the code base is up to date.
composer install

if $IMPORT_BACKUP; then
BACKUP_DIRECTORY="$PROJECT_ROOT/.docksal/backups"
echo "Looking for backups in ${BACKUP_DIRECTORY}"
Expand Down Expand Up @@ -68,6 +65,9 @@ if $IMPORT_BACKUP; then
fi
fi

# Make sure that the code base is up to date.
composer install

echo "Set maintenance mode to on"
drush sset system.maintenance_mode 1 -y

Expand Down
4 changes: 2 additions & 2 deletions html/modules/custom/ncms_publisher/ncms_publisher.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\ncms_ui\Entity\Content\ContentBase;
use Drupal\ncms_ui\Entity\ContentInterface;

/**
* Implements hook_form_FORM_ID_alter().
Expand All @@ -16,7 +16,7 @@ function ncms_publisher_form_node_form_alter(&$form, FormStateInterface $form_st
/** @var \Drupal\node\NodeForm $form_object */
$form_object = $form_state->getFormObject();
$entity = $form_object->getEntity();
if (!$entity instanceof ContentBase) {
if (!$entity instanceof ContentInterface) {
return;
}

Expand Down
10 changes: 5 additions & 5 deletions html/modules/custom/ncms_ui/ncms_ui.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\content_moderation\Entity\ContentModerationStateInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\ncms_ui\Entity\Content\ContentBase;
use Drupal\ncms_ui\Entity\ContentInterface;
use Drupal\node\NodeInterface;

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ function ncms_ui_deploy_set_moderation_state() {
/** @var \Drupal\node\NodeInterface[] $revisions */
$revisions = $node_storage->loadMultipleRevisions($revision_ids);
foreach ($revisions as $revision) {
if (!$revision instanceof ContentBase || $revision->isDefaultRevision() || $revision->isLatestRevision()) {
if (!$revision instanceof ContentInterface || $revision->isDefaultRevision() || $revision->isLatestRevision()) {
continue;
}

Expand All @@ -85,7 +85,7 @@ function ncms_ui_deploy_set_moderation_state() {
/** @var \Drupal\node\NodeInterface[] $revisions */
$revisions = $node_storage->loadMultipleRevisions($revision_ids);
foreach ($revisions as $revision) {
if (!$revision instanceof ContentBase || $revision->isDefaultRevision() || $revision->isLatestRevision() || !$revision->isPublished()) {
if (!$revision instanceof ContentInterface || $revision->isDefaultRevision() || $revision->isLatestRevision() || !$revision->isPublished()) {
continue;
}
$node_storage->updateRevisionStatus($revision, NodeInterface::NOT_PUBLISHED, FALSE);
Expand Down Expand Up @@ -260,7 +260,7 @@ function ncms_ui_deploy_correct_changed_date(&$sandbox) {
]);

foreach ($nodes as $node) {
if (!$node instanceof ContentBase) {
if (!$node instanceof ContentInterface) {
continue;
}

Expand All @@ -271,7 +271,7 @@ function ncms_ui_deploy_correct_changed_date(&$sandbox) {
}
$revision_ids = array_values(array_reverse($revision_ids));

/** @var \Drupal\ncms_ui\Entity\Content\ContentBase $revision */
/** @var \Drupal\ncms_ui\Entity\ContentInterface $revision */
$revision = $node_storage->loadRevision($revision_ids[0]);
if (!$revision) {
continue;
Expand Down
33 changes: 33 additions & 0 deletions html/modules/custom/ncms_ui/ncms_ui.install
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\field\Entity\FieldStorageConfig;
use Drupal\ncms_ui\Entity\ContentInterface;

/**
* Remove existing terms of type article type.
Expand Down Expand Up @@ -127,3 +128,35 @@ function ncms_ui_update_9007(&$sandox) {
}
return 'Deleted ' . count($ids) . ' paragraphs of type "layout"';
}

/**
* Remove duplicated trash revisions.
*
* There should really only be one per entity.
*/
function ncms_ui_update_9008(&$sandbox) {
$database = \Drupal::database();
$query = $database->query('SELECT content_entity_id, COUNT(id) AS cnt FROM {content_moderation_state_field_revision} m
WHERE moderation_state = :moderation_state
GROUP BY content_entity_id
HAVING cnt > 1', [
':moderation_state' => 'trash',
]);
$result = $query->fetchAll();
if (!$result) {
return;
}

/** @var \Drupal\ncms_ui\Entity\Storage\ContentStorage $node_storage */
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
foreach ($result as $row) {
$entity_id = $row->content_entity_id;
/** @var \Drupal\ncms_ui\Entity\ContentInterface $entity */
$entity = $node_storage->load($entity_id);
while ($entity instanceof ContentInterface && $entity?->getPreviousRevision()?->isModerationState('trash')) {
$node_storage->deleteLatestRevision($entity);
/** @var \Drupal\ncms_ui\Entity\ContentInterface $entity */
$entity = $node_storage->load($entity_id);
}
}
}
11 changes: 6 additions & 5 deletions html/modules/custom/ncms_ui/ncms_ui.module
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use Drupal\ncms_ui\Entity\Content\Article;
use Drupal\ncms_ui\Entity\Content\ContentBase;
use Drupal\ncms_ui\Entity\Content\Document;
use Drupal\ncms_ui\Entity\Content\Story;
use Drupal\ncms_ui\Entity\ContentInterface;
use Drupal\ncms_ui\Entity\ContentSpaceAwareInterface;
use Drupal\ncms_ui\Entity\EntityOverviewInterface;
use Drupal\ncms_ui\Entity\IframeDisplayContentInterface;
Expand Down Expand Up @@ -89,7 +90,7 @@ function ncms_ui_entity_base_field_info_alter(&$fields, $entity_type) {
*/
function ncms_ui_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity instanceof ContentBase) {
if ($entity instanceof ContentInterface) {
$operations = $entity->getEntityOperations();
}
return $operations;
Expand Down Expand Up @@ -247,7 +248,7 @@ function ncms_ui_preprocess_html(&$variables) {
$variables['page']['content']['system_main']['#attached']['library'][] = 'ncms_ui/diff';
}

if ($route_name == 'entity.node.edit_form' && $route_match->getParameter('node') instanceof ContentBase) {
if ($route_name == 'entity.node.edit_form' && $route_match->getParameter('node') instanceof ContentInterface) {
$variables['attributes']['class'][] = 'node-content-base';
}
/** @var \Drupal\node\Entity\NodeType $node_type */
Expand Down Expand Up @@ -339,7 +340,7 @@ function ncms_ui_form_node_form_alter(&$form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'ncms_ui/article_edit_form';
}

if ($node instanceof ContentBase) {
if ($node instanceof ContentInterface) {
/** @var \Drupal\ncms_ui\Form\ContentBaseFormAlter $content_base_form_alter */
$content_base_form_alter = \Drupal::service('ncms_ui.content_base_form_alter');
$content_base_form_alter->alterForm($form, $form_state);
Expand Down Expand Up @@ -557,13 +558,13 @@ function ncms_ui_link_alter(&$variables) {
$node_revision = !empty($route_params['node_revision']) ? $node_storage->loadRevision($route_params['node_revision']) : NULL;

$title = t('Preview: @title', ['@title' => $variables['text']]);
if ($node && $node instanceof ContentBase) {
if ($node && $node instanceof ContentInterface) {
$title = t('Preview: @title (@status)', [
'@title' => $node->label(),
'@status' => $node->isPublished() ? t('Latest published') : t('Latest draft'),
]);
}
if ($node_revision && $node_revision instanceof ContentBase) {
if ($node_revision && $node_revision instanceof ContentInterface) {
$title = t('Preview: #@version (@status) - @date', [
'@version' => $node_revision->getVersionId(),
'@status' => $node_revision->getVersionStatusLabel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\ncms_ui\Entity\Content\ContentBase;
use Drupal\ncms_ui\Entity\ContentInterface;
use Drupal\ncms_ui\Entity\Storage\ContentStorage;
use Drupal\node\NodeInterface;

Expand All @@ -19,13 +19,13 @@ class RevisionController extends ControllerBase implements ContainerInjectionInt
/**
* Publish a node revision.
*
* @param \Drupal\ncms_ui\Entity\Content\ContentBase $node_revision
* @param \Drupal\ncms_ui\Entity\ContentInterface $node_revision
* The node revision.
*
* @return array
* An array suitable for \Drupal\Core\Render\RendererInterface::render().
*/
public function publish(ContentBase $node_revision) {
public function publish(ContentInterface $node_revision) {
if ($this->setRevisionStatus($node_revision, NodeInterface::PUBLISHED)) {
$this->messenger()->addStatus($this->t('Version #@version has been published', [
'@version' => $node_revision->getVersionId(),
Expand All @@ -43,13 +43,13 @@ public function publish(ContentBase $node_revision) {
/**
* Publish a node revision.
*
* @param \Drupal\ncms_ui\Entity\Content\ContentBase $node_revision
* @param \Drupal\ncms_ui\Entity\ContentInterface $node_revision
* The node revision.
*
* @return array
* An array suitable for \Drupal\Core\Render\RendererInterface::render().
*/
public function unpublish(ContentBase $node_revision) {
public function unpublish(ContentInterface $node_revision) {
if ($this->setRevisionStatus($node_revision, NodeInterface::NOT_PUBLISHED)) {
$this->messenger()->addStatus($this->t('Version #@version has been unpublished', [
'@version' => $node_revision->getVersionId(),
Expand Down
49 changes: 32 additions & 17 deletions html/modules/custom/ncms_ui/src/Entity/Content/ContentBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\ncms_ui\Entity\ContentInterface;
use Drupal\ncms_ui\Entity\ContentSpaceAwareInterface;
use Drupal\ncms_ui\Entity\ContentVersionInterface;
use Drupal\ncms_ui\Entity\EntityOverviewInterface;
use Drupal\ncms_ui\Entity\IframeDisplayContentInterface;
use Drupal\ncms_ui\Traits\ContentSpaceEntityTrait;
use Drupal\ncms_ui\Traits\IframeDisplayContentTrait;
use Drupal\node\Entity\Node;
Expand All @@ -22,7 +19,7 @@
/**
* Bundle class for organization nodes.
*/
abstract class ContentBase extends Node implements ContentInterface, ContentSpaceAwareInterface, ContentVersionInterface, EntityOverviewInterface, IframeDisplayContentInterface {
abstract class ContentBase extends Node implements ContentInterface {

use StringTranslationTrait;
use ContentSpaceEntityTrait;
Expand Down Expand Up @@ -89,15 +86,15 @@ public function getBundleLabel() {
*/
public function setPublished() {
parent::setPublished();
$this->moderation_state->value = 'published';
$this->setModerationState('published');
}

/**
* {@inheritdoc}
*/
public function setUnpublished() {
parent::setUnpublished();
$this->moderation_state->value = 'draft';
$this->setModerationState('draft');
}

/**
Expand All @@ -108,7 +105,7 @@ public function setDeleted() {
$this->isDefaultRevision(TRUE);
$this->setNewRevision(TRUE);
$this->setRevisionTranslationAffectedEnforced(TRUE);
$this->moderation_state->value = 'trash';
$this->setModerationState('trash');
}

/**
Expand All @@ -118,7 +115,7 @@ public function isDeleted() {
if ($this->isNew()) {
return FALSE;
}
return $this->getLatestRevision()->moderation_state->value == 'trash';
return $this->getLatestRevision()->isModerationState('trash');
}

/**
Expand All @@ -132,6 +129,27 @@ public function getVersionId() {
return $version_key !== FALSE ? $version_key + 1 : NULL;
}

/**
* {@inheritdoc}
*/
public function getModerationState() {
return $this->moderation_state->value;
}

/**
* {@inheritdoc}
*/
public function setModerationState($state) {
$this->moderation_state->value = $state;
}

/**
* {@inheritdoc}
*/
public function isModerationState($state) {
return $this->getModerationState() == $state;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -193,10 +211,7 @@ public function getVersionStatusLabel() {
}

/**
* Get the latest revision.
*
* @return \Drupal\ncms_ui\Entity\Content\ContentBase|null
* The latest revision if available.
* {@inheritdoc}
*/
public function getLatestRevision() {
/** @var \Drupal\Node\NodeStorageInterface $node_storage */
Expand Down Expand Up @@ -230,18 +245,18 @@ public function getPreviousRevision() {
/** @var \Drupal\Node\NodeStorageInterface $node_storage */
$node_storage = $this->entityTypeManager()->getStorage('node');
$revision_ids = array_reverse($node_storage->revisionIds($this));
if (count($revision_ids) < 2) {
return NULL;
}
array_shift($revision_ids);
$previous_revision_id = array_shift($revision_ids);

/** @var \Drupal\node\NodeInterface[] $revisions */
return $node_storage->loadRevision($previous_revision_id);
return $previous_revision_id ? $node_storage->loadRevision($previous_revision_id) : NULL;
}

/**
* Retrieve entity operations specific to our workflows.
*
* @return array
* An array of operation links.
* {@inheritdoc}
*/
public function getEntityOperations() {
$operations = [];
Expand Down
10 changes: 9 additions & 1 deletion html/modules/custom/ncms_ui/src/Entity/ContentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Defines an interface for entities with content spaces.
*/
interface ContentInterface extends NodeInterface {
interface ContentInterface extends NodeInterface, ContentSpaceAwareInterface, ContentVersionInterface, EntityOverviewInterface, IframeDisplayContentInterface {

/**
* Get the URL for the overview backend listing of this content type.
Expand Down Expand Up @@ -35,4 +35,12 @@ public function setDeleted();
*/
public function isDeleted();

/**
* Retrieve entity operations specific to our workflows.
*
* @return array
* An array of operation links.
*/
public function getEntityOperations();

}
Loading

0 comments on commit 29f9fe2

Please sign in to comment.