Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / pathauto.install
1 <?php
2
3 /**
4  * @file
5  * Install, update, and uninstall functions for Pathauto.
6  *
7  * @ingroup pathauto
8  */
9
10 use Drupal\Core\Entity\Entity\EntityFormDisplay;
11 use Drupal\pathauto\Entity\PathautoPattern;
12
13 /**
14  * Implements hook_install().
15  */
16 function pathauto_install() {
17   // Set the weight to 1.
18   module_set_weight('pathauto', 1);
19
20   // Ensure the url_alias table exists.
21   _pathauto_ensure_url_alias_table_exists();
22 }
23
24 /**
25  * Helper function to ensure the url_alias table exists.
26  *
27  * Only necessary on Drupal 8.1.x.
28  *
29  * @see https://www.drupal.org/node/2704821
30  */
31 function _pathauto_ensure_url_alias_table_exists() {
32   $alias_storage = \Drupal::service('path.alias_storage');
33   if (method_exists($alias_storage, 'schemaDefinition')) {
34     $database_schema = \Drupal::database()->schema();
35     if (!$database_schema->tableExists($alias_storage::TABLE)) {
36       $schema_definition = $alias_storage->schemaDefinition();
37       $database_schema->createTable($alias_storage::TABLE, $schema_definition);
38     }
39   }
40 }
41
42 /**
43  * Updates pathauto widgets to use the path widget ID.
44  */
45 function pathauto_update_8001() {
46
47   // Replace values in the 'entity.definitions.installed' keyvalue collection.
48   $collection = \Drupal::service('keyvalue')->get('entity.definitions.installed');
49   foreach ($collection->getAll() as $key => $definitions) {
50     if (!is_array($definitions) || empty($definitions['path'])) {
51       continue;
52     }
53
54     // Retrieve and change path base field definition.
55     $path_definition = $definitions['path'];
56     if (($options = $path_definition->getDisplayOptions('form')) && $options['type'] = 'pathauto') {
57       $options['type'] = 'path';
58       $path_definition->setDisplayOptions('form', $options);
59       // Save the new value.
60       $collection->set($key, $definitions);
61     }
62
63   }
64
65   foreach (EntityFormDisplay::loadMultiple() as $form_display) {
66     if ($component = $form_display->getComponent('path')) {
67       if (isset($component['type']) && $component['type'] == 'pathauto') {
68         $component['type'] = 'path';
69         $form_display->setComponent('path', $component);
70         $form_display->save();
71       }
72     }
73   }
74 }
75
76 /**
77  * Converts patterns from configuration objects to configuration entities.
78  */
79 function pathauto_update_8100() {
80   \Drupal::service('module_installer')->install(['ctools']);
81
82   $messages = array();
83   /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_bundle_info */
84   $entity_bundle_info = \Drupal::service('entity_type.bundle.info');
85   $entity_type_manager = \Drupal::entityTypeManager();
86   $language_manager = \Drupal::languageManager();
87   $entity_type_manager->clearCachedDefinitions();
88   \Drupal::service('plugin.manager.alias_type')->clearCachedDefinitions();
89   $entity_types = $entity_type_manager->getDefinitions();
90
91   // 1. Load all patterns.
92   $config = \Drupal::configFactory()->getEditable('pathauto.pattern');
93   $patterns = $config->get('patterns');
94
95   // 2. Create a configuration entity per pattern.
96   foreach ($patterns as $entity_type => $entity_patterns) {
97     if (!array_key_exists($entity_type, $entity_types)) {
98       // We found an unknown entity type. Report it.
99       $messages[] = t('Entity of type @type was not processed. It defines the following patterns: @patterns', array(
100         '@type' => $entity_type,
101         '@patterns' => print_r($entity_patterns, TRUE),
102       ));
103       continue;
104     }
105     $entity_label = $entity_types[$entity_type]->getLabel();
106
107     if (!empty($entity_patterns['default'])) {
108       // This is a pattern for an entity type, such as "node".
109       $pattern = PathautoPattern::create([
110         'id' => $entity_type,
111         'label' => $entity_label,
112         'type' => 'canonical_entities:' . $entity_type,
113         'pattern' => $entity_patterns['default'],
114         'weight' => 0,
115       ]);
116       $pattern->save();
117     }
118
119     // Loop over bundles and create patterns if they have a value.
120     // Bundle keys may have a language suffix for language-dependant patterns.
121     if (isset($entity_patterns['bundles'])) {
122       $bundle_info = $entity_bundle_info->getBundleInfo($entity_type);
123       foreach ($entity_patterns['bundles'] as $bundle => $bundle_patterns) {
124         if (empty($bundle_patterns['default'])) {
125           // This bundle does not define a pattern. Move on to the next one.
126           continue;
127         }
128
129         if (isset($bundle_info[$bundle])) {
130           // This is a pattern for a bundle, such as "node_article".
131           $pattern = PathautoPattern::create([
132             'id' => $entity_type . '_' . $bundle,
133             'label' => $entity_label . ' ' . $bundle_info[$bundle]['label'],
134             'type' => 'canonical_entities:' . $entity_type,
135             'pattern' => $bundle_patterns['default'],
136             'weight' => -5,
137           ]);
138
139           // Add the bundle condition.
140           $pattern->addSelectionCondition([
141             'id' => 'entity_bundle:' . $entity_type,
142             'bundles' => array($bundle => $bundle),
143             'negate' => FALSE,
144             'context_mapping' => [ $entity_type => $entity_type ],
145           ]);
146
147           $pattern->save();
148         }
149         else {
150           // This is either a language dependent pattern such as "article_es" or
151           // an unknown bundle or langcode. Let's figure it out.
152           $matches = NULL;
153           $langcode = NULL;
154           $extracted_bundle = NULL;
155           $language = NULL;
156           preg_match('/^(.*)_([a-z-]*)$/', $bundle, $matches);
157           if (count($matches) == 3) {
158             list(, $extracted_bundle, $langcode) = $matches;
159             $language = $language_manager->getLanguage($langcode);
160           }
161           // Validate bundle, langcode and language.
162           if (!isset($bundle_info[$extracted_bundle]) || ($langcode == NULL) || ($language == NULL)) {
163             $messages[] = t('Unrecognized entity bundle @entity:@bundle was not processed. It defines the following patterns: @patterns', array(
164               '@entity' => $entity_type,
165               '@bundle' => $bundle,
166               '@patterns' => print_r($entity_patterns, TRUE),
167             ));
168             continue;
169           }
170
171           // This is a pattern for a bundle and a language, such as
172           // "node_article_es".
173           $pattern = PathautoPattern::create([
174             'id' => $entity_type . '_' . $extracted_bundle . '_' . str_replace('-', '_', $langcode),
175             'label' => $entity_label . ' ' . $bundle_info[$extracted_bundle]['label'] . ' ' . $language->getName(),
176             'type' => 'canonical_entities:' . $entity_type,
177             'pattern' => $bundle_patterns['default'],
178             'weight' => -10,
179           ]);
180
181           // Add the bundle condition.
182           $pattern->addSelectionCondition([
183             'id' => 'entity_bundle:' . $entity_type,
184             'bundles' => array($extracted_bundle => $extracted_bundle),
185             'negate' => FALSE,
186             'context_mapping' => [ $entity_type => $entity_type ],
187           ]);
188
189           // Add the language condition.
190           $language_mapping = $entity_type . ':' . $entity_type_manager->getDefinition($entity_type)->getKey('langcode') . ':language';
191           $pattern->addSelectionCondition([
192             'id' => 'language',
193             'langcodes' => [ $langcode => $langcode ],
194             'negate' => FALSE,
195             'context_mapping' => [
196               'language' => $language_mapping,
197             ]
198           ]);
199
200           // Add the context relationship for this language.
201           $pattern->addRelationship($language_mapping, 'Language');
202
203           $pattern->save();
204         }
205       }
206     }
207   }
208
209   // 3. Delete the old configuration object that stores patterns.
210   $config->delete();
211
212   // 4. Print out messages.
213   if (!empty($messages)) {
214     return implode('</br>', $messages);
215   }
216 }
217
218 /**
219  * Update relationship storage.
220  */
221 function pathauto_update_8101() {
222   foreach (\Drupal::configFactory()->listAll('pathauto.pattern.') as $pattern_config_name) {
223     $pattern_config = \Drupal::configFactory()->getEditable($pattern_config_name);
224
225     $relationships = [];
226     foreach ((array) $pattern_config->get('context_definitions') as $context_definition) {
227       $relationships[$context_definition['id']] = ['label' => $context_definition['label']];
228     }
229
230     $pattern_config->clear('context_definitions');
231     $pattern_config->set('relationships', $relationships);
232     $pattern_config->save();
233   }
234 }
235
236 /**
237  * Update node type conditions from entity_bundle to node_type.
238  */
239 function pathauto_update_8102() {
240   // Load all pattern configuration entities.
241   foreach (\Drupal::configFactory()->listAll('pathauto.pattern.') as $pattern_config_name) {
242     $pattern_config = \Drupal::configFactory()->getEditable($pattern_config_name);
243
244     // Loop patterns and swap the entity_bundle:node plugin by the node_type
245     // plugin.
246     if ($pattern_config->get('type') == 'canonical_entities:node') {
247       $selection_criteria = $pattern_config->get('selection_criteria');
248       foreach ($selection_criteria as $uuid => $condition) {
249         if ($condition['id'] == 'entity_bundle:node') {
250           $selection_criteria[$uuid]['id'] = 'node_type';
251           $pattern_config->set('selection_criteria', $selection_criteria);
252           $pattern_config->save();
253           break;
254         }
255       }
256     }
257   }
258 }
259
260 /**
261  * Fix invalid default value for ignore_words.
262  */
263 function pathauto_update_8103() {
264   $config_factory = \Drupal::configFactory();
265   $config = $config_factory->getEditable('pathauto.settings');
266   $ignore_words = $config->get('ignore_words');
267   if ($ignore_words === ', in, is,that, the  , this, with, ') {
268     $config->set('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')->save(TRUE);
269   }
270 }
271
272 /**
273  * Resave patterns so that lookup keys are updated.
274  */
275 function pathauto_update_8104() {
276   \Drupal::entityTypeManager()->clearCachedDefinitions();
277   // Load all pattern configuration entities and save them, so that the new
278   // status lookup keys are saved.
279   foreach (\Drupal::configFactory()->listAll('pathauto.pattern.') as $pattern_config_name) {
280     $pattern_config = \Drupal::configFactory()->getEditable($pattern_config_name);
281     $pattern_config->save();
282   }
283 }
284
285 /**
286  * Ensure the url_alias table exists.
287  */
288 function pathauto_update_8105() {
289   _pathauto_ensure_url_alias_table_exists();
290 }
291
292 /**
293  * Update default configuration for enabled entity types.
294  */
295 function pathauto_update_8106() {
296   $config_factory = \Drupal::configFactory();
297   $config = $config_factory->getEditable('pathauto.settings');
298   $config->set('enabled_entity_types', ['user']);
299   $config->save();
300 }
301
302 /**
303  * Initialize the new safe tokens setting.
304  */
305 function pathauto_update_8107() {
306
307   $safe_tokens = [
308     'alias',
309     'alias',
310     'path',
311     'join-path',
312     'login-url',
313     'url',
314     'url-brief',
315   ];
316
317   \Drupal::configFactory()->getEditable('pathauto.settings')
318     ->set('safe_tokens', $safe_tokens)
319     ->save();
320 }