pathauto.install 11 KB

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