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