You are here

public function WorkspaceSwitcherForm::submitForm in Workspace 8.2

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/WorkspaceSwitcherForm.php, line 116

Class

WorkspaceSwitcherForm
Provides a form that activates a different workspace.

Namespace

Drupal\workspace\Form

Code

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

  /** @var \Drupal\workspace\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(),
    ]));
    $form_state
      ->setRedirect('<front>');
  } catch (WorkspaceAccessException $e) {
    $this->messenger
      ->addError($this
      ->t('You do not have access to activate the %workspace_label workspace.', [
      '%workspace_label' => $workspace
        ->label(),
    ]));
  }
}