Content Moderation compatibility

Created on 10 March 2025, 27 days ago

Problem/Motivation

Entities of type "Site" with a "Content moderation" workflow, when updated to the "Published" state, become unpublished (status field set to 0).

Steps to reproduce

  1. Install the Micro Site and Content Moderation modules
  2. Create a new site type and add an entity (/site/add)
  3. Create a Content moderation Workflow
  4. Edit the previously created entity and save it with the "Change to" field set to "Published"
  5. Check that the saved site is not published (has status field set to 0)

Proposed resolution

Create Drupal\micro_site\Entity\Handler\SiteModerationHandler extending Drupal\content_moderation\Entity\Handler\ModerationHandler.

Then we can use the onPresave function to save the entity status in the correct way.

class SiteModerationHandler extends ModerationHandler {
...
  /**
   * {@inheritdoc}
   */
  public function onPresave(ContentEntityInterface $entity, $default_revision, $published_state) {
    // When entities are syncing, content moderation should not force a new
    // revision to be created and should not update the default status of a
    // revision. This is useful if changes are being made to entities or
    // revisions which are not part of editorial updates triggered by normal
    // content changes.
    if (!$entity->isSyncing()) {
      $entity->setNewRevision(TRUE);
      $entity->isDefaultRevision($default_revision);
    }

    // Update publishing status if it can be updated and if it needs updating.
    if (($entity instanceof SiteInterface) && $entity->isPublished() !== $published_state) {
      $entity->setPublished($published_state);
    }
  }
}

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇵🇹Portugal lolgm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024