diff --git a/core/core.services.yml b/core/core.services.yml index 1b0d520..65d071f 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -616,7 +616,6 @@ services: factory_method: authenticate factory_service: authentication arguments: ['@request'] - scope: request asset.css.collection_renderer: class: Drupal\Core\Asset\CssCollectionRenderer asset.css.collection_optimizer: diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutSetSwitchAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/SwitchShortcutSetAccessCheck.php similarity index 83% rename from core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutSetSwitchAccessCheck.php rename to core/modules/shortcut/lib/Drupal/shortcut/Access/SwitchShortcutSetAccessCheck.php index aacecc4..5046f77 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutSetSwitchAccessCheck.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Access/SwitchShortcutSetAccessCheck.php @@ -2,19 +2,19 @@ /** * @file - * Contains Drupal\shortcut\Access\ShortcutSetSwitchAccessCheck. + * Contains \Drupal\shortcut\Access\SwitchShortcutSetAccessCheck. */ namespace Drupal\shortcut\Access; use Drupal\Core\Access\StaticAccessCheckInterface; -use Symfony\Component\Routing\Route; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Route; /** - * Provides an access check for shortcut link delete routes. + * Defines a access checker for switching the shortcut set of a user account. */ -class ShortcutSetSwitchAccessCheck implements StaticAccessCheckInterface { +class SwitchShortcutSetAccessCheck implements StaticAccessCheckInterface { /** * {@inheritdoc} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutForm.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutForm.php index fc5fec2..3cf97ff 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutForm.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutForm.php @@ -35,14 +35,4 @@ public function add(ShortcutSetInterface $shortcut_set) { return drupal_get_form('shortcut_link_add', $shortcut_set); } - /** - * Wraps shortcut_set_switch(). - * - * @todo Remove shortcut_set_switch(). - */ - public function overview(UserInterface $user) { - module_load_include('admin.inc', 'shortcut'); - return drupal_get_form('shortcut_set_switch', $user); - } - } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php new file mode 100644 index 0000000..05bf9af --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php @@ -0,0 +1,237 @@ +storageController = $shortcut_set_storage_controller; + $this->queryFactory = $query_factory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorageController('shortcut_set'), + $container->get('entity.query') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'shortcut_set_switch'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state, UserInterface $user = NULL) { + $account = $this->currentUser(); + + $this->user = $user; + + // Prepare the list of shortcut sets. + $options = array_map(function ($set) { + return String::checkPlain($set->label()); + }, $this->storageController->loadMultiple()); + + $current_set = shortcut_current_displayed_set($this->user); + + // Only administrators can add shortcut sets. + $add_access = $account->hasPermission('administer shortcuts'); + if ($add_access) { + $options['new'] = $this->t('New set'); + } + + $account_is_user = $this->user->id() == $account->id(); + if (count($options) > 1) { + $form['set'] = array( + '#type' => 'radios', + '#title' => $account_is_user ? $this->t('Choose a set of shortcuts to use') : $this->t('Choose a set of shortcuts for this user'), + '#options' => $options, + '#default_value' => $current_set->id(), + ); + + $form['label'] = array( + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#title_display' => 'invisible', + '#description' => $this->t('The new set is created by copying items from your default shortcut set.'), + '#access' => $add_access, + ); + $form['id'] = array( + '#type' => 'machine_name', + '#machine_name' => array( + 'exists' => array($this, 'exists'), + 'replace_pattern' => '[^a-z0-9-]+', + 'replace' => '-', + ), + // This ID could be used for menu name. + '#maxlength' => 23, + '#states' => array( + 'required' => array( + ':input[name="set"]' => array('value' => 'new'), + ), + ), + '#required' => FALSE, + ); + + if (!$account_is_user) { + $default_set = $this->storageController->getDefaultSet($this->user); + $form['new']['#description'] = $this->t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label())); + } + + $form['#attached'] = array( + 'library' => array(array('shortcut', 'drupal.shortcut.admin')), + ); + + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Change set'), + ); + } + else { + // There is only 1 option, so output a message in the $form array. + $form['info'] = array( + '#markup' => '

' . $this->t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '

', + ); + } + + return $form; + } + + /** + * {@inheritdoc} + */ + 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']) == '') { + form_set_error('new', $this->t('The new set label is required.')); + } + // Check to prevent a duplicate title. + if (shortcut_set_title_exists($form_state['values']['label'])) { + form_set_error('label', $this->t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['label']))); + } + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $account = $this->currentUser(); + + $account_is_user = $this->user->id() == $account->id(); + if ($form_state['values']['set'] == 'new') { + // Save a new shortcut set with links copied from the user's default set. + $default_set = $this->storageController->getDefaultSet($this->user); + $set = $this->storageController->create(array( + 'id' => $form_state['values']['id'], + 'label' => $form_state['values']['label'], + 'links' => $default_set->links, + )); + $set->save(); + $replacements = array( + '%user' => $this->user->label(), + '%set_name' => $set->label(), + // @todo Convert once https://drupal.org/node/2073813 is in. + '@switch-url' => \Drupal::urlGenerator()->generateFromPath($this->getRequest()->attributes->get('_system_path')), + ); + if ($account_is_user) { + // Only administrators can create new shortcut sets, so we know they have + // access to switch back. + drupal_set_message($this->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)); + } + else { + drupal_set_message($this->t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements)); + } + $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set->id(); + } + else { + // Switch to a different shortcut set. + $set = $this->storageController->load($form_state['values']['set']); + $replacements = array( + '%user' => $this->user->label(), + '%set_name' => $set->label(), + ); + 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)); + } + + // Assign the shortcut set to the provided user account. + $this->storageController->assignUser($set, $this->user); + } + + /** + * 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) { + return (bool) $this->queryFactory + ->get('shortcut_set') + ->condition('id', $id) + ->execute(); + } + +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php index 2f17850..c34e4e2 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageController.php @@ -7,8 +7,14 @@ namespace Drupal\shortcut; +use Drupal\Component\Uuid\UuidInterface; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Config\Entity\ConfigStorageController; -use Drupal\shortcut\ShortcutSetInterface; +use Drupal\Core\Config\StorageInterface; +use Drupal\Core\Entity\Query\QueryFactory; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Session\AccountInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines a storage controller for shortcut entities. @@ -16,6 +22,52 @@ class ShortcutSetStorageController extends ConfigStorageController implements ShortcutSetStorageControllerInterface { /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * Constructs a ShortcutStorageController object. + * + * @param string $entity_type + * The entity type for which the instance is created. + * @param array $entity_info + * An array of entity info for the entity type. + * @param \Drupal\Core\Config\ConfigFactory $config_factory + * The config factory service. + * @param \Drupal\Core\Config\StorageInterface $config_storage + * The config storage service. + * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query_factory + * The entity query factory. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The UUID service. + */ + public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory, StorageInterface $config_storage, QueryFactory $entity_query_factory, ModuleHandlerInterface $module_handler, UuidInterface $uuid_service) { + parent::__construct($entity_type, $entity_info, $config_factory, $config_storage, $entity_query_factory, $uuid_service); + + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + return new static( + $entity_type, + $entity_info, + $container->get('config.factory'), + $container->get('config.storage'), + $container->get('entity.query'), + $container->get('module_handler'), + $container->get('uuid') + ); + } + + /** * Overrides \Drupal\config\ConfigStorageController::attachLoad(). */ protected function attachLoad(&$queried_entities, $revision_id = FALSE) { @@ -77,4 +129,24 @@ public function getAssignedToUser($account) { public function countAssignedUsers(ShortcutSetInterface $shortcut_set) { return db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', array(':name' => $shortcut_set->id()))->fetchField(); } + + /** + * {@inheritdoc} + */ + public function getDefaultSet(AccountInterface $account) { + // Allow modules to return a default shortcut set name. Since we can only + // have one, we allow the last module which returns a valid result to take + // precedence. If no module returns a valid set, fall back on the site-wide + // default, which is the lowest-numbered shortcut set. + $suggestions = array_reverse($this->moduleHandler->invokeAll('shortcut_default_set', $account)); + $suggestions[] = 'default'; + foreach ($suggestions as $name) { + if ($shortcut_set = $this->load($name)) { + break; + } + } + + return $shortcut_set; + } + } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageControllerInterface.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageControllerInterface.php index b4e9d97..ab1e1eb 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageControllerInterface.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetStorageControllerInterface.php @@ -8,6 +8,7 @@ namespace Drupal\shortcut; use Drupal\Core\Entity\EntityStorageControllerInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\shortcut\ShortcutSetInterface; /** @@ -69,4 +70,16 @@ public function getAssignedToUser($account); * The number of users who have this set assigned to them. */ public function countAssignedUsers(ShortcutSetInterface $shortcut_set); + + /** + * Gets the default shortcut set for a given user account. + * + * @param \Drupal\Core\Session\AccountInterface $account + * The user account whose default shortcut set will be returned. + * + * @return \Drupal\shortcut\ShortcutSetInterface + * An object representing the default shortcut set. + */ + public function getDefaultSet(AccountInterface $account); + } diff --git a/core/modules/shortcut/shortcut.admin.inc b/core/modules/shortcut/shortcut.admin.inc index 2d6b49d..41d7cbe 100644 --- a/core/modules/shortcut/shortcut.admin.inc +++ b/core/modules/shortcut/shortcut.admin.inc @@ -5,175 +5,6 @@ * Administrative page callbacks for the shortcut module. */ -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; - -/** - * Form callback: builds the form for switching shortcut sets. - * - * @param $form - * An associative array containing the structure of the form. - * @param $form_state - * An associative array containing the current state of the form. - * @param $account - * (optional) The user account whose shortcuts will be switched. Defaults to - * the current logged-in user. - * - * @return - * An array representing the form definition. - * - * @ingroup forms - * @see shortcut_set_switch_validate() - * @see shortcut_set_switch_submit() - * - * @deprecated Use \Drupal\shortcut\Form\ShortcutForm::overview() - */ -function shortcut_set_switch($form, &$form_state, $account = NULL) { - $user = \Drupal::currentUser(); - - if (!isset($account)) { - $account = $user; - } - - // Prepare the list of shortcut sets. - $sets = entity_load_multiple('shortcut_set'); - $current_set = shortcut_current_displayed_set($account); - - $options = array(); - foreach ($sets as $name => $set) { - $options[$name] = check_plain($set->label()); - } - - // Only administrators can add shortcut sets. - $add_access = $user->hasPermission('administer shortcuts'); - if ($add_access) { - $options['new'] = t('New set'); - } - - if (count($options) > 1) { - $form['account'] = array( - '#type' => 'value', - '#value' => $account, - ); - - $form['set'] = array( - '#type' => 'radios', - '#title' => $user->id() == $account->id() ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'), - '#options' => $options, - '#default_value' => $current_set->id(), - ); - - $form['label'] = array( - '#type' => 'textfield', - '#title' => t('Label'), - '#title_display' => 'invisible', - '#description' => t('The new set is created by copying items from your default shortcut set.'), - '#access' => $add_access, - ); - $form['id'] = array( - '#type' => 'machine_name', - '#machine_name' => array( - 'exists' => 'shortcut_set_load', - 'source' => array('label'), - 'replace_pattern' => '[^a-z0-9-]+', - 'replace' => '-', - ), - // This id could be used for menu name. - '#maxlength' => 23, - '#states' => array( - 'required' => array( - ':input[name="set"]' => array('value' => 'new'), - ), - ), - '#required' => FALSE, - ); - - if ($user->id() != $account->id()) { - $default_set = shortcut_default_set($account); - $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label())); - } - - $form['#attached'] = array( - 'library' => array(array('shortcut', 'drupal.shortcut.admin')), - ); - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Change set'), - ); - } - else { - // There is only 1 option, so output a message in the $form array. - $form['info'] = array( - '#markup' => '

' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '

', - ); - } - - return $form; -} - -/** - * Validation handler for shortcut_set_switch(). - */ -function shortcut_set_switch_validate($form, &$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']) == '') { - form_set_error('new', t('The new set label is required.')); - } - // Check to prevent a duplicate title. - if (shortcut_set_title_exists($form_state['values']['label'])) { - form_set_error('label', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['label']))); - } - } -} - -/** - * Submit handler for shortcut_set_switch(). - */ -function shortcut_set_switch_submit($form, &$form_state) { - global $user; - $account = $form_state['values']['account']; - - 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('shortcut_set', array( - 'id' => $form_state['values']['id'], - 'label' => $form_state['values']['label'], - 'links' => $default_set->links, - )); - $set->save(); - $replacements = array( - '%user' => $account->getUsername(), - '%set_name' => $set->label(), - '@switch-url' => url(current_path()), - ); - if ($account->id() == $user->id()) { - // 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)); - } - else { - drupal_set_message(t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements)); - } - $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set->id(); - } - else { - // Switch to a different shortcut set. - $set = shortcut_set_load($form_state['values']['set']); - $replacements = array( - '%user' => $account->getUsername(), - '%set_name' => $set->label(), - ); - drupal_set_message($account->id() == $user->id() ? 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); -} - /** * Form callback: builds the form for adding a new shortcut link. * diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 8ca0472..4d14b0a 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -126,7 +126,7 @@ function shortcut_menu() { ); $items['user/%user/shortcuts'] = array( 'title' => 'Shortcuts', - 'route_name' => 'shortcut.overview', + 'route_name' => 'shortcut.set_switch', 'type' => MENU_LOCAL_TASK, ); diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index 15549fd..2c118e3 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -34,6 +34,17 @@ shortcut.set_edit: requirements: _entity_access: 'shortcut_set.update' +shortcut.set_switch: + path: '/user/{user}/shortcuts' + defaults: + _form: 'Drupal\shortcut\Form\SwitchShortcutSet' + _title: 'Shortcuts' + options: + _access_mode: 'ALL' + requirements: + _permission: 'administer shortcuts' + _access_shortcut_set_switch: 'TRUE' + shortcut.link_add_inline: path: '/admin/config/user-interface/shortcut/manage/{shortcut_set}/add-link-inline' defaults: @@ -64,11 +75,3 @@ shortcut.link_edit: requirements: _access_shortcut_link: 'TRUE' -shortcut.overview: - path: 'user/{user}/shortcuts' - defaults: - _content: '\Drupal\shortcut\Form\ShortcutForm::overview' - _title: 'Shortcuts' - requirements: - _access_shortcut_set_switch: 'TRUE' - diff --git a/core/modules/shortcut/shortcut.services.yml b/core/modules/shortcut/shortcut.services.yml index 439a0e0..c5a4bc5 100644 --- a/core/modules/shortcut/shortcut.services.yml +++ b/core/modules/shortcut/shortcut.services.yml @@ -9,7 +9,7 @@ services: tags: - { name: access_check } - access_check.shortcut.shortcut_set_switch: - class: Drupal\shortcut\Access\ShortcutSetSwitchAccessCheck + access_check.shortcut.set_switch: + class: Drupal\shortcut\Access\SwitchShortcutSetAccessCheck tags: - { name: access_check }