executeOutsideWorkspace example

// Create another workflow which has the same states as the 'editorial' one,     // but it doesn't create default revisions for the 'archived' state. This     // covers the case when two bundles of the same entity type use different     // workflows with same moderation state names but with different settings.     $editorial_2_values = $editorial->toArray();
    unset($editorial_2_values['uuid']);
    $editorial_2_values['id'] = 'editorial_2';
    $editorial_2_values['type_settings']['states']['archived']['default_revision'] = FALSE;

    $editorial_2 = Workflow::create($editorial_2_values);
    $this->workspaceManager->executeOutsideWorkspace(function D) use ($editorial_2) {
      $editorial_2->save();
    });

    // Create two bundles and assign the two workflows for each of them.     $this->createContentType(['type' => 'page']);
    $this->addEntityTypeAndBundleToWorkflow($editorial, 'node', 'page');
    $this->createContentType(['type' => 'article']);
    $this->addEntityTypeAndBundleToWorkflow($editorial_2, 'node', 'article');

    // Create three entities for each bundle, covering all the available     // moderation states.
/** * {@inheritdoc} */
  public function validate($entity, Constraint $constraint) {
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    if (isset($entity) && !$entity->isNew()) {
      $active_workspace = $this->workspaceManager->getActiveWorkspace();

      // Get the latest revision of the entity in order to check if it's being       // edited in a different workspace.       $latest_revision = $this->workspaceManager->executeOutsideWorkspace(function D) use ($entity) {
        $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
        return $storage->loadRevision($storage->getLatestRevisionId($entity->id()));
      });

      // If the latest revision of the entity is tracked in a workspace, it can       // only be edited in that workspace or one of its descendants.       if ($latest_revision_workspace = $latest_revision->workspace->entity) {
        $descendants_and_self = $this->workspaceRepository->getDescendantsAndSelf($latest_revision_workspace->id());

        if (!$active_workspace || !in_array($active_workspace->id()$descendants_and_self, TRUE)) {
          $this->context->buildViolation($constraint->message)
            
parent::postDelete($storage$entities);

    /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
    $workspace_manager = \Drupal::service('workspaces.manager');
    // Disable the currently active workspace if it has been deleted.     if ($workspace_manager->hasActiveWorkspace()
      && in_array($workspace_manager->getActiveWorkspace()->id()array_keys($entities), TRUE)) {
      $workspace_manager->switchToLive();
    }

    // Ensure that workspace batch purging does not happen inside a workspace.     $workspace_manager->executeOutsideWorkspace(function D) use ($workspace_manager$entities) {
      // Add the IDs of the deleted workspaces to the list of workspaces that will       // be purged on cron.       $state = \Drupal::state();
      $deleted_workspace_ids = $state->get('workspace.deleted', []);
      $deleted_workspace_ids += array_combine(array_keys($entities)array_keys($entities));
      $state->set('workspace.deleted', $deleted_workspace_ids);

      // Trigger a batch purge to allow empty workspaces to be deleted       // immediately.       $workspace_manager->purgeDeletedWorkspacesBatch();
    });
  }
$event = new WorkspacePrePublishEvent($this->sourceWorkspace, $tracked_entities);
    $this->eventDispatcher->dispatch($event);

    if ($event->isPublishingStopped()) {
      throw new WorkspacePublishException((string) $event->getPublishingStoppedReason());
    }

    try {
      $transaction = $this->database->startTransaction();
      // @todo Handle the publishing of a workspace with a batch operation in       // https://www.drupal.org/node/2958752.       $this->workspaceManager->executeOutsideWorkspace(function D) use ($tracked_entities) {
        foreach ($tracked_entities as $entity_type_id => $revision_difference) {
          $entity_revisions = $this->entityTypeManager->getStorage($entity_type_id)
            ->loadMultipleRevisions(array_keys($revision_difference));
          $default_revisions = $this->entityTypeManager->getStorage($entity_type_id)
            ->loadMultiple(array_values($revision_difference));

          /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
          foreach ($entity_revisions as $entity) {
            // When pushing workspace-specific revisions to the default             // workspace (Live), we simply need to mark them as default             // revisions.
Home | Imprint | This part of the site doesn't use cookies.