function WorkspaceSubscriber::onWorkspacePrePublish

Same name in other branches
  1. 10 core/modules/content_moderation/src/EventSubscriber/WorkspaceSubscriber.php \Drupal\content_moderation\EventSubscriber\WorkspaceSubscriber::onWorkspacePrePublish()

Prevents a workspace from being published based on certain conditions.

Parameters

\Drupal\workspaces\Event\WorkspacePublishEvent $event: The workspace publish event.

File

core/modules/content_moderation/src/EventSubscriber/WorkspaceSubscriber.php, line 36

Class

WorkspaceSubscriber
Checks whether a workspace is publishable, and prevents publishing if needed.

Namespace

Drupal\content_moderation\EventSubscriber

Code

public function onWorkspacePrePublish(WorkspacePublishEvent $event) : void {
    // Prevent a workspace from being published if there are any pending
    // revisions in a moderation state that doesn't create default revisions.
    $workspace = $event->getWorkspace();
    $tracked_revisions = $this->workspaceAssociation
        ->getTrackedEntities($workspace->id());
    // Gather a list of moderation states that don't create a default revision.
    $workflow_non_default_states = [];
    foreach ($this->entityTypeManager
        ->getStorage('workflow')
        ->loadByProperties([
        'type' => 'content_moderation',
    ]) as $workflow) {
        
        /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $workflow_type */
        $workflow_type = $workflow->getTypePlugin();
        // Find all workflows which are moderating entity types of the same type
        // to those that are tracked by the workspace.
        if (array_intersect($workflow_type->getEntityTypes(), array_keys($tracked_revisions))) {
            $workflow_non_default_states[$workflow->id()] = array_filter(array_map(function (ContentModerationState $state) {
                return !$state->isDefaultRevisionState() ? $state->id() : NULL;
            }, $workflow_type->getStates()));
        }
    }
    // If no tracked revisions are moderated then no status check is necessary.
    if (empty($workflow_non_default_states)) {
        return;
    }
    // Check if any revisions that are about to be published are in a
    // non-default revision moderation state.
    $query = $this->entityTypeManager
        ->getStorage('content_moderation_state')
        ->getQuery()
        ->allRevisions()
        ->accessCheck(FALSE);
    $tracked_revisions_condition_group = $query->orConditionGroup();
    foreach ($tracked_revisions as $tracked_type => $tracked_revision_ids) {
        $entity_type_group = $query->andConditionGroup()
            ->condition('content_entity_type_id', $tracked_type)
            ->condition('content_entity_revision_id', array_keys($tracked_revision_ids), 'IN');
        $tracked_revisions_condition_group->condition($entity_type_group);
    }
    $query->condition($tracked_revisions_condition_group);
    $workflow_condition_group = $query->orConditionGroup();
    foreach ($workflow_non_default_states as $workflow_id => $non_default_states) {
        $group = $query->andConditionGroup()
            ->condition('workflow', $workflow_id, '=')
            ->condition('moderation_state', $non_default_states, 'IN');
        $workflow_condition_group->condition($group);
    }
    $query->condition($workflow_condition_group);
    if ($count = $query->count()
        ->execute()) {
        $message = \Drupal::translation()->formatPlural($count, 'The @label workspace can not be published because it contains 1 item in an unpublished moderation state.', 'The @label workspace can not be published because it contains @count items in an unpublished moderation state.', [
            '@label' => $workspace->label(),
        ]);
        $event->stopPublishing();
        $event->setPublishingStoppedReason((string) $message);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.