function WorkspaceSwitcherForm::buildForm

Same name in this branch
  1. 11.x core/modules/workspaces_ui/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces_ui\Form\WorkspaceSwitcherForm::buildForm()
Same name and namespace in other branches
  1. 10 core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces\Form\WorkspaceSwitcherForm::buildForm()
  2. 9 core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces\Form\WorkspaceSwitcherForm::buildForm()
  3. 8.9.x core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces\Form\WorkspaceSwitcherForm::buildForm()
  4. main core/modules/workspaces_ui/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces_ui\Form\WorkspaceSwitcherForm::buildForm()

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php, line 89

Class

WorkspaceSwitcherForm
Provides a form that activates a different workspace.

Namespace

Drupal\workspaces\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  // Use the workspace selection handler to retrieve only the relevant
  // workspaces.
  $selection_handler = $this->selectionManager
    ->getInstance([
    'target_type' => 'workspace',
    'handler' => 'default',
  ]);
  $workspace_labels = $selection_handler->getReferenceableEntities()['workspace'] ?? [];
  $active_workspace = $this->workspaceManager
    ->getActiveWorkspace();
  if ($active_workspace) {
    unset($workspace_labels[$active_workspace->id()]);
  }
  $form['current'] = [
    '#type' => 'item',
    '#title' => $this->t('Current workspace'),
    '#markup' => $active_workspace ? $active_workspace->label() : $this->t('None'),
    '#wrapper_attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
  ];
  $form['workspace_id'] = [
    '#type' => 'select',
    '#title' => $this->t('Select workspace'),
    '#required' => TRUE,
    '#options' => $workspace_labels,
    '#wrapper_attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#access' => !empty($workspace_labels),
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Activate'),
    '#button_type' => 'primary',
    '#access' => !empty($workspace_labels),
  ];
  if ($active_workspace) {
    $form['actions']['switch_to_live'] = [
      '#type' => 'submit',
      '#submit' => [
        '::submitSwitchToLive',
      ],
      '#value' => $this->t('Switch to Live'),
      '#limit_validation_errors' => [],
      '#button_type' => 'primary',
    ];
  }
  return $form;
}

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