You are here

public function CreateModeratedForwardRevision::onPreEntitySave in Acquia Content Hub 8.2

Creates a forward revision and state change for entities being imported.

Parameters

\Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event: The pre entity save event.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/acquia_contenthub_moderation/src/EventSubscriber/PreEntitySave/CreateModeratedForwardRevision.php, line 94

Class

CreateModeratedForwardRevision
Sets the entity with a forward revision and change of moderation state.

Namespace

Drupal\acquia_contenthub_moderation\EventSubscriber\PreEntitySave

Code

public function onPreEntitySave(PreEntitySaveEvent $event) {
  $entity = $event
    ->getEntity();
  if (!$entity instanceof ContentEntityInterface) {

    // Only Content Entities can be moderated.
    return;
  }
  $entity_type = $entity
    ->getEntityType();
  $bundle = $entity
    ->bundle();
  if (!$bundle) {
    return;
  }

  // If this bundle is not moderated, nothing needs to be done.
  if (!$this->moderationInfo
    ->shouldModerateEntitiesOfBundle($entity_type, $bundle)) {
    return;
  }

  // Is there a workflow moderation state defined for imported entities.
  $workflow = $this->moderationInfo
    ->getWorkflowForEntity($entity);
  if (!$workflow) {
    return;
  }
  $import_moderation_state = $this->config
    ->get("workflows.{$workflow->id()}.moderation_state");
  if (empty($import_moderation_state)) {
    $this->channel
      ->error("Acquia Content Hub moderation config doesn't have a default moderation state.");
    return;
  }

  // Use configured moderation state.
  $entity
    ->set($this->fieldName, $import_moderation_state);

  // Check whether the entity is configured to create a new revision.
  if ($entity instanceof RevisionableInterface && $entity
    ->isNewRevision()) {

    // Make it a forward revision if import moderation state is
    // not a "published" state.
    $published_state = $workflow
      ->getTypePlugin()
      ->getState($import_moderation_state)
      ->isPublishedState();
    if (!$published_state) {
      $entity
        ->isDefaultRevision(FALSE);
    }
  }
}