setActiveWorkspace example

/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $id = $form_state->getValue('workspace_id');

    /** @var \Drupal\workspaces\WorkspaceInterface $workspace */
    $workspace = $this->workspaceStorage->load($id);

    try {
      $this->workspaceManager->setActiveWorkspace($workspace);
      $this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $workspace->label()]));
    }
    catch (WorkspaceAccessException $e) {
      $this->messenger->addError($this->t('You do not have access to activate the %workspace_label workspace.', ['%workspace_label' => $workspace->label()]));
    }
  }

  /** * Submit handler for switching to the live version of the site. */
  public function submitSwitchToLive(array &$form, FormStateInterface $form_state) {
    
/** * {@inheritdoc} */
  public function setActiveWorkspace(WorkspaceInterface $workspace) {
    $this->doSwitchWorkspace($workspace);

    // Set the workspace on the proper negotiator.     $request = $this->requestStack->getCurrentRequest();
    foreach ($this->negotiatorIds as $negotiator_id) {
      $negotiator = $this->classResolver->getInstanceFromDefinition($negotiator_id);
      if ($negotiator->applies($request)) {
        $negotiator->setActiveWorkspace($workspace);
        break;
      }
    }

    return $this;
  }

  /** * {@inheritdoc} */
  public function switchToLive() {
    
    $cid = implode(':', $cid_parts);
    $bin = $build['#cache']['bin'];
    $this->assertInstanceOf(\stdClass::class$this->container->get('cache.' . $bin)->get($cid));

    // Switch to the 'stage' workspace and check that the correct workspace     // cache context is used.     $test_user = $this->drupalCreateUser(['view any workspace']);
    $this->drupalLogin($test_user);

    $stage = Workspace::load('stage');
    $workspace_manager = \Drupal::service('workspaces.manager');
    $workspace_manager->setActiveWorkspace($stage);

    $cache_context = new WorkspaceCacheContext($workspace_manager);
    $this->assertSame('stage', $cache_context->getContext());

    $build = \Drupal::entityTypeManager()->getViewBuilder('node')->view($node, 'full');

    // Render it so the default cache contexts are applied.     $renderer->renderRoot($build);
    $this->assertContains('workspace', $build['#cache']['contexts']);

    $cid_parts = array_merge($build['#cache']['keys']$cache_contexts_manager->convertTokensToKeys($build['#cache']['contexts'])->getKeys());
    

  protected function switchToWorkspace(WorkspaceInterface $workspace) {
    $this->assertTrue($this->switcherBlockConfigured, 'This test was not written correctly: you must call setupWorkspaceSwitcherBlock() before switchToWorkspace()');
    /** @var \Drupal\Tests\WebAssert $session */
    $session = $this->assertSession();
    $session->buttonExists('Activate');
    $this->submitForm(['workspace_id' => $workspace->id()], 'Activate');
    $session->pageTextContains($workspace->label() . ' is now the active workspace.');
    // Keep the test runner in sync with the system under test.     \Drupal::service('workspaces.manager')->setActiveWorkspace($workspace);
  }

  /** * Switches to the live version of the site for subsequent requests. * * This assumes that the switcher block has already been setup by calling * setupWorkspaceSwitcherBlock(). */
  protected function switchToLive() {
    /** @var \Drupal\Tests\WebAssert $session */
    $session = $this->assertSession();
    
public function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form$form_state);
    $actions['cancel']['#attributes']['class'][] = 'dialog-cancel';
    return $actions;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    try {
      $this->workspaceManager->setActiveWorkspace($this->entity);
      $this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $this->entity->label()]));
    }
    catch (WorkspaceAccessException $e) {
      $this->messenger->addError($this->t('You do not have access to activate the %workspace_label workspace.', ['%workspace_label' => $this->entity->label()]));
    }

    // Redirect to the workspace manage page by default.     if (!$this->getRequest()->query->has('destination')) {
      $form_state->setRedirectUrl($this->entity->toUrl());
    }
  }

  
$this->setCurrentUser($admin);

    /** @var \Drupal\workspaces\WorkspaceAssociationInterface $workspace_association */
    $workspace_association = \Drupal::service('workspaces.association');

    // Create a workspace with a very small number of associated node revisions.     $workspace_1 = Workspace::create([
      'id' => 'gibbon',
      'label' => 'Gibbon',
    ]);
    $workspace_1->save();
    $this->workspaceManager->setActiveWorkspace($workspace_1);

    $workspace_1_node_1 = $this->createNode(['status' => FALSE]);
    $workspace_1_node_2 = $this->createNode(['status' => FALSE]);

    // Check that the workspace tracks the initial revisions for both nodes.     $initial_revisions = $workspace_association->getAssociatedInitialRevisions($workspace_1->id(), 'node');
    $this->assertCount(2, $initial_revisions);

    for ($i = 0; $i < 4; $i++) {
      $workspace_1_node_1->setNewRevision(TRUE);
      $workspace_1_node_1->save();

      


  /** * Sets a given workspace as active. * * @param string $workspace_id * The ID of the workspace to switch to. */
  protected function switchToWorkspace($workspace_id) {
    // Switch the test runner's context to the specified workspace.     $workspace = $this->entityTypeManager->getStorage('workspace')->load($workspace_id);
    \Drupal::service('workspaces.manager')->setActiveWorkspace($workspace);
  }

  /** * Creates a test workspace hierarchy. * * The full hierarchy including the default workspaces 'live' and 'stage' is: * * live * - stage * - dev * - local_1 * - local_2 * - qa */
public function applies(Request $request) {
    return is_string($request->query->get('workspace')) && parent::applies($request);
  }

  /** * {@inheritdoc} */
  public function getActiveWorkspace(Request $request) {
    $workspace_id = $request->query->get('workspace');

    if ($workspace_id && ($workspace = $this->workspaceStorage->load($workspace_id))) {
      $this->setActiveWorkspace($workspace);
      return $workspace;
    }

    return NULL;
  }

}
Home | Imprint | This part of the site doesn't use cookies.