taxonomy.install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // Count records using a join, as there might be orphans in the hierarchy
  30. // table. See https://www.drupal.org/project/drupal/issues/2997982.
  31. $select = $database->select('taxonomy_term_hierarchy', 'h');
  32. $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid');
  33. $sandbox['max'] = $select
  34. ->countQuery()
  35. ->execute()
  36. ->fetchField();
  37. }
  38. // Save the hierarchy.
  39. $select = $database->select('taxonomy_term_hierarchy', 'h');
  40. $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid');
  41. $hierarchy = $select
  42. ->fields('h', ['tid', 'parent'])
  43. ->fields('d', ['vid', 'langcode'])
  44. ->range($sandbox['current'], $sandbox['limit'])
  45. ->orderBy('tid', 'ASC')
  46. ->orderBy('parent', 'ASC')
  47. ->execute()
  48. ->fetchAll();
  49. // Restore data.
  50. $insert = $database->insert('taxonomy_term__parent')
  51. ->fields(['bundle', 'entity_id', 'revision_id', 'langcode', 'delta', 'parent_target_id']);
  52. foreach ($hierarchy as $row) {
  53. if ($row->tid !== $sandbox['tid']) {
  54. $sandbox['delta'] = 0;
  55. $sandbox['tid'] = $row->tid;
  56. }
  57. $insert->values([
  58. 'bundle' => $row->vid,
  59. 'entity_id' => $row->tid,
  60. 'revision_id' => $row->tid,
  61. 'langcode' => $row->langcode,
  62. 'delta' => $sandbox['delta'],
  63. 'parent_target_id' => $row->parent,
  64. ]);
  65. $sandbox['delta']++;
  66. $sandbox['current']++;
  67. }
  68. $insert->execute();
  69. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
  70. if ($sandbox['#finished'] >= 1) {
  71. // Update the entity type because the 'taxonomy_term_hierarchy' table is no
  72. // longer part of its shared tables schema.
  73. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  74. $definition_update_manager->updateEntityType($definition_update_manager->getEntityType('taxonomy_term'));
  75. // \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::onEntityTypeUpdate()
  76. // only deletes *known* entity tables (i.e. the base, data and revision
  77. // tables), so we have to drop it manually.
  78. $database->schema()->dropTable('taxonomy_term_hierarchy');
  79. return t('Taxonomy term hierarchy has been converted to default entity reference storage.');
  80. }
  81. }
  82. /**
  83. * Update views to use {taxonomy_term__parent} in relationships.
  84. */
  85. function taxonomy_update_8503() {
  86. $config_factory = \Drupal::configFactory();
  87. foreach ($config_factory->listAll('views.view.') as $id) {
  88. $view = $config_factory->getEditable($id);
  89. foreach (array_keys($view->get('display')) as $display_id) {
  90. $changed = FALSE;
  91. foreach (['relationships', 'filters', 'arguments'] as $handler_type) {
  92. $base_path = "display.$display_id.display_options.$handler_type";
  93. $handlers = $view->get($base_path);
  94. if (!$handlers) {
  95. continue;
  96. }
  97. foreach ($handlers as $handler_key => $handler_config) {
  98. $table_path = "$base_path.$handler_key.table";
  99. $field_path = "$base_path.$handler_key.field";
  100. $table = $view->get($table_path);
  101. $field = $view->get($field_path);
  102. if (($table && ($table === 'taxonomy_term_hierarchy')) && ($field && ($field === 'parent'))) {
  103. $view->set($table_path, 'taxonomy_term__parent');
  104. $view->set($field_path, 'parent_target_id');
  105. $changed = TRUE;
  106. }
  107. }
  108. }
  109. if ($changed) {
  110. $view->save(TRUE);
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Add the publishing status fields to taxonomy terms.
  117. */
  118. function taxonomy_update_8601() {
  119. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  120. $entity_type = $definition_update_manager->getEntityType('taxonomy_term');
  121. // Bail out early if a field named 'status' is already installed.
  122. if ($definition_update_manager->getFieldStorageDefinition('status', 'taxonomy_term')) {
  123. $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.', [
  124. ':link' => 'https://www.drupal.org/node/2985366',
  125. ]));
  126. return $message;
  127. }
  128. // Add the 'published' entity key to the taxonomy_term entity type.
  129. $entity_keys = $entity_type->getKeys();
  130. $entity_keys['published'] = 'status';
  131. $entity_type->set('entity_keys', $entity_keys);
  132. $definition_update_manager->updateEntityType($entity_type);
  133. // Add the status field.
  134. $status = BaseFieldDefinition::create('boolean')
  135. ->setLabel(t('Publishing status'))
  136. ->setDescription(t('A boolean indicating the published state.'))
  137. ->setRevisionable(TRUE)
  138. ->setTranslatable(TRUE)
  139. ->setDefaultValue(TRUE);
  140. $has_content_translation_status_field = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
  141. if ($has_content_translation_status_field) {
  142. $status->setInitialValueFromField('content_translation_status', TRUE);
  143. }
  144. else {
  145. $status->setInitialValue(TRUE);
  146. }
  147. $definition_update_manager->installFieldStorageDefinition('status', 'taxonomy_term', 'taxonomy_term', $status);
  148. // Uninstall the 'content_translation_status' field if needed.
  149. if ($has_content_translation_status_field) {
  150. $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
  151. $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
  152. }
  153. return t('The publishing status field has been added to taxonomy terms.');
  154. }