diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutLinkUpdateAccessCheck.php similarity index 72% rename from core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php rename to core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutLinkUpdateAccessCheck.php index 6fad35c..c1ce742 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutLinkUpdateAccessCheck.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\shortcut\Access\LinkDeleteAccessCheck. + * Contains Drupal\shortcut\Access\ShortcutLinkUpdateAccessCheck. */ namespace Drupal\shortcut\Access; @@ -14,13 +14,13 @@ /** * Provides an access check for shortcut link delete routes. */ -class LinkDeleteAccessCheck implements StaticAccessCheckInterface { +class ShortcutLinkUpdateAccessCheck implements StaticAccessCheckInterface { /** * {@inheritdoc} */ public function appliesTo() { - return array('_access_shortcut_link_delete'); + return array('_access_shortcut_link_update'); } /** @@ -30,7 +30,7 @@ public function access(Route $route, Request $request) { $menu_link = $request->attributes->get('menu_link'); $set_name = str_replace('shortcut-', '', $menu_link['menu_name']); if ($shortcut_set = shortcut_set_load($set_name)) { - return shortcut_set_edit_access($shortcut_set); + return $shortcut_set->access('update'); } } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php b/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php index a67b53b..0f4c088 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php @@ -94,7 +94,7 @@ public function addShortcutLinkInline(ShortcutSetInterface $shortcut_set, Reques 'link_path' => $link, ); $this->moduleHandler->loadInclude('shortcut', 'admin.inc'); - shortcut_admin_add_link($link, $shortcut_set); + $shortcut_set->addLink($link); if ($shortcut_set->save() == SAVED_UPDATED) { drupal_set_message(t('Added a shortcut for %title.', array('%title' => $link['link_title']))); } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php index 42761ac..89184f8 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkAddForm.php @@ -7,8 +7,6 @@ namespace Drupal\shortcut\Form; -use Drupal\Core\Entity\EntityControllerInterface; -use Drupal\Core\Entity\EntityFormController; use Drupal\menu_link\MenuLinkStorageControllerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -16,7 +14,7 @@ /** * Builds the shortcut link add form. */ -class ShortcutLinkAddForm extends EntityFormController implements EntityControllerInterface { +class ShortcutLinkAddForm extends ShortcutLinkFormBase { /** * The menu link storage. @@ -25,6 +23,11 @@ class ShortcutLinkAddForm extends EntityFormController implements EntityControll */ protected $menuLinkStorage; + /** + * @var \Drupal\shortcut\ShortcutSetInterface + */ + protected $entity; + public function __construct(ModuleHandlerInterface $module_handler, MenuLinkStorageControllerInterface $menu_link_storage) { parent::__construct($module_handler); $this->menuLinkStorage = $menu_link_storage; @@ -43,54 +46,13 @@ public static function createInstance(ContainerInterface $container, $entity_typ /** * {@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(); + $this->entity + ->addLink($shortcut_link) + ->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']))); } @@ -104,4 +66,14 @@ protected function actions(array $form, array &$form_state) { return $actions; } + /** + * {@inheritdoc} + */ + protected function getShortcutLink() { + drupal_set_title(t('Add new shortcut')); + return $this->menuLinkStorage->create(array( + 'link_title' => '', + 'link_path' => '', + )); + } } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkEditForm.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkEditForm.php new file mode 100644 index 0000000..f506298 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkEditForm.php @@ -0,0 +1,88 @@ +aliasManager = $alias_manager; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + return new static( + $container->get('module_handler'), + $container->get('path.alias_manager') + ); + } + + /** + * {@inheritdoc} + */ + protected function getShortcutLink() { + $shortcut_link = clone $this->entity; + drupal_set_title(t('Editing @shortcut', array('@shortcut' => $shortcut_link['link_title']))); + $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '') ? '' : $this->aliasManager->getPathAlias($shortcut_link['link_path']); + return $shortcut_link; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, array &$form_state) { + $shortcut_link = $form_state['values']['shortcut_link']; + + // Normalize the path in case it is an alias. + $shortcut_path = $this->aliasManager->getSystemPath($shortcut_link['link_path']); + if (empty($shortcut_path)) { + $shortcut_path = ''; + } + $shortcut_link['link_path'] = $shortcut_path; + + foreach ($shortcut_link as $key => $value) { + $this->entity[$key] = $value; + } + + $this->entity->save(); + $set_name = str_replace('shortcut-', '' , $this->entity['menu_name']); + $form_state['redirect'] = "admin/config/user-interface/shortcut/manage/$set_name"; + drupal_set_message(t('The shortcut %link has been updated.', array('%link' => $this->entity['link_title']))); + } + + /** + * {@inheritdoc} + */ + public function delete(array $form, array &$form_state) { + $form_state['redirect'] = 'admin/config/user-interface/shortcut/link/' . $this->entity->id() . '/delete'; + } + +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkFormBase.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkFormBase.php new file mode 100644 index 0000000..ca46aaa --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/ShortcutLinkFormBase.php @@ -0,0 +1,63 @@ +getShortcutLink(); + + $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.')); + } + } + + /** + * Gets the shortcut link for this form. + * + * @return \Drupal\menu_link\MenuLinkInterface + * The shortcut link object. + */ + abstract protected function getShortcutLink(); + +} 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 14a50a8..07640f5 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 @@ -144,4 +144,34 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr } } + + /** + * {@inheritdoc} + */ + public function addLink(array $shortcut_link) { + // Normalize the path in case it is an alias. + $shortcut_link['link_path'] = \Drupal::service('path.alias_manager')->getSystemPath($shortcut_link['link_path']); + if (empty($shortcut_link['link_path'])) { + $shortcut_link['link_path'] = ''; + } + $menu_link = \Drupal::entityManager()->getStorageController('menu_link')->create($shortcut_link); + $menu_link->save(); + + // Add the link to the end of the list. + $this->links[$menu_link->uuid()] = $menu_link; + $this->resetLinkWeights(); + return $this; + } + + /** + * {@inheritdoc} + */ + public function resetLinkWeights() { + $weight = -50; + foreach ($this->links as $menu_link) { + $menu_link->weight = ++$weight; + } + return $this; + } + } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetInterface.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetInterface.php index d2849fc..d3ba64b 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetInterface.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutSetInterface.php @@ -14,4 +14,27 @@ */ interface ShortcutSetInterface extends ConfigEntityInterface { + /** + * Adds a link to the end of a shortcut set, keeping within a prescribed limit. + * + * @param array $shortcut_link + * An array representing a shortcut link. + * + * @return self + * The shortcut set. + */ + public function addLink(array $shortcut_link); + + /** + * Resets the link weights in a shortcut set to match their current order. + * + * This function can be used, for example, when a new shortcut link is added + * to the set. If the link is added to the end of the array and this function + * is called, it will force that link to display at the end of the list. + * + * @return self + * The shortcut set. + */ + public function resetLinkWeights(); + } diff --git a/core/modules/shortcut/shortcut.admin.inc b/core/modules/shortcut/shortcut.admin.inc index f863c37..be0f2ca 100644 --- a/core/modules/shortcut/shortcut.admin.inc +++ b/core/modules/shortcut/shortcut.admin.inc @@ -5,9 +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. * @@ -170,137 +167,3 @@ function shortcut_set_switch_submit($form, &$form_state) { // Assign the shortcut set to the provided user account. shortcut_set_assign_user($set, $account); } - -/** - * Form callback: builds the form for editing a shortcut link. - * - * @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 $shortcut_link - * An array representing the link that is being edited. - * - * @return - * An array representing the form definition. - * - * @ingroup forms - * @see shortcut_link_edit_validate() - * @see shortcut_link_edit_submit() - */ -function shortcut_link_edit($form, &$form_state, $shortcut_link) { - drupal_set_title(t('Editing @shortcut', array('@shortcut' => $shortcut_link['link_title']))); - $form['original_shortcut_link'] = array( - '#type' => 'value', - '#value' => $shortcut_link, - ); - $form += _shortcut_link_form_elements($shortcut_link); - return $form; -} - -/** - * 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. - */ -function _shortcut_link_form_elements($shortcut_link = NULL) { - if (!isset($shortcut_link)) { - $shortcut_link = entity_create('menu_link', array( - 'link_title' => '', - 'link_path' => '' - )); - } - else { - $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '') ? '' : Drupal::service('path.alias_manager')->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['#validate'][] = 'shortcut_link_edit_validate'; - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - - return $form; -} - -/** - * Validation handler for the shortcut link add and edit forms. - */ -function shortcut_link_edit_validate($form, &$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.')); - } -} - -/** - * Submit handler for shortcut_link_edit(). - */ -function shortcut_link_edit_submit($form, &$form_state) { - // Normalize the path in case it is an alias. - $shortcut_path = Drupal::service('path.alias_manager')->getSystemPath($form_state['values']['shortcut_link']['link_path']); - if (empty($shortcut_path)) { - $shortcut_path = ''; - } - $form_state['values']['shortcut_link']['link_path'] = $shortcut_path; - - $shortcut_link = $form_state['values']['original_shortcut_link']; - foreach ($form_state['values']['shortcut_link'] as $key => $value) { - $shortcut_link[$key] = $value; - } - - menu_link_save($shortcut_link); - $set_name = str_replace('shortcut-', '' , $shortcut_link['menu_name']); - $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set_name; - drupal_set_message(t('The shortcut %link has been updated.', array('%link' => $shortcut_link['link_title']))); -} - -/** - * Adds a link to the end of a shortcut set, keeping within a prescribed limit. - * - * @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. - * The links in the shortcut set will be re-weighted so that the new link is - * at the end, and some existing links may be disabled (if the $limit - * parameter is provided). - */ -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']); - if (empty($shortcut_link['link_path'])) { - $shortcut_link['link_path'] = ''; - } - $menu_link = entity_create('menu_link', $shortcut_link); - $menu_link->save(); - - // Add the link to the end of the list. - $shortcut_set->links[$menu_link->uuid()] = $menu_link; - shortcut_set_reset_link_weights($shortcut_set); -} diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index f340760..8e59bc3 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -122,11 +122,7 @@ function shortcut_menu() { ); $items['admin/config/user-interface/shortcut/link/%menu_link'] = array( 'title' => 'Edit shortcut', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('shortcut_link_edit', 5), - 'access callback' => 'shortcut_link_access', - 'access arguments' => array(5), - 'file' => 'shortcut.admin.inc', + 'route_name' => 'shortcut_link_edit', ); $items['admin/config/user-interface/shortcut/link/%menu_link/delete'] = array( 'title' => 'Delete shortcut', @@ -156,18 +152,10 @@ function shortcut_admin_paths() { } /** - * Access callback for editing a shortcut set. - * - * @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 bool - * TRUE if the current user has access to edit the shortcut set, FALSE - * otherwise. + * Implements hook_entity_info(). */ -function shortcut_set_edit_access(ShortcutSetInterface $shortcut_set) { - return $shortcut_set->access('update'); +function shortcut_entity_info(&$entity_info) { + $entity_info['menu_link']['controllers']['form']['edit_shortcut_link'] = 'Drupal\shortcut\Form\ShortcutLinkEditForm'; } /** @@ -205,19 +193,6 @@ function shortcut_set_switch_access($account = NULL) { } /** - * Access callback for editing a link in a shortcut set. - */ -function shortcut_link_access($menu_link) { - // The link must belong to a shortcut set that the current user has access - // to edit. - $set_name = str_replace('shortcut-', '', $menu_link['menu_name']); - if ($shortcut_set = shortcut_set_load($set_name)) { - return shortcut_set_edit_access($shortcut_set); - } - return FALSE; -} - -/** * Implements hook_menu_link_delete(). */ function shortcut_menu_link_delete($menu_link) { @@ -505,7 +480,7 @@ function shortcut_toolbar() { $links = shortcut_renderable_links(); $shortcut_set = shortcut_current_displayed_set(); $configure_link = NULL; - if (shortcut_set_edit_access($shortcut_set)) { + if ($shortcut_set->access('update')) { $configure_link = array( '#type' => 'link', '#title' => t('Edit shortcuts'), diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index 8b676af..76fd544 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -3,7 +3,7 @@ shortcut_link_delete: defaults: _form: 'Drupal\shortcut\Form\LinkDelete' requirements: - _access_shortcut_link_delete: 'TRUE' + _access_shortcut_link_update: 'TRUE' shortcut_set_delete: pattern: '/admin/config/user-interface/shortcut/manage/{shortcut_set}/delete' @@ -40,6 +40,13 @@ shortcut_link_add: requirements: _entity_access: 'shortcut_set.update' +shortcut_link_edit: + pattern: '/admin/config/user-interface/shortcut/link/{menu_link}' + defaults: + _entity_form: 'menu_link.edit_shortcut_link' + requirements: + _access_shortcut_link_update: 'TRUE' + shortcut_link_add_inline: pattern: '/admin/config/user-interface/shortcut/manage/{shortcut_set}/add-link-inline' defaults: diff --git a/core/modules/shortcut/shortcut.services.yml b/core/modules/shortcut/shortcut.services.yml index bb95f49..b83c1c4 100644 --- a/core/modules/shortcut/shortcut.services.yml +++ b/core/modules/shortcut/shortcut.services.yml @@ -1,5 +1,5 @@ services: access_check.shortcut.link: - class: Drupal\shortcut\Access\LinkDeleteAccessCheck + class: Drupal\shortcut\Access\ShortcutLinkUpdateAccessCheck tags: - { name: access_check }