SwitchShortcutSet::buildForm

public SwitchShortcutSet::buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL)

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 60

Class

SwitchShortcutSet
Builds the shortcut set switch form.

Namespace

Drupal\shortcut\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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'] = array(
      '#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'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#description' => $this->t('The new set is created by copying items from your default shortcut set.'),
      '#access' => $add_access,
      '#states' => array(
        'visible' => array(
          ':input[name="set"]' => array('value' => 'new'),
        ),
        'required' => array(
          ':input[name="set"]' => array('value' => 'new'),
        ),
      ),
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#machine_name' => array(
        'exists' => array($this, 'exists'),
        'replace_pattern' => '[^a-z0-9-]+',
        'replace' => '-',
      ),
      // This ID could be used for menu name.
      '#maxlength' => 23,
      '#states' => array(
        'required' => array(
          ':input[name="set"]' => array('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 items from the %default set.', array('%default' => $default_set->label()));
    }
 
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Change set'),
    );
  }
  else {
    // There is only 1 option, so output a message in the $form array.
    $form['info'] = array(
      '#markup' => '<p>' . $this->t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '</p>',
    );
  }
 
  return $form;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.