3 namespace Drupal\shortcut\Form;
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;
13 * Builds the shortcut set switch form.
17 class SwitchShortcutSet extends FormBase {
20 * The account the shortcut set is for.
22 * @var \Drupal\user\UserInterface
27 * The shortcut set storage.
29 * @var \Drupal\shortcut\ShortcutSetStorageInterface
31 protected $shortcutSetStorage;
34 * Constructs a SwitchShortcutSet object.
36 * @param \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_set_storage
37 * The shortcut set storage.
39 public function __construct(ShortcutSetStorageInterface $shortcut_set_storage) {
40 $this->shortcutSetStorage = $shortcut_set_storage;
46 public static function create(ContainerInterface $container) {
48 $container->get('entity.manager')->getStorage('shortcut_set')
55 public function getFormId() {
56 return 'shortcut_set_switch';
62 public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
63 $account = $this->currentUser();
67 // Prepare the list of shortcut sets.
68 $options = array_map(function (ShortcutSet $set) {
70 }, $this->shortcutSetStorage->loadMultiple());
72 $current_set = shortcut_current_displayed_set($this->user);
74 // Only administrators can add shortcut sets.
75 $add_access = $account->hasPermission('administer shortcuts');
77 $options['new'] = $this->t('New set');
80 $account_is_user = $this->user->id() == $account->id();
81 if (count($options) > 1) {
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(),
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,
96 ':input[name="set"]' => ['value' => 'new'],
99 ':input[name="set"]' => ['value' => 'new'],
104 '#type' => 'machine_name',
106 'exists' => [$this, 'exists'],
107 'replace_pattern' => '[^a-z0-9-]+',
110 // This ID could be used for menu name.
114 ':input[name="set"]' => ['value' => 'new'],
117 '#required' => FALSE,
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()]);
125 $form['actions'] = ['#type' => 'actions'];
126 $form['actions']['submit'] = [
128 '#value' => $this->t('Change set'),
132 // There is only 1 option, so output a message in the $form array.
134 '#markup' => '<p>' . $this->t('You are currently using the %set-name shortcut set.', ['%set-name' => $current_set->label()]) . '</p>',
142 * Determines if a shortcut set exists already.
145 * The set ID to check.
148 * TRUE if the shortcut set exists, FALSE otherwise.
150 public function exists($id) {
151 return (bool) $this->shortcutSetStorage->getQuery()
152 ->condition('id', $id)
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.'));
171 public function submitForm(array &$form, FormStateInterface $form_state) {
172 $account = $this->currentUser();
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'),
184 '%user' => $this->user->label(),
185 '%set_name' => $set->label(),
186 ':switch-url' => $this->url('<current>'),
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));
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));
196 $form_state->setRedirect(
197 'entity.shortcut_set.customize_form',
198 ['shortcut_set' => $set->id()]
202 // Switch to a different shortcut set.
203 /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
204 $set = $this->shortcutSetStorage->load($form_state->getValue('set'));
206 '%user' => $this->user->getDisplayName(),
207 '%set_name' => $set->label(),
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));
212 // Assign the shortcut set to the provided user account.
213 $this->shortcutSetStorage->assignUser($set, $this->user);
217 * Checks access for the shortcut set switch form.
219 * @param \Drupal\user\UserInterface $user
220 * (optional) The owner of the shortcut set.
222 * @return \Drupal\Core\Access\AccessResultInterface
225 public function checkAccess(UserInterface $user = NULL) {
226 return shortcut_set_switch_access($user);