annotate core/modules/taxonomy/taxonomy.install @ 19:fa3358dc1485 tip

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