diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php new file mode 100644 index 0000000..32a0cd0 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php @@ -0,0 +1,37 @@ +getRequirements()); + } + + /** + * Implements AccessCheckInterface::access(). + */ + 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 FALSE; + } +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/SetDeleteAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/SetDeleteAccessCheck.php new file mode 100644 index 0000000..d3790de --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Access/SetDeleteAccessCheck.php @@ -0,0 +1,35 @@ +getRequirements()); + } + + /** + * Implements AccessCheckInterface::access(). + */ + public function access(Route $route, Request $request) { + if ($shortcut = $request->attributes->get('shortcut')) { + return $shortcut->id() !== 'default'; + } + return FALSE; + } +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkDelete.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkDelete.php new file mode 100644 index 0000000..4fab6c4 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkDelete.php @@ -0,0 +1,65 @@ + $this->menuLink->link_title)); + } + + /** + * Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath(). + */ + protected function getCancelPath() { + return 'admin/config/user-interface/shortcut/manage/' . $this->menuLink->menu_name; + } + + /** + * Overrides \Drupal\Core\Form\ConfirmFormBase::getConfirmText(). + */ + protected function getConfirmText() { + return t('Delete'); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::buildForm(). + */ + public function buildForm(array $form, array &$form_state, MenuLink $menu_link = NULL) { + $this->menuLink = $menu_link; + + return parent::buildForm($form, $form_state); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + menu_link_delete($this->menuLink->mlid); + $set_name = str_replace('shortcut-', '' , $this->menuLink->menu_name); + $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set_name; + drupal_set_message(t('The shortcut %title has been deleted.', array('%title' => $this->menuLink->link_title))); + } + +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetDelete.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetDelete.php new file mode 100644 index 0000000..42b54d1 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetDelete.php @@ -0,0 +1,84 @@ + $this->shortcut->label())); + } + + /** + * Implements \Drupal\Core\Form\ConfirmFormBase::getCancelPath(). + */ + protected function getCancelPath() { + return 'admin/config/user-interface/shortcut/manage/' . $this->shortcut->id(); + } + + /** + * Overrides \Drupal\Core\Form\ConfirmFormBase::getConfirmText(). + */ + protected function getConfirmText() { + return t('Delete'); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::buildForm(). + */ + public function buildForm(array $form, array &$form_state, Shortcut $shortcut = NULL) { + $this->shortcut = $shortcut; + + // Find out how many users are directly assigned to this shortcut set, and + // make a message. + $number = db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', array(':name' => $this->shortcut->id()))->fetchField(); + $info = ''; + if ($number) { + $info .= '

' . format_plural($number, + '1 user has chosen or been assigned to this shortcut set.', + '@count users have chosen or been assigned to this shortcut set.') . '

'; + } + + // Also, if a module implements hook_shortcut_default_set(), it's possible + // that this set is being used as a default set. Add a message about that too. + if (\Drupal::service('module_handler')->getImplementations('shortcut_default_set')) { + $info .= '

' . t('If you have chosen this shortcut set as the default for some or all users, they may also be affected by deleting it.') . '

'; + } + + $form['info'] = array( + '#markup' => $info, + ); + + return parent::buildForm($form, $form_state); + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + $this->shortcut->delete(); + $form_state['redirect'] = 'admin/config/user-interface/shortcut'; + drupal_set_message(t('The shortcut set %title has been deleted.', array('%title' => $this->shortcut->label()))); + } + +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutBundle.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutBundle.php new file mode 100644 index 0000000..4225310 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutBundle.php @@ -0,0 +1,28 @@ +register('access_check.shortcut.link', 'Drupal\shortcut\Access\LinkDeleteAccessCheck') + ->addTag('access_check'); + $container->register('access_check.shortcut.set', 'Drupal\shortcut\Access\SetDeleteAccessCheck') + ->addTag('access_check'); + } + +} diff --git a/core/modules/shortcut/shortcut.admin.inc b/core/modules/shortcut/shortcut.admin.inc index 0e43052..dec4dbd 100644 --- a/core/modules/shortcut/shortcut.admin.inc +++ b/core/modules/shortcut/shortcut.admin.inc @@ -472,113 +472,6 @@ function shortcut_admin_add_link($shortcut_link, &$shortcut_set) { } /** - * Form callback: builds the confirmation form for deleting a shortcut set. - * - * @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_set Drupal\shortcut\Plugin\Core\Entity\Shortcut - * An object representing the shortcut set, as returned from - * shortcut_set_load(). - * - * @return - * An array representing the form definition. - * - * @ingroup forms - * @see shortcut_set_delete_form_submit() - */ -function shortcut_set_delete_form($form, &$form_state, $shortcut_set) { - $form['shortcut_set'] = array( - '#type' => 'value', - '#value' => $shortcut_set->id(), - ); - - // Find out how many users are directly assigned to this shortcut set, and - // make a message. - $number = db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', array(':name' => $shortcut_set->id()))->fetchField(); - $info = ''; - if ($number) { - $info .= '

' . format_plural($number, - '1 user has chosen or been assigned to this shortcut set.', - '@count users have chosen or been assigned to this shortcut set.') . '

'; - } - - // Also, if a module implements hook_shortcut_default_set(), it's possible - // that this set is being used as a default set. Add a message about that too. - if (count(module_implements('shortcut_default_set')) > 0) { - $info .= '

' . t('If you have chosen this shortcut set as the default for some or all users, they may also be affected by deleting it.') . '

'; - } - - $form['info'] = array( - '#markup' => $info, - ); - - return confirm_form( - $form, - t('Are you sure you want to delete the shortcut set %title?', array('%title' => $shortcut_set->label())), - 'admin/config/user-interface/shortcut/manage/' . $shortcut_set->id(), - t('This action cannot be undone.'), - t('Delete'), - t('Cancel') - ); -} - -/** - * Submit handler for shortcut_set_delete_form(). - */ -function shortcut_set_delete_form_submit($form, &$form_state) { - $shortcut_set = shortcut_set_load($form_state['values']['shortcut_set']); - $label = $shortcut_set->label(); - $shortcut_set->delete(); - $form_state['redirect'] = 'admin/config/user-interface/shortcut'; - drupal_set_message(t('The shortcut set %title has been deleted.', array('%title' => $label))); -} - -/** - * Form callback: builds the confirmation form for deleting 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 will be deleted. - * - * @return - * An array representing the form definition. - * - * @ingroup forms - * @see shortcut_link_delete_submit() - */ -function shortcut_link_delete($form, &$form_state, $shortcut_link) { - $form['shortcut_link'] = array( - '#type' => 'value', - '#value' => $shortcut_link, - ); - - return confirm_form( - $form, - t('Are you sure you want to delete the shortcut %title?', array('%title' => $shortcut_link['link_title'])), - 'admin/config/user-interface/shortcut/manage/' . $shortcut_link['menu_name'], - t('This action cannot be undone.'), - t('Delete'), - t('Cancel') - ); -} - -/** - * Submit handler for shortcut_link_delete_submit(). - */ -function shortcut_link_delete_submit($form, &$form_state) { - $shortcut_link = $form_state['values']['shortcut_link']; - menu_link_delete($shortcut_link['mlid']); - $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 %title has been deleted.', array('%title' => $shortcut_link['link_title']))); -} - -/** * Menu page callback: creates a new link in the provided shortcut set. * * After completion, redirects the user back to where they came from. diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 25a04c7..21e3d8d 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -101,11 +101,7 @@ function shortcut_menu() { ); $items['admin/config/user-interface/shortcut/manage/%shortcut_set/delete'] = array( 'title' => 'Delete shortcut set', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('shortcut_set_delete_form', 5), - 'access callback' => 'shortcut_set_delete_access', - 'access arguments' => array(5), - 'file' => 'shortcut.admin.inc', + 'route_name' => 'shortcut_set_delete', ); $items['admin/config/user-interface/shortcut/manage/%shortcut_set/add-link'] = array( 'title' => 'Add shortcut', @@ -135,11 +131,7 @@ function shortcut_menu() { ); $items['admin/config/user-interface/shortcut/link/%menu_link/delete'] = array( 'title' => 'Delete shortcut', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('shortcut_link_delete', 5), - 'access callback' => 'shortcut_link_access', - 'access arguments' => array(5), - 'file' => 'shortcut.admin.inc', + 'route_name' => 'shortcut_link_delete', ); $items['user/%user/shortcuts'] = array( 'title' => 'Shortcuts', @@ -200,30 +192,6 @@ function shortcut_set_edit_access($shortcut_set = NULL) { } /** - * Access callback for deleting a shortcut set. - * - * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut - * The shortcut set to be deleted. - * - * @return - * TRUE if the current user has access to delete shortcut sets and this is - * not the site-wide default set; FALSE otherwise. - */ -function shortcut_set_delete_access($shortcut_set) { - // Only admins can delete sets. - if (!user_access('administer shortcuts')) { - return FALSE; - } - - // Never let the default shortcut set be deleted. - if ($shortcut_set->id() == 'default') { - return FALSE; - } - - return TRUE; -} - -/** * Access callback for switching the shortcut set assigned to a user account. * * @param object $account @@ -258,19 +226,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) { diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml new file mode 100644 index 0000000..522431e --- /dev/null +++ b/core/modules/shortcut/shortcut.routing.yml @@ -0,0 +1,14 @@ +shortcut_link_delete: + pattern: '/admin/config/user-interface/shortcut/link/{menu_link}/delete' + defaults: + _form: 'Drupal\shortcut\Form\LinkDelete' + requirements: + _access_shortcut_link_delete: 'TRUE' + +shortcut_set_delete: + pattern: '/admin/config/user-interface/shortcut/manage/{shortcut}/delete' + defaults: + _form: 'Drupal\shortcut\Form\SetDelete' + requirements: + _permission: 'administer shortcuts' + _access_shortcut_set_delete: 'TRUE'