diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutLinkAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutLinkAccessCheck.php deleted file mode 100644 index 45ade59..0000000 --- a/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutLinkAccessCheck.php +++ /dev/null @@ -1,52 +0,0 @@ -getRequirements()); - } - - /** - * {@inheritdoc} - */ - public function access(Route $route, Request $request) { - $shortcut = $request->attributes->get('shortcut'); - $operation = $route->getRequirement('_shortcut_link_access'); - - if ($shortcut) { - switch ($operation) { - case 'add': - // Sufficiently-privileged users can edit their currently displayed shortcut - // set, but not other sets. Shortcut administrators can edit any set. - if (user_access('administer shortcuts')) { - return static::ALLOW; - } - if (user_access('customize shortcut links')) { - return $shortcut == shortcut_current_displayed_set() ? static::ALLOW : static::DENY; - } - break; - } - } - - return static::DENY; - } - -} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkAdd.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkAdd.php deleted file mode 100644 index e79172c..0000000 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkAdd.php +++ /dev/null @@ -1,150 +0,0 @@ -moduleHandler = $module_handler; - $this->entityManager = $entity_manager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('module_handler'), - $container->get('plugin.manager.entity') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormID() { - return 'shortcut_link_add'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, array &$form_state, Shortcut $shortcut = NULL) { - $this->shortcut = $shortcut; - drupal_set_title(t('Add new shortcut')); - $form += $this->getFormElements(); - return $form; - } - - /** - * Implements \Drupal\Core\Form\FormInterface::validateForm(). - */ - public function validateForm(array &$form, array &$form_state) { - if (!shortcut_valid_link($form_state['values']['shortcut_link']['link_path'])) { - form_set_error('shortcut_link][link_path', t('The link must correspond to a valid path on the site.')); - } - } - - /** - * {@inheritdoc} - */ - public function submitForm(array &$form, array &$form_state) { - $this->moduleHandler->loadInclude('shortcut', 'inc', 'shortcut.admin'); - // Add the shortcut link to the set. - $shortcut_link = $form_state['values']['shortcut_link']; - $shortcut_link['menu_name'] = $this->shortcut->id(); - shortcut_admin_add_link($shortcut_link, $this->shortcut); - $this->shortcut->save(); - $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $this->shortcut->id(); - drupal_set_message(t('Added a shortcut for %title.', array('%title' => $shortcut_link['link_title']))); - } - - /** - * Helper function for building a form for adding or editing shortcut links. - * - * @param $shortcut_link - * (optional) An array representing the shortcut link that will be edited. If - * not provided, a new link will be created. - * - * @return - * An array of form elements. - */ - public function getFormElements(array $shortcut_link = array()) { - if (empty($shortcut_link)) { - $shortcut_link = $this->entityManager - ->getStorageController('menu_link') - ->create(array( - 'link_title' => '', - 'link_path' => '', - )); - } - else { - $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '') ? '' : $this->entityManager->getPathAlias($shortcut_link['link_path']); - } - - $form['shortcut_link']['#tree'] = TRUE; - $form['shortcut_link']['link_title'] = array( - '#type' => 'textfield', - '#title' => t('Name'), - '#size' => 40, - '#maxlength' => 255, - '#default_value' => $shortcut_link['link_title'], - '#required' => TRUE, - ); - - $form['shortcut_link']['link_path'] = array( - '#type' => 'textfield', - '#title' => t('Path'), - '#size' => 40, - '#maxlength' => 255, - '#field_prefix' => url(NULL, array('absolute' => TRUE)), - '#default_value' => $shortcut_link['link_path'], - ); - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - - return $form; - } - -} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php new file mode 100644 index 0000000..42761ac --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php @@ -0,0 +1,107 @@ +menuLinkStorage = $menu_link_storage; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + return new static( + $container->get('module_handler'), + $container->get('plugin.manager.entity')->getStorageController('menu_link') + ); + } + + /** + * {@inheritdoc} + */ + public function form(array $form, array &$form_state) { + drupal_set_title(t('Add new shortcut')); + $shortcut_link = $this->menuLinkStorage->create(array( + 'link_title' => '', + 'link_path' => '', + )); + + $form['shortcut_link']['#tree'] = TRUE; + $form['shortcut_link']['link_title'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#size' => 40, + '#maxlength' => 255, + '#default_value' => $shortcut_link['link_title'], + '#required' => TRUE, + ); + + $form['shortcut_link']['link_path'] = array( + '#type' => 'textfield', + '#title' => t('Path'), + '#size' => 40, + '#maxlength' => 255, + '#field_prefix' => url(NULL, array('absolute' => TRUE)), + '#default_value' => $shortcut_link['link_path'], + ); + + return parent::form($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validate(array $form, array &$form_state) { + if (!shortcut_valid_link($form_state['values']['shortcut_link']['link_path'])) { + form_set_error('shortcut_link][link_path', t('The link must correspond to a valid path on the site.')); + } + } + + /** + * {@inheritdoc} + */ + public function save(array $form, array &$form_state) { + $this->moduleHandler->loadInclude('shortcut', 'inc', 'shortcut.admin'); + // Add the shortcut link to the set. + $shortcut_link = $form_state['values']['shortcut_link']; + $shortcut_link['menu_name'] = $this->entity->id(); + shortcut_admin_add_link($shortcut_link, $this->entity); + $this->entity->save(); + $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $this->entity->id(); + drupal_set_message(t('Added a shortcut for %title.', array('%title' => $shortcut_link['link_title']))); + } + + /** + * {@inheritdoc} + */ + protected function actions(array $form, array &$form_state) { + $actions = parent::actions($form, $form_state); + unset($actions['delete']); + return $actions; + } + +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/ShortcutSet.php b/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/ShortcutSet.php index d55c61b..14a50a8 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/ShortcutSet.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Plugin/Core/Entity/ShortcutSet.php @@ -28,6 +28,7 @@ * "default" = "Drupal\shortcut\ShortcutSetFormController", * "edit" = "Drupal\shortcut\ShortcutSetFormController", * "customize" = "Drupal\shortcut\Form\SetCustomize", + * "add_link" = "Drupal\shortcut\Form\ShortcutLinkAddForm", * "delete" = "Drupal\shortcut\Form\ShortcutSetDeleteForm" * } * }, diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetAccessController.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetAccessController.php index 3c56353..6a15859 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetAccessController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetAccessController.php @@ -40,4 +40,13 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A } } + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + if ($account->hasPermission('administer shortcuts') || $account->hasPermission('customize shortcut links')) { + return TRUE; + } + } + } diff --git a/core/modules/shortcut/shortcut.admin.inc b/core/modules/shortcut/shortcut.admin.inc index ee41c7b..f863c37 100644 --- a/core/modules/shortcut/shortcut.admin.inc +++ b/core/modules/shortcut/shortcut.admin.inc @@ -283,7 +283,7 @@ function shortcut_link_edit_submit($form, &$form_state) { /** * Adds a link to the end of a shortcut set, keeping within a prescribed limit. * - * @param $shortcut_link + * @param $link * An array representing a shortcut link. * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut * An object representing the shortcut set which the link will be added to. @@ -293,7 +293,7 @@ function shortcut_link_edit_submit($form, &$form_state) { */ function shortcut_admin_add_link($shortcut_link, &$shortcut_set) { // Normalize the path in case it is an alias. - $shortcut_link['link_path'] = \Drupal::service('path.alias_manager')->getSystemPath($shortcut_link['link_path']); + $shortcut_link['link_path'] = Drupal::service('path.alias_manager')->getSystemPath($shortcut_link['link_path']); if (empty($shortcut_link['link_path'])) { $shortcut_link['link_path'] = ''; } diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 3a10b04..f340760 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -6,6 +6,7 @@ */ use Drupal\shortcut\Plugin\Core\Entity\Shortcut; +use Drupal\shortcut\ShortcutSetInterface; /** * Implements hook_help(). @@ -157,24 +158,16 @@ function shortcut_admin_paths() { /** * Access callback for editing a shortcut set. * - * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut - * (optional) The shortcut set to be edited. If not set, the current user's - * shortcut set will be used. + * @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set + * The shortcut set to be edited. If not set, the current user's shortcut set + * will be used. * - * @return + * @return bool * TRUE if the current user has access to edit the shortcut set, FALSE * otherwise. */ -function shortcut_set_edit_access($shortcut_set = NULL) { - // Sufficiently-privileged users can edit their currently displayed shortcut - // set, but not other sets. Shortcut administrators can edit any set. - if (user_access('administer shortcuts')) { - return TRUE; - } - if (user_access('customize shortcut links')) { - return !isset($shortcut_set) || $shortcut_set == shortcut_current_displayed_set(); - } - return FALSE; +function shortcut_set_edit_access(ShortcutSetInterface $shortcut_set) { + return $shortcut_set->access('update'); } /** @@ -451,7 +444,7 @@ function shortcut_preprocess_page(&$variables) { // shortcuts and if the page's actual content is being shown (for example, // we do not want to display it on "access denied" or "page not found" // pages). - if (shortcut_set_edit_access() && ($item = menu_get_item()) && $item['access']) { + if (entity_page_create_access('shortcut_set') && ($item = menu_get_item()) && $item['access']) { $link = current_path(); $query_parameters = drupal_get_query_parameters(); if (!empty($query_parameters)) { diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index afc8e78..8b676af 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -34,11 +34,11 @@ shortcut_set_edit: _entity_access: 'shortcut_set.update' shortcut_link_add: - pattern: '/admin/config/user-interface/shortcut/manage/{shortcut}/add-link' + pattern: '/admin/config/user-interface/shortcut/manage/{shortcut_set}/add-link' defaults: - _form: 'Drupal\shortcut\Form\LinkAdd' + _entity_form: 'shortcut_set.add_link' requirements: - _shortcut_link_access: 'add' + _entity_access: 'shortcut_set.update' shortcut_link_add_inline: pattern: '/admin/config/user-interface/shortcut/manage/{shortcut_set}/add-link-inline' diff --git a/core/modules/shortcut/shortcut.services.yml b/core/modules/shortcut/shortcut.services.yml index 57482f1..bb95f49 100644 --- a/core/modules/shortcut/shortcut.services.yml +++ b/core/modules/shortcut/shortcut.services.yml @@ -3,8 +3,3 @@ services: class: Drupal\shortcut\Access\LinkDeleteAccessCheck tags: - { name: access_check } - access_check.shortcut_link: - class: Drupal\shortcut\Access\ShortcutLinkAccessCheck - tags: - - { name: access_check } -