forked from larowlan/pathauto
-
Notifications
You must be signed in to change notification settings - Fork 34
/
pathauto.install
197 lines (172 loc) · 7.08 KB
/
pathauto.install
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* @file
* Install, update, and uninstall functions for Pathauto.
*
* @ingroup pathauto
*/
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Utility\UpdateException;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\pathauto\Entity\PathautoPattern;
/**
* Implements hook_install().
*/
function pathauto_install() {
// Set the weight to 1
module_set_weight('pathauto', 1);
}
/**
* Updates pathauto widgets to use the path widget ID.
*/
function pathauto_update_8001() {
// Replace values in the 'entity.definitions.installed' keyvalue collection.
$collection = \Drupal::service('keyvalue')->get('entity.definitions.installed');
foreach ($collection->getAll() as $key => $definitions) {
if (!is_array($definitions) || empty($definitions['path'])) {
continue;
}
// Retrieve and change path base field definition.
$path_definition = $definitions['path'];
if (($options = $path_definition->getDisplayOptions('form')) && $options['type'] = 'pathauto') {
$options['type'] = 'path';
$path_definition->setDisplayOptions('form', $options);
// Save the new value.
$collection->set($key, $definitions);
}
}
foreach (EntityFormDisplay::loadMultiple() as $form_display) {
if ($component = $form_display->getComponent('path')) {
if (isset($component['type']) && $component['type'] == 'pathauto') {
$component['type'] = 'path';
$form_display->setComponent('path', $component);
$form_display->save();
}
}
}
}
/**
* Converts patterns from configuration objects to configuration entities.
*/
function pathauto_update_8100(&$sandbox) {
if (!\Drupal::service('module_handler')->moduleExists('ctools')) {
throw new UpdateException('Install Chaos tools suite (https://www.drupal.org/project/ctools) before running this database update.');
}
$messages = array();
$entity_manager = \Drupal::service('entity.manager');
$entity_type_manager = \Drupal::service('entity_type.manager');
$language_manager = \Drupal::service('language_manager');
$entity_types = $entity_manager->getDefinitions();
// 1. Load all patterns.
$config = \Drupal::service('config.factory')->getEditable('pathauto.pattern');
$patterns = $config->get('patterns');
// 2. Create a configuration entity per pattern.
foreach ($patterns as $entity_type => $entity_patterns) {
if (!array_key_exists($entity_type, $entity_types)) {
// We found an unknown entity type. Report it.
$messages[] = t('Entity of type @type was not processed. It defines the following patterns: @patterns', array(
'@type' => $entity_type,
'@patterns' => print_r($entity_patterns, TRUE),
));
continue;
}
$entity_label = $entity_types[$entity_type]->getLabel();
if (isset($entity_patterns['default'])) {
// This is a pattern for an entity type, such as "node".
$pattern = PathautoPattern::create([
'id' => $entity_type,
'label' => $entity_label,
'type' => 'canonical_entities:' . $entity_type,
'pattern' => $entity_patterns['default'],
'weight' => 0,
]);
$pattern->save();
}
// Loop over bundles and create patterns if they have a value.
// Bundle keys may have a language suffix for language-dependant patterns.
if (isset($entity_patterns['bundles'])) {
$bundle_info = $entity_manager->getBundleInfo($entity_type);
foreach ($entity_patterns['bundles'] as $bundle => $bundle_patterns) {
if (empty($bundle_patterns['default'])) {
// This bundle does not define a pattern. Move on to the next one.
continue;
}
if (isset($bundle_info[$bundle])) {
// This is a pattern for a bundle, such as "node_article".
$pattern = PathautoPattern::create([
'id' => $entity_type . '_' . $bundle,
'label' => $entity_label . ' ' . $bundle_info[$bundle]['label'],
'type' => 'canonical_entities:' . $entity_type,
'pattern' => $bundle_patterns['default'],
'weight' => -5,
]);
// Add the bundle condition.
$pattern->addSelectionCondition([
'id' => 'entity_bundle:' . $entity_type,
'bundles' => array($bundle),
'negate' => FALSE,
'context_mapping' => [ $entity_type => $entity_type ],
]);
$pattern->save();
}
else {
// This is either a language dependent pattern such as "article_es" or
// an unknown bundle or langcode. Let's figure it out.
$matches = NULL;
$langcode = NULL;
preg_match('/^(.*)_([a-z-]*)$/', $bundle, $matches);
if (count($matches) == 3) {
list(, $extracted_bundle, $langcode) = $matches;
$language = $language_manager->getLanguage($langcode);
}
// Validate bundle, langcode and language.
if (!isset($bundle_info[$extracted_bundle]) || ($langcode == NULL) || ($language == NULL)) {
$messages[] = t('Unrecognized entity bundle @entity:@bundle was not processed. It defines the following patterns: @patterns', array(
'@entity' => $entity_type,
'@bundle' => $bundle,
'@patterns' => print_r($entity_patterns, TRUE),
));
continue;
}
// This is a pattern for a bundle and a language, such as "node_article_es".
$pattern = PathautoPattern::create([
'id' => $entity_type . '_' . $extracted_bundle . '_' . str_replace('-', '_', $langcode),
'label' => $entity_label . ' ' . $bundle_info[$extracted_bundle]['label'] . ' ' . $language->getName(),
'type' => 'canonical_entities:' . $entity_type,
'pattern' => $bundle_patterns['default'],
'weight' => -10,
]);
// Add the bundle condition.
$pattern->addSelectionCondition([
'id' => 'entity_bundle:' . $entity_type,
'bundles' => array($extracted_bundle => $extracted_bundle),
'negate' => FALSE,
'context_mapping' => [ $entity_type => $entity_type ],
]);
// Add the language condition.
$language_mapping = $entity_type . ':' . $entity_type_manager->getDefinition($entity_type)->getKey('langcode') . ':language';
$pattern->addSelectionCondition([
'id' => 'language',
'langcodes' => [ $langcode => $langcode ],
'negate' => FALSE,
'context_mapping' => [
'language' => $language_mapping,
]
]);
// Add the context relationship for this language.
$new_definition = new ContextDefinition('language', 'Language');
$new_context = new Context($new_definition);
$pattern->addContext($language_mapping, $new_context);
$pattern->save();
}
}
}
}
// 3. Delete the old configuration object that stores patterns.
$config->delete();
// 4. Print out messages.
if (!empty($messages)) {
return implode('</br>', $messages);
}
}