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.
15 class SwitchShortcutSet extends FormBase {
18 * The account the shortcut set is for.
20 * @var \Drupal\user\UserInterface
25 * The shortcut set storage.
27 * @var \Drupal\shortcut\ShortcutSetStorageInterface
29 protected $shortcutSetStorage;
32 * Constructs a SwitchShortcutSet object.
34 * @param \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_set_storage
35 * The shortcut set storage.
37 public function __construct(ShortcutSetStorageInterface $shortcut_set_storage) {
38 $this->shortcutSetStorage = $shortcut_set_storage;
44 public static function create(ContainerInterface $container) {
46 $container->get('entity.manager')->getStorage('shortcut_set')
53 public function getFormId() {
54 return 'shortcut_set_switch';
60 public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
61 $account = $this->currentUser();
65 // Prepare the list of shortcut sets.
66 $options = array_map(function (ShortcutSet $set) {
68 }, $this->shortcutSetStorage->loadMultiple());
70 $current_set = shortcut_current_displayed_set($this->user);
72 // Only administrators can add shortcut sets.
73 $add_access = $account->hasPermission('administer shortcuts');
75 $options['new'] = $this->t('New set');
78 $account_is_user = $this->user->id() == $account->id();
79 if (count($options) > 1) {
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(),
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,
94 ':input[name="set"]' => ['value' => 'new'],
97 ':input[name="set"]' => ['value' => 'new'],
102 '#type' => 'machine_name',
104 'exists' => [$this, 'exists'],
105 'replace_pattern' => '[^a-z0-9-]+',
108 // This ID could be used for menu name.
112 ':input[name="set"]' => ['value' => 'new'],
115 '#required' => FALSE,
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()]);
123 $form['actions'] = ['#type' => 'actions'];
124 $form['actions']['submit'] = [
126 '#value' => $this->t('Change set'),
130 // There is only 1 option, so output a message in the $form array.
132 '#markup' => '<p>' . $this->t('You are currently using the %set-name shortcut set.', ['%set-name' => $current_set->label()]) . '</p>',
140 * Determines if a shortcut set exists already.
143 * The set ID to check.
146 * TRUE if the shortcut set exists, FALSE otherwise.
148 public function exists($id) {
149 return (bool) $this->shortcutSetStorage->getQuery()
150 ->condition('id', $id)
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.'));
169 public function submitForm(array &$form, FormStateInterface $form_state) {
170 $account = $this->currentUser();
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'),
182 '%user' => $this->user->label(),
183 '%set_name' => $set->label(),
184 ':switch-url' => $this->url('<current>'),
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));
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));
194 $form_state->setRedirect(
195 'entity.shortcut_set.customize_form',
196 ['shortcut_set' => $set->id()]
200 // Switch to a different shortcut set.
201 /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
202 $set = $this->shortcutSetStorage->load($form_state->getValue('set'));
204 '%user' => $this->user->getDisplayName(),
205 '%set_name' => $set->label(),
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));
210 // Assign the shortcut set to the provided user account.
211 $this->shortcutSetStorage->assignUser($set, $this->user);
215 * Checks access for the shortcut set switch form.
217 * @param \Drupal\user\UserInterface $user
218 * (optional) The owner of the shortcut set.
220 * @return \Drupal\Core\Access\AccessResultInterface
223 public function checkAccess(UserInterface $user = NULL) {
224 return shortcut_set_switch_access($user);