forked from md-systems/pathauto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathauto.module
158 lines (140 loc) · 5.81 KB
/
pathauto.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @defgroup pathauto Pathauto: Automatically generates aliases for content
*
* The Pathauto module automatically generates path aliases for various kinds of
* content (nodes, categories, users) without requiring the user to manually
* specify the path alias. This allows you to get aliases like
* /category/my-node-title.html instead of /node/123. The aliases are based upon
* a "pattern" system which the administrator can control.
*/
/**
* @file
* Main file for the Pathauto module, which automatically generates aliases for content.
*
* @ingroup pathauto
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* The default ignore word list.
*/
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');
/**
* Implements hook_hook_info().
*/
function pathauto_hook_info() {
$hooks = array(
'pathauto_pattern_alter',
'pathauto_alias_alter',
'pathauto_is_alias_reserved',
);
return array_fill_keys($hooks, array('group' => 'pathauto'));
}
/**
* Implements hook_help().
*/
function pathauto_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.pathauto':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides a mechanism for modules to automatically generate aliases for the content they manage.') . '</p>';
$output .= '<h3>' . t('Settings') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Maximum alias and component length') . '</dt>';
$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. This length is limited by the length of the "alias" column of the url_alias database table. The default database schema for this column is @max. If you set a length that is equal to that of the one set in the "alias" column it will cause problems in situations where the system needs to append additional words to the aliased URL. You should enter a value that is the length of the "alias" column minus the length of any strings that might get added to the end of the URL. The length of strings that might get added to the end of your URLs depends on which modules you have enabled and on your Pathauto settings. The recommended and default value is 100.', array('@max' => \Drupal::service('pathauto.alias_storage_helper')->getAliasSchemaMaxlength())) . '</dd>';
$output .= '</dl>';
return $output;
case 'pathauto.bulk.update.form':
$output = '<p>' . t('Bulk generation will only 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.') . '</p>';
return $output;
}
}
/**
* Implements hook__entity_bundle_delete().
*/
function pathauto_entity_bundle_delete($entity_type, $bundle) {
$config = \Drupal::configFactory()->getEditable('pathauto.pattern');
$config->clear('patterns.' . $entity_type . '.bundles.' . $bundle);
$config->save();
}
/**
* Implements hook_entity_presave().
*/
function pathauto_entity_presave($entity) {
if (!($entity instanceof ContentEntityInterface) || $entity->hasField('path')) {
return;
}
// About to be saved (before insert/update)
if (!empty($entity->path->pathauto) && isset($entity->path->old_alias)
&& $entity->path->alias == '' && $entity->path->old_alias != '') {
/*
* There was an old alias, but when pathauto_perform_alias was checked
* the javascript disabled the textbox which led to an empty value being
* submitted. Restoring the old path-value here prevents the Path module
* from deleting any old alias before Pathauto gets control.
*/
$entity->path->alias = $entity->path->old_alias;
}
// Help prevent errors with progromatically creating entities by defining
// path['alias'] as an empty string.
// @see http://drupal.org/node/1328180
// @see http://drupal.org/node/1576552
if (isset($entity->path->pathauto) && !isset($entity->path->alias)) {
$entity->path->alias = '';
}
}
/**
* Implements hook_entity_insert().
*/
function pathauto_entity_insert(EntityInterface $entity) {
\Drupal::service('pathauto.manager')->updateAlias($entity, 'insert');
}
/**
* Implements hook_entity_update().
*/
function pathauto_entity_update(EntityInterface $entity) {
\Drupal::service('pathauto.manager')->updateAlias($entity, 'update');
}
/**
* Implements hook_entity_update().
*/
function pathauto_entity_delete(EntityInterface $entity) {
if ($entity->hasLinkTemplate('canonical')) {
\Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($entity);
}
}
/**
* Implements hook_field_info_alter().
*/
function pathauto_field_info_alter(&$info) {
$info['path']['class'] = '\Drupal\pathauto\PathautoItem';
}
/**
* Implements hook_field_widget_info_alter().
*/
function pathauto_field_widget_info_alter(&$widgets) {
$widgets['path']['class'] = 'Drupal\pathauto\PathautoWidget';
}
/**
* Implements hook_entity_base_field_info().
*/
function pathauto_entity_base_field_info(EntityTypeInterface $entity_type) {
// @todo: Make this configurable and/or remove if
// https://drupal.org/node/476294 is resolved.
if ($entity_type->id() === 'user') {
$fields['path'] = BaseFieldDefinition::create('path')
->setCustomStorage(TRUE)
->setLabel(t('URL alias'))
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'type' => 'path',
'weight' => 30,
))
->setDisplayConfigurable('form', TRUE);
return $fields;
}
}