You are here

public function WorkspaceSwitcherForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces\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 76

Class

WorkspaceSwitcherForm
Provides a form that activates a different workspace.

Namespace

Drupal\workspaces\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $workspaces = $this->workspaceStorage
    ->loadMultiple();
  $workspace_labels = [];
  foreach ($workspaces as $workspace) {
    $workspace_labels[$workspace
      ->id()] = $workspace
      ->label();
  }
  $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;
}