path.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to rename URLs.
  5. */
  6. use Drupal\Core\Entity\ContentEntityInterface;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\Core\Field\BaseFieldDefinition;
  9. use Drupal\Core\Form\FormStateInterface;
  10. use Drupal\Core\Routing\RouteMatchInterface;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function path_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'help.page.path':
  17. $output = '';
  18. $output .= '<h3>' . t('About') . '</h3>';
  19. $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>';
  20. $output .= '<h3>' . t('Uses') . '</h3>';
  21. $output .= '<dl>';
  22. $output .= '<dt>' . t('Creating aliases') . '</dt>';
  23. $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>';
  24. $output .= '<dt>' . t('Managing aliases') . '</dt>';
  25. $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>';
  26. $output .= '</dl>';
  27. return $output;
  28. case 'path.admin_overview':
  29. 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>';
  30. case 'path.admin_add':
  31. return '<p>' . t('Enter the path you wish to create the alias for, followed by the name of the new alias.') . '</p>';
  32. }
  33. }
  34. /**
  35. * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
  36. */
  37. function path_form_node_form_alter(&$form, FormStateInterface $form_state) {
  38. $node = $form_state->getFormObject()->getEntity();
  39. $form['path_settings'] = [
  40. '#type' => 'details',
  41. '#title' => t('URL path settings'),
  42. '#open' => !empty($form['path']['widget'][0]['alias']['#value']),
  43. '#group' => 'advanced',
  44. '#access' => !empty($form['path']['#access']) && $node->hasField('path') && $node->get('path')->access('edit'),
  45. '#attributes' => [
  46. 'class' => ['path-form'],
  47. ],
  48. '#attached' => [
  49. 'library' => ['path/drupal.path'],
  50. ],
  51. '#weight' => 30,
  52. ];
  53. $form['path']['#group'] = 'path_settings';
  54. }
  55. /**
  56. * Implements hook_entity_base_field_info().
  57. */
  58. function path_entity_base_field_info(EntityTypeInterface $entity_type) {
  59. if (in_array($entity_type->id(), ['taxonomy_term', 'node', 'media'], TRUE)) {
  60. $fields['path'] = BaseFieldDefinition::create('path')
  61. ->setLabel(t('URL alias'))
  62. ->setTranslatable(TRUE)
  63. ->setDisplayOptions('form', [
  64. 'type' => 'path',
  65. 'weight' => 30,
  66. ])
  67. ->setDisplayConfigurable('form', TRUE)
  68. ->setComputed(TRUE);
  69. return $fields;
  70. }
  71. }
  72. /**
  73. * Implements hook_entity_translation_create().
  74. */
  75. function path_entity_translation_create(ContentEntityInterface $translation) {
  76. foreach ($translation->getFieldDefinitions() as $field_name => $field_definition) {
  77. if ($field_definition->getType() === 'path' && $translation->get($field_name)->pid) {
  78. // If there are values and a path ID, update the langcode and unset the
  79. // path ID to save this as a new alias.
  80. $translation->get($field_name)->langcode = $translation->language()->getId();
  81. $translation->get($field_name)->pid = NULL;
  82. }
  83. }
  84. }