Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / pathauto / pathauto.module
1 <?php
2
3 /**
4  * @file
5  * @defgroup pathauto Pathauto: Automatically generates aliases for content
6  *
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.
12  */
13
14 /**
15  * @file
16  * Main file for the Pathauto module, which automatically generates aliases for content.
17  *
18  * @ingroup pathauto
19  */
20
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;
27 use Drupal\Core\Url;
28 use Drupal\pathauto\PathautoState;
29
30 /**
31  * The default ignore word list.
32  */
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');
34
35 /**
36  * Implements hook_hook_info().
37  */
38 function pathauto_hook_info() {
39   $hooks = array(
40     'pathauto_pattern_alter',
41     'pathauto_alias_alter',
42     'pathauto_is_alias_reserved',
43   );
44   return array_fill_keys($hooks, array('group' => 'pathauto'));
45 }
46
47 /**
48  * Implements hook_help().
49  */
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>';
55       $output .= '<dl>';
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>';
67       $output .= '</dl>';
68       return $output;
69
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>';
72       return $output;
73
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>';
76       return $output;
77
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>';
82       return $output;
83   }
84 }
85
86 /**
87  * Implements hook_entity_insert().
88  */
89 function pathauto_entity_insert(EntityInterface $entity) {
90   \Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'insert');
91 }
92
93 /**
94  * Implements hook_entity_update().
95  */
96 function pathauto_entity_update(EntityInterface $entity) {
97   \Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'update');
98 }
99
100 /**
101  * Implements hook_entity_update().
102  */
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();
107   }
108 }
109
110 /**
111  * Implements hook_field_info_alter().
112  */
113 function pathauto_field_info_alter(&$info) {
114   $info['path']['class'] = '\Drupal\pathauto\PathautoItem';
115 }
116
117 /**
118  * Implements hook_field_widget_info_alter().
119  */
120 function pathauto_field_widget_info_alter(&$widgets) {
121   $widgets['path']['class'] = 'Drupal\pathauto\PathautoWidget';
122 }
123
124 /**
125  * Implements hook_entity_base_field_info().
126  */
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)
137         ->setComputed(TRUE)
138         ->setDisplayOptions('form', array(
139           'type' => 'path',
140           'weight' => 30,
141         ))
142         ->setDisplayConfigurable('form', TRUE);
143
144       return $fields;
145     }
146   }
147 }
148
149 /**
150  * Implements hook_entity_base_field_info_alter().
151  */
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);
157   }
158 }
159
160 /**
161  * Validate the pattern field, to ensure it doesn't contain any characters that
162  * are invalid in URLs.
163  */
164 function pathauto_pattern_validate($element, FormStateInterface $form_state) {
165
166   if (isset($element['#value'])) {
167     $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
168     $invalid_characters = ['#', '?', '&'];
169     $invalid_characters_used = [];
170
171     foreach ($invalid_characters as $invalid_character) {
172       if (strpos($element['#value'], $invalid_character) !== FALSE) {
173         $invalid_characters_used[] = $invalid_character;
174       }
175     }
176
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))));
179     }
180
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)));
183     }
184   }
185
186   return $element;
187
188 }