You are here

public function SwitchShortcutSet::buildForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::buildForm()
  2. 9 core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::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/shortcut/src/Form/SwitchShortcutSet.php, line 63

Class

SwitchShortcutSet
Builds the shortcut set switch form.

Namespace

Drupal\shortcut\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
  $account = $this
    ->currentUser();
  $this->user = $user;

  // Prepare the list of shortcut sets.
  $options = array_map(function (ShortcutSet $set) {
    return $set
      ->label();
  }, $this->shortcutSetStorage
    ->loadMultiple());
  $current_set = shortcut_current_displayed_set($this->user);

  // Only administrators can add shortcut sets.
  $add_access = $account
    ->hasPermission('administer shortcuts');
  if ($add_access) {
    $options['new'] = $this
      ->t('New set');
  }
  $account_is_user = $this->user
    ->id() == $account
    ->id();
  if (count($options) > 1) {
    $form['set'] = [
      '#type' => 'radios',
      '#title' => $account_is_user ? $this
        ->t('Choose a set of shortcuts to use') : $this
        ->t('Choose a set of shortcuts for this user'),
      '#options' => $options,
      '#default_value' => $current_set
        ->id(),
    ];
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#description' => $this
        ->t('The new set is created by copying links from your default shortcut set.'),
      '#access' => $add_access,
      '#states' => [
        'visible' => [
          ':input[name="set"]' => [
            'value' => 'new',
          ],
        ],
        'required' => [
          ':input[name="set"]' => [
            'value' => 'new',
          ],
        ],
      ],
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#machine_name' => [
        'exists' => [
          $this,
          'exists',
        ],
        'replace_pattern' => '[^a-z0-9-]+',
        'replace' => '-',
      ],
      // This ID could be used for menu name.
      '#maxlength' => 23,
      '#states' => [
        'required' => [
          ':input[name="set"]' => [
            'value' => 'new',
          ],
        ],
      ],
      '#required' => FALSE,
    ];
    if (!$account_is_user) {
      $default_set = $this->shortcutSetStorage
        ->getDefaultSet($this->user);
      $form['new']['#description'] = $this
        ->t('The new set is created by copying links from the %default set.', [
        '%default' => $default_set
          ->label(),
      ]);
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Change set'),
    ];
  }
  else {

    // There is only 1 option, so output a message in the $form array.
    $form['info'] = [
      '#markup' => '<p>' . $this
        ->t('You are currently using the %set-name shortcut set.', [
        '%set-name' => $current_set
          ->label(),
      ]) . '</p>',
    ];
  }
  return $form;
}