taxonomy.install 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the taxonomy module.
  5. */
  6. use Drupal\Core\Field\BaseFieldDefinition;
  7. use Drupal\Core\Site\Settings;
  8. /**
  9. * Convert the custom taxonomy term hierarchy storage to a default storage.
  10. */
  11. function taxonomy_update_8501() {
  12. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  13. /** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */
  14. $field_storage_definition = $definition_update_manager->getFieldStorageDefinition('parent', 'taxonomy_term');
  15. $field_storage_definition->setCustomStorage(FALSE);
  16. $definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
  17. }
  18. /**
  19. * Copy hierarchy from {taxonomy_term_hierarchy} to {taxonomy_term__parent}.
  20. */
  21. function taxonomy_update_8502(&$sandbox) {
  22. $database = \Drupal::database();
  23. if (!isset($sandbox['current'])) {
  24. // Set batch ops sandbox.
  25. $sandbox['current'] = 0;
  26. $sandbox['tid'] = -1;
  27. $sandbox['delta'] = 0;
  28. $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
  29. $sandbox['max'] = $database->select('taxonomy_term_hierarchy')
  30. ->countQuery()
  31. ->execute()
  32. ->fetchField();
  33. }
  34. // Save the hierarchy.
  35. $select = $database->select('taxonomy_term_hierarchy', 'h');
  36. $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid');
  37. $hierarchy = $select
  38. ->fields('h', ['tid', 'parent'])
  39. ->fields('d', ['vid', 'langcode'])
  40. ->range($sandbox['current'], $sandbox['limit'])
  41. ->orderBy('tid', 'ASC')
  42. ->orderBy('parent', 'ASC')
  43. ->execute()
  44. ->fetchAll();
  45. // Restore data.
  46. $insert = $database->insert('taxonomy_term__parent')
  47. ->fields(['bundle', 'entity_id', 'revision_id', 'langcode', 'delta', 'parent_target_id']);
  48. foreach ($hierarchy as $row) {
  49. if ($row->tid !== $sandbox['tid']) {
  50. $sandbox['delta'] = 0;
  51. $sandbox['tid'] = $row->tid;
  52. }
  53. $insert->values([
  54. 'bundle' => $row->vid,
  55. 'entity_id' => $row->tid,
  56. 'revision_id' => $row->tid,
  57. 'langcode' => $row->langcode,
  58. 'delta' => $sandbox['delta'],
  59. 'parent_target_id' => $row->parent,
  60. ]);
  61. $sandbox['delta']++;
  62. $sandbox['current']++;
  63. }
  64. $insert->execute();
  65. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
  66. if ($sandbox['#finished'] >= 1) {
  67. // Update the entity type because the 'taxonomy_term_hierarchy' table is no
  68. // longer part of its shared tables schema.
  69. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  70. $definition_update_manager->updateEntityType($definition_update_manager->getEntityType('taxonomy_term'));
  71. // \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::onEntityTypeUpdate()
  72. // only deletes *known* entity tables (i.e. the base, data and revision
  73. // tables), so we have to drop it manually.
  74. $database->schema()->dropTable('taxonomy_term_hierarchy');
  75. return t('Taxonomy term hierarchy has been converted to default entity reference storage.');
  76. }
  77. }
  78. /**
  79. * Update views to use {taxonomy_term__parent} in relationships.
  80. */
  81. function taxonomy_update_8503() {
  82. $config_factory = \Drupal::configFactory();
  83. foreach ($config_factory->listAll('views.view.') as $id) {
  84. $view = $config_factory->getEditable($id);
  85. foreach (array_keys($view->get('display')) as $display_id) {
  86. $changed = FALSE;
  87. foreach (['relationships', 'filters', 'arguments'] as $handler_type) {
  88. $base_path = "display.$display_id.display_options.$handler_type";
  89. $handlers = $view->get($base_path);
  90. if (!$handlers) {
  91. continue;
  92. }
  93. foreach ($handlers as $handler_key => $handler_config) {
  94. $table_path = "$base_path.$handler_key.table";
  95. $field_path = "$base_path.$handler_key.field";
  96. $table = $view->get($table_path);
  97. $field = $view->get($field_path);
  98. if (($table && ($table === 'taxonomy_term_hierarchy')) && ($field && ($field === 'parent'))) {
  99. $view->set($table_path, 'taxonomy_term__parent');
  100. $view->set($field_path, 'parent_target_id');
  101. $changed = TRUE;
  102. }
  103. }
  104. }
  105. if ($changed) {
  106. $view->save(TRUE);
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Add the publishing status fields to taxonomy terms.
  113. */
  114. function taxonomy_update_8601() {
  115. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  116. $entity_type = $definition_update_manager->getEntityType('taxonomy_term');
  117. // Bail out early if a field named 'status' is already installed.
  118. if ($definition_update_manager->getFieldStorageDefinition('status', 'taxonomy_term')) {
  119. $message = \Drupal::state()->get('taxonomy_update_8601_skip_message', t('The publishing status field has <strong>not</strong> been added to taxonomy terms. See <a href=":link">this page</a> for more information on how to install it.', [
  120. ':link' => 'https://www.drupal.org/node/2985366',
  121. ]));
  122. return $message;
  123. }
  124. // Add the 'published' entity key to the taxonomy_term entity type.
  125. $entity_keys = $entity_type->getKeys();
  126. $entity_keys['published'] = 'status';
  127. $entity_type->set('entity_keys', $entity_keys);
  128. $definition_update_manager->updateEntityType($entity_type);
  129. // Add the status field.
  130. $status = BaseFieldDefinition::create('boolean')
  131. ->setLabel(t('Publishing status'))
  132. ->setDescription(t('A boolean indicating the published state.'))
  133. ->setRevisionable(TRUE)
  134. ->setTranslatable(TRUE)
  135. ->setDefaultValue(TRUE);
  136. $has_content_translation_status_field = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
  137. if ($has_content_translation_status_field) {
  138. $status->setInitialValueFromField('content_translation_status', TRUE);
  139. }
  140. else {
  141. $status->setInitialValue(TRUE);
  142. }
  143. $definition_update_manager->installFieldStorageDefinition('status', 'taxonomy_term', 'taxonomy_term', $status);
  144. // Uninstall the 'content_translation_status' field if needed.
  145. if ($has_content_translation_status_field) {
  146. $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
  147. $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
  148. }
  149. return t('The publishing status field has been added to taxonomy terms.');
  150. }