path.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to rename URLs.
  5. */
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\Core\Field\BaseFieldDefinition;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Routing\RouteMatchInterface;
  10. /**
  11. * Implements hook_help().
  12. */
  13. function path_help($route_name, RouteMatchInterface $route_match) {
  14. switch ($route_name) {
  15. case 'help.page.path':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Path module allows you to specify an alias, or custom URL, for any existing internal system path. Aliases should not be confused with URL redirects, which allow you to forward a changed or inactive URL to a new URL. In addition to making URLs more readable, aliases also help search engines index content more effectively. Multiple aliases may be used for a single internal system path. To automate the aliasing of paths, you can install the contributed module <a href=":pathauto">Pathauto</a>. For more information, see the <a href=":path">online documentation for the Path module</a>.', [':path' => 'https://www.drupal.org/documentation/modules/path', ':pathauto' => 'https://www.drupal.org/project/pathauto']) . '</p>';
  19. $output .= '<h3>' . t('Uses') . '</h3>';
  20. $output .= '<dl>';
  21. $output .= '<dt>' . t('Creating aliases') . '</dt>';
  22. $output .= '<dd>' . t('If you create or edit a taxonomy term you can add an alias (for example <em>music/jazz</em>) in the field "URL alias". When creating or editing content you can add an alias (for example <em>about-us/team</em>) under the section "URL path settings" in the field "URL alias". Aliases for any other path can be added through the page <a href=":aliases">URL aliases</a>. To add aliases a user needs the permission <a href=":permissions">Create and edit URL aliases</a>.', [':aliases' => \Drupal::url('path.admin_overview'), ':permissions' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-path'])]) . '</dd>';
  23. $output .= '<dt>' . t('Managing aliases') . '</dt>';
  24. $output .= '<dd>' . t('The Path module provides a way to search and view a <a href=":aliases">list of all aliases</a> that are in use on your website. Aliases can be added, edited and deleted through this list.', [':aliases' => \Drupal::url('path.admin_overview')]) . '</dd>';
  25. $output .= '</dl>';
  26. return $output;
  27. case 'path.admin_overview':
  28. return '<p>' . t("An alias defines a different name for an existing URL path - for example, the alias 'about' for the URL path 'node/1'. A URL path can have multiple aliases.") . '</p>';
  29. case 'path.admin_add':
  30. return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
  31. }
  32. }
  33. /**
  34. * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
  35. */
  36. function path_form_node_form_alter(&$form, FormStateInterface $form_state) {
  37. $node = $form_state->getFormObject()->getEntity();
  38. $form['path_settings'] = [
  39. '#type' => 'details',
  40. '#title' => t('URL path settings'),
  41. '#open' => !empty($form['path']['widget'][0]['alias']['#value']),
  42. '#group' => 'advanced',
  43. '#access' => !empty($form['path']['#access']) && $node->hasField('path') && $node->get('path')->access('edit'),
  44. '#attributes' => [
  45. 'class' => ['path-form'],
  46. ],
  47. '#attached' => [
  48. 'library' => ['path/drupal.path'],
  49. ],
  50. '#weight' => 30,
  51. ];
  52. $form['path']['#group'] = 'path_settings';
  53. }
  54. /**
  55. * Implements hook_entity_base_field_info().
  56. */
  57. function path_entity_base_field_info(EntityTypeInterface $entity_type) {
  58. if ($entity_type->id() === 'taxonomy_term' || $entity_type->id() === 'node') {
  59. $fields['path'] = BaseFieldDefinition::create('path')
  60. ->setLabel(t('URL alias'))
  61. ->setTranslatable(TRUE)
  62. ->setDisplayOptions('form', [
  63. 'type' => 'path',
  64. 'weight' => 30,
  65. ])
  66. ->setDisplayConfigurable('form', TRUE)
  67. ->setComputed(TRUE);
  68. return $fields;
  69. }
  70. }