diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php index f9d1738..333e9f9 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php @@ -7,13 +7,12 @@ namespace Drupal\shortcut\Form; -use Drupal\Core\ControllerInterface; +use Drupal\Core\Controller\ControllerInterface; use Drupal\Core\Entity\EntityManager; -use Drupal\Form\FormInterface; -use Drupal\menu_link\MenuLinkStorageContainer; -use Drupal\shortcut\Plugin\Core\Entity\shortcut; -use Drupal\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Form\FormInterface; use Drupal\Component\Utility\String; +use Drupal\user\UserInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -22,11 +21,25 @@ class SetSwitch implements ControllerInterface, FormInterface { /** - * The entity manager. + * The account the shortcut set is for. + * + * @var \Drupal\user\UserInterface + */ + protected $account; + + /** + * The request object. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * The shortcut storage controller. * - * @var \Drupal\Core\Entity\EntityManager + * @var \Drupal\Core\Entity\EntityStorageControllerInterface */ - protected $entityManager; + protected $storageController; /** * Constructs a SetSwitch object. @@ -35,7 +48,7 @@ class SetSwitch implements ControllerInterface, FormInterface { * The entity manager. */ public function __construct(EntityManager $entity_manager) { - $this->entityManager = $entity_manager; + $this->storageController = $entity_manager->getStorageController('shortcut'); } /** @@ -43,7 +56,7 @@ public function __construct(EntityManager $entity_manager) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('plugin.manager.entity')->get('request') + $container->get('plugin.manager.entity') ); } @@ -57,21 +70,15 @@ public function getFormID() { /** * {@inheritdoc} */ - public function buildForm(array $form, array &$form_state, Request $request = NULL) { + public function buildForm(array $form, array &$form_state, UserInterface $account = NULL, Request $request = NULL) { global $user; - if (!isset($account)) { - $account = $user; - } + $this->request = $request; + $this->account = $account; // Prepare the list of shortcut sets. - $sets = $this->entityManager->getStorageController('shortcut')->load(); - $sets = entity_load_multiple('shortcut'); - $current_set = shortcut_current_displayed_set($account); - - $options = array(); - foreach ($sets as $name => $set) { - $options[$name] = String::check_plain($set->label()); - } + $options = array_map(function ($set) { + return String::checkPlain($set->label()); + }, $this->storageController->load()); // Only administrators can add shortcut sets. $add_access = user_access('administer shortcuts'); @@ -79,20 +86,15 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $options['new'] = t('New set'); } + $account_is_user = $this->account->id() == $user->uid; if (count($options) > 1) { - $form['account'] = array( - '#type' => 'value', - '#value' => $account, - ); - $form['set'] = array( '#type' => 'radios', - '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'), + '#title' => $account_is_user ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'), '#options' => $options, - '#default_value' => $current_set->id(), + '#default_value' => shortcut_current_displayed_set($this->account)->id(), ); - $form['label'] = array( '#type' => 'textfield', '#title' => t('Label'), @@ -103,12 +105,11 @@ public function buildForm(array $form, array &$form_state, Request $request = NU $form['id'] = array( '#type' => 'machine_name', '#machine_name' => array( - 'exists' => 'shortcut_set_load', - 'source' => array('label'), + 'exists' => array($this, 'exists'), 'replace_pattern' => '[^a-z0-9-]+', 'replace' => '-', ), - // This id could be used for menu name. + // This ID could be used for menu name. '#maxlength' => 23, '#states' => array( 'required' => array( @@ -118,8 +119,8 @@ public function buildForm(array $form, array &$form_state, Request $request = NU '#required' => FALSE, ); - if ($user->uid != $account->uid) { - $default_set = shortcut_default_set($account); + if (!$account_is_user) { + $default_set = shortcut_default_set($this->account); $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label())); } @@ -144,9 +145,9 @@ public function buildForm(array $form, array &$form_state, Request $request = NU } /** - * Validation handler for shortcut_set_switch(). + * {@inheritdoc} */ -function validateForm(array $form, array &$form_state) { + public function validateForm(array &$form, array &$form_state) { if ($form_state['values']['set'] == 'new') { // Check to prevent creating a shortcut set with an empty title. if (trim($form_state['values']['label']) == '') { @@ -160,28 +161,27 @@ function validateForm(array $form, array &$form_state) { } /** - * Submit handler for shortcut_set_switch(). + * {@inheritdoc} */ -function submitForm(array $form, array &$form_state) { + public function submitForm(array &$form, array &$form_state) { global $user; - $this->currentPath = $request->attributes->get('system_path'); - $account = $form_state['values']['account']; + $account_is_user = $this->account->id() == $user->uid; if ($form_state['values']['set'] == 'new') { // Save a new shortcut set with links copied from the user's default set. - $default_set = shortcut_default_set($account); - $set = entity_create($this->entityType, array( + $default_set = shortcut_default_set($this->account); + $set = $this->storageController->create(array( 'id' => $form_state['values']['id'], 'label' => $form_state['values']['label'], 'links' => $default_set->links, )); $set->save(); $replacements = array( - '%user' => $account->name, + '%user' => $this->account->label(), '%set_name' => $set->label(), - '@switch-url' => url($this->currentPath), + '@switch-url' => url($this->request->attributes->get('system_path')), ); - if ($account->uid == $user->uid) { + if ($account_is_user) { // Only administrators can create new shortcut sets, so we know they have // access to switch back. drupal_set_message(t('You are now using the new %set_name shortcut set. You can edit it from this page or switch back to a different one.', $replacements)); @@ -193,15 +193,31 @@ function submitForm(array $form, array &$form_state) { } else { // Switch to a different shortcut set. - $set = shortcut_set_load($form_state['values']['set']); + $sets = $this->storageController->load(array($form_state['values']['set'])); + $set = reset($sets); $replacements = array( - '%user' => $account->name, + '%user' => $this->account->label(), '%set_name' => $set->label(), ); - drupal_set_message($account->uid == $user->uid ? t('You are now using the %set_name shortcut set.', $replacements) : t('%user is now using the %set_name shortcut set.', $replacements)); + drupal_set_message($account_is_user ? t('You are now using the %set_name shortcut set.', $replacements) : t('%user is now using the %set_name shortcut set.', $replacements)); } // Assign the shortcut set to the provided user account. - shortcut_set_assign_user($set, $account); + shortcut_set_assign_user($set, $this->account); + } + + /** + * Determines if a shortcut set exists already. + * + * @param string $id + * The set ID to check. + * + * @return bool + * TRUE if the shortcut set exists, FALSE otherwise. + */ + public function exists($id) { + $sets = $this->storageController->load(array($id)); + return !empty($sets[$id]); } + } diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index 5a63009..b7fb77c 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -26,8 +26,11 @@ shortcut_set_edit: _entity_access: 'shortcut.edit' shortcut_set_switch: - pattern: '/user/{user}/shortcuts' + pattern: '/user/{account}/shortcuts' defaults: _form: 'Drupal\shortcut\Form\SetSwitch' + options: + converters: + account: 'user' requirements: - _entity_access: 'shortcut.switch' + _access: 'TRUE'