649cef765b7279aef4c9f7cb518554f07c66ef4b
[yaffs-website] / web / core / modules / shortcut / src / Form / SwitchShortcutSet.php
1 <?php
2
3 namespace Drupal\shortcut\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\shortcut\Entity\ShortcutSet;
8 use Drupal\shortcut\ShortcutSetStorageInterface;
9 use Drupal\user\UserInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Builds the shortcut set switch form.
14  */
15 class SwitchShortcutSet extends FormBase {
16
17   /**
18    * The account the shortcut set is for.
19    *
20    * @var \Drupal\user\UserInterface
21    */
22   protected $user;
23
24   /**
25    * The shortcut set storage.
26    *
27    * @var \Drupal\shortcut\ShortcutSetStorageInterface
28    */
29   protected $shortcutSetStorage;
30
31   /**
32    * Constructs a SwitchShortcutSet object.
33    *
34    * @param \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_set_storage
35    *   The shortcut set storage.
36    */
37   public function __construct(ShortcutSetStorageInterface $shortcut_set_storage) {
38     $this->shortcutSetStorage = $shortcut_set_storage;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static(
46       $container->get('entity.manager')->getStorage('shortcut_set')
47     );
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getFormId() {
54     return 'shortcut_set_switch';
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
61     $account = $this->currentUser();
62
63     $this->user = $user;
64
65     // Prepare the list of shortcut sets.
66     $options = array_map(function (ShortcutSet $set) {
67       return $set->label();
68     }, $this->shortcutSetStorage->loadMultiple());
69
70     $current_set = shortcut_current_displayed_set($this->user);
71
72     // Only administrators can add shortcut sets.
73     $add_access = $account->hasPermission('administer shortcuts');
74     if ($add_access) {
75       $options['new'] = $this->t('New set');
76     }
77
78     $account_is_user = $this->user->id() == $account->id();
79     if (count($options) > 1) {
80       $form['set'] = [
81         '#type' => 'radios',
82         '#title' => $account_is_user ? $this->t('Choose a set of shortcuts to use') : $this->t('Choose a set of shortcuts for this user'),
83         '#options' => $options,
84         '#default_value' => $current_set->id(),
85       ];
86
87       $form['label'] = [
88         '#type' => 'textfield',
89         '#title' => $this->t('Label'),
90         '#description' => $this->t('The new set is created by copying items from your default shortcut set.'),
91         '#access' => $add_access,
92         '#states' => [
93           'visible' => [
94             ':input[name="set"]' => ['value' => 'new'],
95           ],
96           'required' => [
97             ':input[name="set"]' => ['value' => 'new'],
98           ],
99         ],
100       ];
101       $form['id'] = [
102         '#type' => 'machine_name',
103         '#machine_name' => [
104           'exists' => [$this, 'exists'],
105           'replace_pattern' => '[^a-z0-9-]+',
106           'replace' => '-',
107         ],
108         // This ID could be used for menu name.
109         '#maxlength' => 23,
110         '#states' => [
111           'required' => [
112             ':input[name="set"]' => ['value' => 'new'],
113           ],
114         ],
115         '#required' => FALSE,
116       ];
117
118       if (!$account_is_user) {
119         $default_set = $this->shortcutSetStorage->getDefaultSet($this->user);
120         $form['new']['#description'] = $this->t('The new set is created by copying items from the %default set.', ['%default' => $default_set->label()]);
121       }
122
123       $form['actions'] = ['#type' => 'actions'];
124       $form['actions']['submit'] = [
125         '#type' => 'submit',
126         '#value' => $this->t('Change set'),
127       ];
128     }
129     else {
130       // There is only 1 option, so output a message in the $form array.
131       $form['info'] = [
132         '#markup' => '<p>' . $this->t('You are currently using the %set-name shortcut set.', ['%set-name' => $current_set->label()]) . '</p>',
133       ];
134     }
135
136     return $form;
137   }
138
139   /**
140    * Determines if a shortcut set exists already.
141    *
142    * @param string $id
143    *   The set ID to check.
144    *
145    * @return bool
146    *   TRUE if the shortcut set exists, FALSE otherwise.
147    */
148   public function exists($id) {
149     return (bool) $this->shortcutSetStorage->getQuery()
150       ->condition('id', $id)
151       ->execute();
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function validateForm(array &$form, FormStateInterface $form_state) {
158     if ($form_state->getValue('set') == 'new') {
159       // Check to prevent creating a shortcut set with an empty title.
160       if (trim($form_state->getValue('label')) == '') {
161         $form_state->setErrorByName('label', $this->t('The new set label is required.'));
162       }
163     }
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function submitForm(array &$form, FormStateInterface $form_state) {
170     $account = $this->currentUser();
171
172     $account_is_user = $this->user->id() == $account->id();
173     if ($form_state->getValue('set') == 'new') {
174       // Save a new shortcut set with links copied from the user's default set.
175       /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
176       $set = $this->shortcutSetStorage->create([
177         'id' => $form_state->getValue('id'),
178         'label' => $form_state->getValue('label'),
179       ]);
180       $set->save();
181       $replacements = [
182         '%user' => $this->user->label(),
183         '%set_name' => $set->label(),
184         ':switch-url' => $this->url('<current>'),
185       ];
186       if ($account_is_user) {
187         // Only administrators can create new shortcut sets, so we know they have
188         // access to switch back.
189         drupal_set_message($this->t('You are now using the new %set_name shortcut set. You can edit it from this page or <a href=":switch-url">switch back to a different one.</a>', $replacements));
190       }
191       else {
192         drupal_set_message($this->t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements));
193       }
194       $form_state->setRedirect(
195         'entity.shortcut_set.customize_form',
196         ['shortcut_set' => $set->id()]
197       );
198     }
199     else {
200       // Switch to a different shortcut set.
201       /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
202       $set = $this->shortcutSetStorage->load($form_state->getValue('set'));
203       $replacements = [
204         '%user' => $this->user->getDisplayName(),
205         '%set_name' => $set->label(),
206       ];
207       drupal_set_message($account_is_user ? $this->t('You are now using the %set_name shortcut set.', $replacements) : $this->t('%user is now using the %set_name shortcut set.', $replacements));
208     }
209
210     // Assign the shortcut set to the provided user account.
211     $this->shortcutSetStorage->assignUser($set, $this->user);
212   }
213
214   /**
215    * Checks access for the shortcut set switch form.
216    *
217    * @param \Drupal\user\UserInterface $user
218    *   (optional) The owner of the shortcut set.
219    *
220    * @return \Drupal\Core\Access\AccessResultInterface
221    *   The access result.
222    */
223   public function checkAccess(UserInterface $user = NULL) {
224     return shortcut_set_switch_access($user);
225   }
226
227 }