annotate core/modules/taxonomy/taxonomy.install @ 5:12f9dff5fda9 tip

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