diff --git a/core/modules/path/lib/Drupal/path/Controller/PathController.php b/core/modules/path/lib/Drupal/path/Controller/PathController.php index 90f0526..162bf6e 100644 --- a/core/modules/path/lib/Drupal/path/Controller/PathController.php +++ b/core/modules/path/lib/Drupal/path/Controller/PathController.php @@ -7,42 +7,125 @@ namespace Drupal\path\Controller; +use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Path\AliasStorageInterface; +use Drupal\Core\Path\AliasManagerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + /** * Controller routines for path routes. */ -class PathController { +class PathController extends ControllerBase { /** - * @todo Remove path_admin_overview(). + * The path alias storage. + * + * @var \Drupal\Core\Path\AliasStorageInterface */ - public function adminOverview() { - module_load_include('admin.inc', 'path'); - return path_admin_overview(); - } + protected $aliasStorage; /** - * @todo Remove path_admin_overview(). + * The path alias manager. + * + * @var \Drupal\Core\Path\AliasManagerInterface */ - public function adminOverviewFiltered($keys) { - module_load_include('admin.inc', 'path'); - return path_admin_overview($keys); - } + protected $aliasManager; /** - * @todo Remove path_admin_edit(). + * Constructs a new PathController. + * + * @param \Drupal\Core\Path\AliasStorageInterface $alias_storage + * The path alias storage. + * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager + * The path alias manager. */ - public function adminEdit($path) { - $path = \Drupal::service('path.alias_storage')->load(array('pid' => $path)); - module_load_include('admin.inc', 'path'); - return path_admin_edit($path); + public function __construct(AliasStorageInterface $alias_storage, AliasManagerInterface $alias_manager) { + $this->aliasStorage = $alias_storage; + $this->aliasManager = $alias_manager; } /** - * @todo Remove path_admin_edit(). + * {@inheritdoc} */ - public function adminAdd() { - module_load_include('admin.inc', 'path'); - return path_admin_edit(); + public static function create(ContainerInterface $container) { + return new static( + $container->get('path.alias_storage'), + $container->get('path.alias_manager') + ); + } + + public function adminOverview($keys) { + // Add the filter form above the overview table. + $build['path_admin_filter_form'] = $this->formBuilder()->getForm('Drupal\path\Form\PathFilterForm', $keys); + // Enable language column if language.module is enabled or if we have any + // alias with a language. + $multilanguage = ($this->moduleHandler()->moduleExists('language') || $this->aliasStorage->languageAliasExists()); + + $header = array(); + $header[] = array('data' => $this->t('Alias'), 'field' => 'alias', 'sort' => 'asc'); + $header[] = array('data' => $this->t('System'), 'field' => 'source'); + if ($multilanguage) { + $header[] = array('data' => $this->t('Language'), 'field' => 'langcode'); + } + $header[] = $this->t('Operations'); + + $rows = array(); + $destination = drupal_get_destination(); + foreach ($this->aliasStorage->getAliasesForAdminListing($header, $keys) as $data) { + $row = array(); + $row['data']['alias'] = l(truncate_utf8($data->alias, 50, FALSE, TRUE), $data->source, array( + 'attributes' => array('title' => $data->alias), + )); + $row['data']['source'] = l(truncate_utf8($data->source, 50, FALSE, TRUE), $data->source, array( + 'alias' => TRUE, + 'attributes' => array('title' => $data->source), + )); + if ($multilanguage) { + $row['data']['language_name'] = $this->languageManager()->getLanguageName($data->langcode); + } + + $operations = array(); + $operations['edit'] = array( + 'title' => $this->t('Edit'), + 'route_name' => 'path.admin_edit', + 'route_parameters' => array( + 'pid' => $data->pid, + ), + 'query' => $destination, + ); + $operations['delete'] = array( + 'title' => $this->t('Delete'), + 'route_name' => 'path.delete', + 'route_parameters' => array( + 'pid' => $data->pid, + ), + 'query' => $destination, + ); + $row['data']['operations'] = array( + 'data' => array( + '#type' => 'operations', + '#links' => $operations, + ), + ); + + // If the system path maps to a different URL alias, highlight this table + // row to let the user know of old aliases. + if ($data->alias != $this->aliasManager->getPathAlias($data->source, $data->langcode)) { + $row['class'] = array('warning'); + } + + $rows[] = $row; + } + + $build['path_table'] = array( + '#type' => 'table', + '#header' => $header, + '#rows' => $rows, + '#empty' => $this->t('No URL aliases available. Add URL alias.', array('@link' => $this->url('path.admin_add'))), + ); + $build['path_pager'] = array('#theme' => 'pager'); + + return $build; } } diff --git a/core/modules/path/lib/Drupal/path/Form/AddForm.php b/core/modules/path/lib/Drupal/path/Form/AddForm.php new file mode 100644 index 0000000..a5b056b --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Form/AddForm.php @@ -0,0 +1,36 @@ + '', + 'alias' => '', + 'langcode' => Language::LANGCODE_NOT_SPECIFIED, + 'pid' => NULL, + ); + } + +} diff --git a/core/modules/path/lib/Drupal/path/Form/EditForm.php b/core/modules/path/lib/Drupal/path/Form/EditForm.php new file mode 100644 index 0000000..0c1a7ab --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Form/EditForm.php @@ -0,0 +1,65 @@ +aliasStorage->load(array('pid' => $pid)); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state, $pid = NULL) { + $form = parent::buildForm($form, $form_state, $pid); + + $form['#title'] = String::checkPlain($this->path['alias']); + $form['pid'] = array( + '#type' => 'hidden', + '#value' => $this->path['pid'], + ); + $form['actions']['delete'] = array( + '#type' => 'submit', + '#value' => $this->t('Delete'), + '#submit' => array(array($this, 'deleteSubmit')), + ); + return $form; + } + + /** + * Submits the delete form. + */ + public function deleteSubmit(array &$form, array &$form_state) { + $form_state['redirect_route'] = new Url('path.delete', array( + 'pid' => $form_state['values']['pid'], + )); + + if ($this->getRequest()->query->has('destination')) { + $form_state['redirect_route']->setOption('query', drupal_get_destination()); + $this->getRequest()->query->remove('destination'); + } + } + +} diff --git a/core/modules/path/lib/Drupal/path/Form/PathFilterForm.php b/core/modules/path/lib/Drupal/path/Form/PathFilterForm.php new file mode 100644 index 0000000..ce66c70 --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Form/PathFilterForm.php @@ -0,0 +1,75 @@ + array('search-form')); + $form['basic'] = array( + '#type' => 'details', + '#title' => $this->t('Filter aliases'), + '#open' => TRUE, + '#attributes' => array('class' => array('container-inline')), + ); + $form['basic']['filter'] = array( + '#type' => 'search', + '#title' => 'Path alias', + '#title_display' => 'invisible', + '#default_value' => $keys, + '#maxlength' => 128, + '#size' => 25, + ); + $form['basic']['submit'] = array( + '#type' => 'submit', + '#button_type' => 'primary', + '#value' => $this->t('Filter'), + ); + if ($keys) { + $form['basic']['reset'] = array( + '#type' => 'submit', + '#value' => $this->t('Reset'), + '#submit' => array(array($this, 'resetForm')), + ); + } + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['redirect_route'] = new Url('path.admin_overview_filter', array( + 'keys' => trim($form_state['values']['filter']), + )); + } + + /** + * Resets the filter selections. + */ + public function resetForm(array &$form, array &$form_state) { + $form_state['redirect_route'] = new Url('path.admin_overview'); + } + +} diff --git a/core/modules/path/lib/Drupal/path/Form/PathFormBase.php b/core/modules/path/lib/Drupal/path/Form/PathFormBase.php new file mode 100644 index 0000000..251fbe3 --- /dev/null +++ b/core/modules/path/lib/Drupal/path/Form/PathFormBase.php @@ -0,0 +1,175 @@ +aliasStorage = $alias_storage; + $this->aliasManager = $alias_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('path.alias_storage'), + $container->get('path.alias_manager') + ); + } + + /** + * Builds the path used by the form. + * + * @param int|null $pid + * Either the unique path ID, or NULL if a new one is being created. + */ + abstract protected function buildPath($pid); + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state, $pid = NULL) { + $this->path = $this->buildPath($pid); + $form['source'] = array( + '#type' => 'textfield', + '#title' => $this->t('Existing system path'), + '#default_value' => $this->path['source'], + '#maxlength' => 255, + '#size' => 45, + '#description' => $this->t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'), + '#field_prefix' => url(NULL, array('absolute' => TRUE)), + '#required' => TRUE, + ); + $form['alias'] = array( + '#type' => 'textfield', + '#title' => $this->t('Path alias'), + '#default_value' => $this->path['alias'], + '#maxlength' => 255, + '#size' => 45, + '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'), + '#field_prefix' => url(NULL, array('absolute' => TRUE)), + '#required' => TRUE, + ); + + // A hidden value unless language.module is enabled. + if (\Drupal::moduleHandler()->moduleExists('language')) { + $languages = \Drupal::languageManager()->getLanguages(); + $language_options = array(); + foreach ($languages as $langcode => $language) { + $language_options[$langcode] = $language->name; + } + + $form['langcode'] = array( + '#type' => 'select', + '#title' => $this->t('Language'), + '#options' => $language_options, + '#empty_value' => Language::LANGCODE_NOT_SPECIFIED, + '#empty_option' => $this->t('- None -'), + '#default_value' => $this->path['langcode'], + '#weight' => -10, + '#description' => $this->t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set as - None -.'), + ); + } + else { + $form['langcode'] = array( + '#type' => 'value', + '#value' => $this->path['langcode'] + ); + } + + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Save'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + $source = &$form_state['values']['source']; + $source = $this->aliasManager->getSystemPath($source); + $alias = $form_state['values']['alias']; + // Language is only set if language.module is enabled, otherwise save for all + // languages. + $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : Language::LANGCODE_NOT_SPECIFIED; + + if ($this->aliasStorage->aliasExists($alias, $langcode, $source)) { + $this->setFormError('alias', $form_state, t('The alias %alias is already in use in this language.', array('%alias' => $alias))); + } + if (!drupal_valid_path($source)) { + $this->setFormError('source', $form_state, t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source))); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + // Remove unnecessary values. + form_state_values_clean($form_state); + + $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0; + $source = &$form_state['values']['source']; + $source = $this->aliasManager->getSystemPath($source); + $alias = $form_state['values']['alias']; + // Language is only set if language.module is enabled, otherwise save for all + // languages. + $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : Language::LANGCODE_NOT_SPECIFIED; + + $this->aliasStorage->save($source, $alias, $langcode, $pid); + + drupal_set_message($this->t('The alias has been saved.')); + $form_state['redirect_route'] = new Url('path.admin_overview'); + } + +} diff --git a/core/modules/path/path.admin.inc b/core/modules/path/path.admin.inc deleted file mode 100644 index c0eb73e..0000000 --- a/core/modules/path/path.admin.inc +++ /dev/null @@ -1,311 +0,0 @@ -getForm('path_admin_filter_form', $keys); - // Enable language column if language.module is enabled or if we have any - // alias with a language. - $multilanguage = (\Drupal::moduleHandler()->moduleExists('language') || \Drupal::service('path.alias_storage')->languageAliasExists()); - - $header = array(); - $header[] = array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'); - $header[] = array('data' => t('System'), 'field' => 'source'); - if ($multilanguage) { - $header[] = array('data' => t('Language'), 'field' => 'langcode'); - } - $header[] = t('Operations'); - - $rows = array(); - $destination = drupal_get_destination(); - /** @var \Drupal\core\Path\AliasStorage $alias_storage */ - $alias_storage = \Drupal::service('path.alias_storage'); - foreach ($alias_storage->getAliasesForAdminListing($header, $keys) as $data) { - $row = array(); - $row['data']['alias'] = l(truncate_utf8($data->alias, 50, FALSE, TRUE), $data->source, array( - 'attributes' => array('title' => $data->alias), - )); - $row['data']['source'] = l(truncate_utf8($data->source, 50, FALSE, TRUE), $data->source, array( - 'alias' => TRUE, - 'attributes' => array('title' => $data->source), - )); - if ($multilanguage) { - $row['data']['language_name'] = \Drupal::languageManager()->getLanguageName($data->langcode); - } - - $operations = array(); - $operations['edit'] = array( - 'title' => t('Edit'), - 'href' => "admin/config/search/path/edit/$data->pid", - 'query' => $destination, - ); - $operations['delete'] = array( - 'title' => t('Delete'), - 'href' => "admin/config/search/path/delete/$data->pid", - 'query' => $destination, - ); - $row['data']['operations'] = array( - 'data' => array( - '#type' => 'operations', - '#links' => $operations, - ), - ); - - // If the system path maps to a different URL alias, highlight this table - // row to let the user know of old aliases. - if ($data->alias != \Drupal::service('path.alias_manager')->getPathAlias($data->source, $data->langcode)) { - $row['class'] = array('warning'); - } - - $rows[] = $row; - } - - $build['path_table'] = array( - '#type' => 'table', - '#header' => $header, - '#rows' => $rows, - '#empty' => t('No URL aliases available. Add URL alias.', array('@link' => url('admin/config/search/path/add'))), - ); - $build['path_pager'] = array('#theme' => 'pager'); - - return $build; -} - -/** - * Page callback: Returns a form creating or editing a path alias. - * - * @param $path - * An array containing the path ID, source, alias, and language code. - * - * @return - * A form for adding or editing a URL alias. - * - * @see path_menu() - * - * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. Use - * \Drupal\path\Controller\PathController::adminAdd() or - * \Drupal\path\Controller\PathController::adminEdit() - */ -function path_admin_edit($path = array()) { - if ($path) { - $output = \Drupal::formBuilder()->getForm('path_admin_form', $path); - $output['#title'] = String::checkPlain($path['alias']); - } - else { - $output = \Drupal::formBuilder()->getForm('path_admin_form'); - } - - return $output; -} - -/** - * Form constructor for the path administration form. - * - * @param $path - * An array containing the path ID, source, alias, and language code. - * - * @ingroup forms - * @see path_admin_form_validate() - * @see path_admin_form_submit() - * @see path_admin_form_delete_submit() - */ -function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'langcode' => Language::LANGCODE_NOT_SPECIFIED, 'pid' => NULL)) { - $form['source'] = array( - '#type' => 'textfield', - '#title' => t('Existing system path'), - '#default_value' => $path['source'], - '#maxlength' => 255, - '#size' => 45, - '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'), - '#field_prefix' => url(NULL, array('absolute' => TRUE)), - '#required' => TRUE, - ); - $form['alias'] = array( - '#type' => 'textfield', - '#title' => t('Path alias'), - '#default_value' => $path['alias'], - '#maxlength' => 255, - '#size' => 45, - '#description' => t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'), - '#field_prefix' => url(NULL, array('absolute' => TRUE)), - '#required' => TRUE, - ); - - // A hidden value unless language.module is enabled. - if (\Drupal::moduleHandler()->moduleExists('language')) { - $languages = \Drupal::languageManager()->getLanguages(); - foreach ($languages as $langcode => $language) { - $language_options[$langcode] = $language->name; - } - - $form['langcode'] = array( - '#type' => 'select', - '#title' => t('Language'), - '#options' => $language_options, - '#empty_value' => Language::LANGCODE_NOT_SPECIFIED, - '#empty_option' => t('- None -'), - '#default_value' => $path['langcode'], - '#weight' => -10, - '#description' => t('A path alias set for a specific language will always be used when displaying this page in that language, and takes precedence over path aliases set as - None -.'), - ); - } - else { - $form['langcode'] = array( - '#type' => 'value', - '#value' => $path['langcode'] - ); - } - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - if ($path['pid']) { - $form['pid'] = array( - '#type' => 'hidden', - '#value' => $path['pid'], - ); - $form['actions']['delete'] = array( - '#type' => 'submit', - '#value' => t('Delete'), - '#submit' => array('path_admin_form_delete_submit'), - ); - } - - return $form; -} - -/** - * Form submission handler for the 'Delete' button on path_admin_form(). - * - * @see path_admin_form_validate() - * @see path_admin_form_submit() - */ -function path_admin_form_delete_submit($form, &$form_state) { - $destination = array(); - $query = \Drupal::request()->query; - if ($query->has('destination')) { - $destination = drupal_get_destination(); - $query->remove('destination'); - } - $form_state['redirect'] = array('admin/config/search/path/delete/' . $form_state['values']['pid'], array('query' => $destination)); -} - -/** - * Form validation handler for path_admin_form(). - * - * @see path_admin_form_submit() - * @see path_admin_form_delete_submit() - */ -function path_admin_form_validate($form, &$form_state) { - $source = &$form_state['values']['source']; - $source = \Drupal::service('path.alias_manager')->getSystemPath($source); - $alias = $form_state['values']['alias']; - // Language is only set if language.module is enabled, otherwise save for all - // languages. - $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : Language::LANGCODE_NOT_SPECIFIED; - - if (\Drupal::service('path.alias_storage')->aliasExists($alias, $langcode, $source)) { - form_set_error('alias', $form_state, t('The alias %alias is already in use in this language.', array('%alias' => $alias))); - } - if (!drupal_valid_path($source)) { - form_set_error('source', $form_state, t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source))); - } -} - -/** - * Form submission handler for path_admin_form(). - * - * @see path_admin_form_validate() - * @see path_admin_form_delete_submit() - */ -function path_admin_form_submit($form, &$form_state) { - // Remove unnecessary values. - form_state_values_clean($form_state); - - $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0; - $source = &$form_state['values']['source']; - $source = \Drupal::service('path.alias_manager')->getSystemPath($source); - $alias = $form_state['values']['alias']; - // Language is only set if language.module is enabled, otherwise save for all - // languages. - $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : Language::LANGCODE_NOT_SPECIFIED; - - \Drupal::service('path.alias_storage')->save($source, $alias, $langcode, $pid); - - drupal_set_message(t('The alias has been saved.')); - $form_state['redirect'] = 'admin/config/search/path'; -} - -/** - * Form constructor for the path admin overview filter form. - * - * @ingroup forms - * @see path_admin_filter_form_submit_filter() - * @see path_admin_filter_form_submit_reset() - */ -function path_admin_filter_form($form, &$form_state, $keys = '') { - $form['#attributes'] = array('class' => array('search-form')); - $form['basic'] = array( - '#type' => 'details', - '#title' => t('Filter aliases'), - '#open' => TRUE, - '#attributes' => array('class' => array('container-inline')), - ); - $form['basic']['filter'] = array( - '#type' => 'search', - '#title' => 'Path alias', - '#title_display' => 'invisible', - '#default_value' => $keys, - '#maxlength' => 128, - '#size' => 25, - ); - $form['basic']['submit'] = array( - '#type' => 'submit', - '#value' => t('Filter'), - '#submit' => array('path_admin_filter_form_submit_filter'), - ); - if ($keys) { - $form['basic']['reset'] = array( - '#type' => 'submit', - '#value' => t('Reset'), - '#submit' => array('path_admin_filter_form_submit_reset'), - ); - } - return $form; -} - -/** - * Form submission handler for the path_admin_filter_form() Filter button. - * - * @see path_admin_filter_form_submit_reset() - */ -function path_admin_filter_form_submit_filter($form, &$form_state) { - $form_state['redirect'] = 'admin/config/search/path/list/' . trim($form_state['values']['filter']); -} - -/** - * Form submission handler for the path_admin_filter_form() Reset button. - * - * @see path_admin_filter_form_submit_filter() - */ -function path_admin_filter_form_submit_reset($form, &$form_state) { - $form_state['redirect'] = 'admin/config/search/path'; -} diff --git a/core/modules/path/path.routing.yml b/core/modules/path/path.routing.yml index 9c702e1..d3e44c3 100644 --- a/core/modules/path/path.routing.yml +++ b/core/modules/path/path.routing.yml @@ -11,6 +11,7 @@ path.admin_overview: defaults: _title: 'URL aliases' _content: '\Drupal\path\Controller\PathController::adminOverview' + keys: NULL requirements: _permission: 'administer url aliases' @@ -18,7 +19,7 @@ path.admin_overview_filter: path: '/admin/config/search/path/list/{keys}' defaults: _title: 'URL aliases' - _content: '\Drupal\path\Controller\PathController::adminOverviewFiltered' + _content: '\Drupal\path\Controller\PathController::adminOverview' requirements: _permission: 'administer url aliases' @@ -26,14 +27,14 @@ path.admin_add: path: '/admin/config/search/path/add' defaults: _title: 'Add alias' - _content: '\Drupal\path\Controller\PathController::adminAdd' + _form: '\Drupal\path\Form\AddForm' requirements: _permission: 'administer url aliases' path.admin_edit: - path: '/admin/config/search/path/edit/{path}' + path: '/admin/config/search/path/edit/{pid}' defaults: _title: 'Edit alias' - _content: '\Drupal\path\Controller\PathController::adminEdit' + _form: '\Drupal\path\Form\EditForm' requirements: _permission: 'administer url aliases'