5 * @defgroup pathauto Pathauto: Automatically generates aliases for content
7 * The Pathauto module automatically generates path aliases for various kinds of
8 * content (nodes, categories, users) without requiring the user to manually
9 * specify the path alias. This allows you to get aliases like
10 * /category/my-node-title.html instead of /node/123. The aliases are based upon
11 * a "pattern" system which the administrator can control.
16 * Main file for the Pathauto module, which automatically generates aliases for content.
21 use Drupal\Core\Entity\ContentEntityInterface;
22 use Drupal\Core\Entity\EntityInterface;
23 use Drupal\Core\Entity\EntityTypeInterface;
24 use Drupal\Core\Field\BaseFieldDefinition;
25 use Drupal\Core\Form\FormStateInterface;
26 use Drupal\Core\Routing\RouteMatchInterface;
28 use Drupal\pathauto\PathautoState;
31 * The default ignore word list.
33 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');
36 * Implements hook_hook_info().
38 function pathauto_hook_info() {
40 'pathauto_pattern_alter',
41 'pathauto_alias_alter',
42 'pathauto_is_alias_reserved',
44 return array_fill_keys($hooks, array('group' => 'pathauto'));
48 * Implements hook_help().
50 function pathauto_help($route_name, RouteMatchInterface $route_match) {
51 switch ($route_name) {
52 case 'help.page.pathauto':
53 $output = '<h3>' . t('About') . '</h3>';
54 $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>';
56 $output .= '<h3>' . t('Uses') . '</h3>';
57 $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>';
58 $output .= '<dt>' . t('Creating Pathauto Patterns') . '</dt>';
59 $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>';
60 $output .= '<dt>' . t('Pathauto Settings') . '</dt>';
61 $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>';
62 $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>';
63 $output .= '<dt>' . t('Bulk Generation') . '</dt>';
64 $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>';
65 $output .= '<dt>' . t('Delete Aliases') . '</dt>';
66 $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>';
70 case 'entity.pathauto_pattern.collection':
71 $output = '<p>' . t('This page provides a list of all patterns on the site and allows you to edit and reorder them.') . '</p>';
74 case 'entity.pathauto_pattern.add_form':
75 $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>';
78 case 'pathauto.bulk.update.form':
79 $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>';
80 $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>';
81 $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>';
87 * Implements hook_entity_insert().
89 function pathauto_entity_insert(EntityInterface $entity) {
90 \Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'insert');
94 * Implements hook_entity_update().
96 function pathauto_entity_update(EntityInterface $entity) {
97 \Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'update');
101 * Implements hook_entity_update().
103 function pathauto_entity_delete(EntityInterface $entity) {
104 if ($entity->hasLinkTemplate('canonical') && $entity instanceof ContentEntityInterface && $entity->hasField('path')) {
105 \Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($entity);
106 $entity->path->first()->get('pathauto')->purge();
111 * Implements hook_field_info_alter().
113 function pathauto_field_info_alter(&$info) {
114 $info['path']['class'] = '\Drupal\pathauto\PathautoItem';
118 * Implements hook_field_widget_info_alter().
120 function pathauto_field_widget_info_alter(&$widgets) {
121 $widgets['path']['class'] = 'Drupal\pathauto\PathautoWidget';
125 * Implements hook_entity_base_field_info().
127 function pathauto_entity_base_field_info(EntityTypeInterface $entity_type) {
128 $config = \Drupal::config('pathauto.settings');
129 // Verify that the configuration data isn't null (as is the case before the
130 // module's initialization, in tests), so that in_array() won't fail.
131 if ($enabled_entity_types = $config->get('enabled_entity_types')) {
132 if (in_array($entity_type->id(), $enabled_entity_types)) {
133 $fields['path'] = BaseFieldDefinition::create('path')
134 ->setCustomStorage(TRUE)
135 ->setLabel(t('URL alias'))
136 ->setTranslatable(TRUE)
138 ->setDisplayOptions('form', array(
142 ->setDisplayConfigurable('form', TRUE);
150 * Implements hook_entity_base_field_info_alter().
152 function pathauto_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
153 if (isset($fields['path'])) {
154 // Path fields need to be computed so that the pathauto state can be
155 // accessed even if there is no alias being set.
156 $fields['path']->setComputed(TRUE);
161 * Validate the pattern field, to ensure it doesn't contain any characters that
162 * are invalid in URLs.
164 function pathauto_pattern_validate($element, FormStateInterface $form_state) {
166 if (isset($element['#value'])) {
167 $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
168 $invalid_characters = ['#', '?', '&'];
169 $invalid_characters_used = [];
171 foreach ($invalid_characters as $invalid_character) {
172 if (strpos($element['#value'], $invalid_character) !== FALSE) {
173 $invalid_characters_used[] = $invalid_character;
177 if (!empty($invalid_characters_used)) {
178 $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))));
181 if (preg_match('/(\s$)+/', $element['#value'])) {
182 $form_state->setError($element, t('The %element-title doesn\'t allow the patterns ending with whitespace.', array('%element-title' => $title)));