diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkAccessCheck.php new file mode 100644 index 0000000..18d38c2 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkAccessCheck.php @@ -0,0 +1,40 @@ +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); + } + } + +} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php deleted file mode 100644 index 6fad35c..0000000 --- a/core/modules/shortcut/lib/Drupal/shortcut/Access/LinkDeleteAccessCheck.php +++ /dev/null @@ -1,37 +0,0 @@ -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); - } - } - -} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/Elements.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/Elements.php new file mode 100644 index 0000000..2b132fa --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/Elements.php @@ -0,0 +1,70 @@ +menu_link = $entityManager + ->getStorageController('menu_link') + ->create(array( + 'link_title' => '', + 'link_path' => '', + )); + } + else { + $this->menu_link = $menu_link; + $this->menu_link['link_path'] = ($menu_link['link_path'] == '') ? '' : drupal_container()->get('path.alias_manager')->getPathAlias($menu_link['link_path']); + } + } + + /** + * Give form elements + */ + public function giveFormElements() { + $form['shortcut_link']['#tree'] = TRUE; + $form['shortcut_link']['link_title'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#size' => 40, + '#maxlength' => 255, + '#default_value' => $this->menu_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' => $this->menu_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/LinkEdit.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkEdit.php new file mode 100644 index 0000000..c7b73d6 --- /dev/null +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/LinkEdit.php @@ -0,0 +1,124 @@ +entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity') + ); + } + + /** + * Returns a unique string identifying the form. + * + * @return string + * The unique string identifying the form. + */ + public function getFormID() { + return 'shortcut_link_edit'; + } + + /** + * Form constructor. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + * + * @return array + * The form structure. + */ + public function buildForm(array $form, array &$form_state, MenuLink $menu_link = NULL, Request $request = NULL) { + $this->menuLink = $menu_link; + drupal_set_title(t('Editing @shortcut', array('@shortcut' => $this->menuLink['link_title']))); + $form['original_shortcut_link'] = array( + '#type' => 'value', + '#value' => $this->menuLink, + ); + $elements = new Elements($this->menuLink, $this->entityManager); + $form += $elements->giveFormElements(); + return $form; + } + + /** + * Form validation handler. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + */ + 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.')); + } + } + + /** + * Form submission handler. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + */ + public function submitForm(array &$form, array &$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; + } + + $shortcut_link->save(); + $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']))); + } + +} diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index c23577c..b2b5d3e 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -125,11 +125,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', diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index e6bb718..fd4017e 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -5,6 +5,13 @@ shortcut_link_delete: requirements: _access_shortcut_link_delete: 'TRUE' +shortcut_link_edit: + pattern: '/admin/config/user-interface/shortcut/link/{menu_link}' + defaults: + _form: 'Drupal\shortcut\Form\LinkEdit' + requirements: + _access_shortcut_link_edit: 'TRUE' + shortcut_set_delete: pattern: '/admin/config/user-interface/shortcut/manage/{shortcut}/delete' defaults: