pathauto.module 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @file
  4. * @defgroup pathauto Pathauto: Automatically generates aliases for content
  5. *
  6. * The Pathauto module automatically generates path aliases for various kinds of
  7. * content (nodes, categories, users) without requiring the user to manually
  8. * specify the path alias. This allows you to get aliases like
  9. * /category/my-node-title.html instead of /node/123. The aliases are based upon
  10. * a "pattern" system which the administrator can control.
  11. */
  12. /**
  13. * @file
  14. * Main file for the Pathauto module, which automatically generates aliases for content.
  15. *
  16. * @ingroup pathauto
  17. */
  18. use Drupal\Core\Entity\EntityTypeInterface;
  19. use Drupal\Core\Field\BaseFieldDefinition;
  20. use Drupal\Core\Form\FormStateInterface;
  21. use Drupal\Core\Routing\RouteMatchInterface;
  22. use Drupal\Core\Url;
  23. use Drupal\pathauto\PathautoFieldItemList;
  24. use Drupal\pathauto\PathautoItem;
  25. /**
  26. * The default ignore word list.
  27. */
  28. define('PATHAUTO_IGNORE_WORDS', 'a, an, as, at, before, but, by, for, from, is, in, into, like, of, off, on, onto, per, since, than, the, this, that, to, up, via, with');
  29. /**
  30. * Implements hook_hook_info().
  31. */
  32. function pathauto_hook_info() {
  33. $hooks = array(
  34. 'pathauto_pattern_alter',
  35. 'pathauto_alias_alter',
  36. 'pathauto_is_alias_reserved',
  37. );
  38. return array_fill_keys($hooks, array('group' => 'pathauto'));
  39. }
  40. /**
  41. * Implements hook_help().
  42. */
  43. function pathauto_help($route_name, RouteMatchInterface $route_match) {
  44. switch ($route_name) {
  45. case 'help.page.pathauto':
  46. $output = '<h3>' . t('About') . '</h3>';
  47. $output .= '<p>' . t('The Pathauto module provides a mechanism to automate the creation of <a href="path">path</a> aliases. This makes URLs more readable and helps search engines index content more effectively. For more information, see the <a href=":online">online documentation for Pathauto</a>.', [':online' => 'https://www.drupal.org/documentation/modules/pathauto']) . '</p>';
  48. $output .= '<dl>';
  49. $output .= '<h3>' . t('Uses') . '</h3>';
  50. $output .= '<dd>' . t('Pathauto is accessed from the tabs it adds to the list of <a href=":aliases">URL aliases</a>.', [':aliases' => Url::fromRoute('path.admin_overview')->toString()]) . '</dd>';
  51. $output .= '<dt>' . t('Creating Pathauto Patterns') . '</dt>';
  52. $output .= '<dd>' . t('The <a href=":pathauto_pattern">"Patterns"</a> page is used to configure automatic path aliasing. New patterns are created here using the <a href=":add_form">Add Pathauto pattern</a> button which presents a form to simplify pattern creation thru the use of <a href="token">available tokens</a>. The patterns page provides a list of all patterns on the site and allows you to edit and reorder them. An alias is generated for the first pattern that applies.', [':pathauto_pattern' => Url::fromRoute('entity.pathauto_pattern.collection')->toString(), ':add_form' => Url::fromRoute('entity.pathauto_pattern.add_form')->toString()]) . '</dd>';
  53. $output .= '<dt>' . t('Pathauto Settings') . '</dt>';
  54. $output .= '<dd>' . t('The <a href=":settings">"Settings"</a> page is used to customize global Pathauto settings for automated pattern creation.', [':settings' => Url::fromRoute('pathauto.settings.form')->toString()]) . '</dd>';
  55. $output .= '<dd>' . t('The <strong>maximum alias length</strong> and <strong>maximum component length</strong> values default to 100 and have a limit of @max from Pathauto. You should enter a value that is the length of the "alias" column of the url_alias database table minus the length of any strings that might get added to the end of the URL. The recommended and default value is 100.', array('@max' => \Drupal::service('pathauto.alias_storage_helper')->getAliasSchemaMaxlength())) . '</dd>';
  56. $output .= '<dt>' . t('Bulk Generation') . '</dt>';
  57. $output .= '<dd>' . t('The <a href=":pathauto_bulk">"Bulk Generate"</a> page allows you to create URL aliases for items that currently have no aliases. This is typically used when installing Pathauto on a site that has existing un-aliased content that needs to be aliased in bulk.', [':pathauto_bulk' => Url::fromRoute('pathauto.bulk.update.form')->toString()]) . '</dd>';
  58. $output .= '<dt>' . t('Delete Aliases') . '</dt>';
  59. $output .= '<dd>' . t('The <a href=":pathauto_delete">"Delete Aliases"</a> page allows you to remove URL aliases from items that have previously been assigned aliases using pathauto.', [':pathauto_delete' => Url::fromRoute('pathauto.admin.delete')->toString()]) . '</dd>';
  60. $output .= '</dl>';
  61. return $output;
  62. case 'entity.pathauto_pattern.collection':
  63. $output = '<p>' . t('This page provides a list of all patterns on the site and allows you to edit and reorder them.') . '</p>';
  64. return $output;
  65. case 'entity.pathauto_pattern.add_form':
  66. $output = '<p>' . t('You need to select a pattern type, then a pattern and filter, and a label. Additional types can be enabled on the <a href=":settings">Settings</a> page.', [':settings' => Url::fromRoute('pathauto.settings.form')->toString()]) . '</p>';
  67. return $output;
  68. case 'pathauto.bulk.update.form':
  69. $output = '<p>' . t('Bulk generation can be used to generate URL aliases for items that currently have no aliases. This is typically used when installing Pathauto on a site that has existing un-aliased content that needs to be aliased in bulk.') . '<br>';
  70. $output .= t('It can also be used to regenerate URL aliases for items that have an old alias and for which the Pathauto pattern has been changed.') . '</p>';
  71. $output .= '<p>' . t('Note that this will only affect items which are configured to have their URL alias automatically set. Items whose URL alias is manually set are not affected.') . '</p>';
  72. return $output;
  73. }
  74. }
  75. /**
  76. * Implements hook_field_info_alter().
  77. */
  78. function pathauto_field_info_alter(&$info) {
  79. $info['path']['class'] = PathautoItem::class;
  80. $info['path']['list_class'] = PathautoFieldItemList::class;
  81. }
  82. /**
  83. * Implements hook_field_widget_info_alter().
  84. */
  85. function pathauto_field_widget_info_alter(&$widgets) {
  86. $widgets['path']['class'] = 'Drupal\pathauto\PathautoWidget';
  87. }
  88. /**
  89. * Implements hook_entity_base_field_info().
  90. */
  91. function pathauto_entity_base_field_info(EntityTypeInterface $entity_type) {
  92. $config = \Drupal::config('pathauto.settings');
  93. // Verify that the configuration data isn't null (as is the case before the
  94. // module's initialization, in tests), so that in_array() won't fail.
  95. if ($enabled_entity_types = $config->get('enabled_entity_types')) {
  96. if (in_array($entity_type->id(), $enabled_entity_types)) {
  97. $fields['path'] = BaseFieldDefinition::create('path')
  98. ->setCustomStorage(TRUE)
  99. ->setLabel(t('URL alias'))
  100. ->setTranslatable(TRUE)
  101. ->setComputed(TRUE)
  102. ->setDisplayOptions('form', array(
  103. 'type' => 'path',
  104. 'weight' => 30,
  105. ))
  106. ->setDisplayConfigurable('form', TRUE);
  107. return $fields;
  108. }
  109. }
  110. }
  111. /**
  112. * Validate the pattern field, to ensure it doesn't contain any characters that
  113. * are invalid in URLs.
  114. */
  115. function pathauto_pattern_validate($element, FormStateInterface $form_state) {
  116. if (isset($element['#value'])) {
  117. $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
  118. $invalid_characters = ['#', '?', '&'];
  119. $invalid_characters_used = [];
  120. foreach ($invalid_characters as $invalid_character) {
  121. if (strpos($element['#value'], $invalid_character) !== FALSE) {
  122. $invalid_characters_used[] = $invalid_character;
  123. }
  124. }
  125. if (!empty($invalid_characters_used)) {
  126. $form_state->setError($element, t('The %element-title is using the following invalid characters: @invalid-characters.', array('%element-title' => $title, '@invalid-characters' => implode(', ', $invalid_characters_used))));
  127. }
  128. if (preg_match('/(\s$)+/', $element['#value'])) {
  129. $form_state->setError($element, t('The %element-title doesn\'t allow the patterns ending with whitespace.', array('%element-title' => $title)));
  130. }
  131. }
  132. return $element;
  133. }