You are here

public function WorkspaceSwitcherForm::buildForm in Workspace 8.2

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

src/Form/WorkspaceSwitcherForm.php, line 76

Class

WorkspaceSwitcherForm
Provides a form that activates a different workspace.

Namespace

Drupal\workspace\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();
  unset($workspace_labels[$active_workspace
    ->id()]);
  $form['current'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Current workspace'),
    '#markup' => $active_workspace
      ->label(),
    '#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',
      ],
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Activate'),
  ];
  return $form;
}