5 * Install, update and uninstall functions for the taxonomy module.
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\Core\Site\Settings;
12 * Convert the custom taxonomy term hierarchy storage to a default storage.
14 function taxonomy_update_8501() {
15 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
17 /** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */
18 $field_storage_definition = $definition_update_manager->getFieldStorageDefinition('parent', 'taxonomy_term');
19 $field_storage_definition->setCustomStorage(FALSE);
20 $definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
24 * Copy hierarchy from {taxonomy_term_hierarchy} to {taxonomy_term__parent}.
26 function taxonomy_update_8502(&$sandbox) {
27 $database = \Drupal::database();
29 if (!isset($sandbox['current'])) {
30 // Set batch ops sandbox.
31 $sandbox['current'] = 0;
33 $sandbox['delta'] = 0;
34 $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
36 // Count records using a join, as there might be orphans in the hierarchy
37 // table. See https://www.drupal.org/project/drupal/issues/2997982.
38 $select = $database->select('taxonomy_term_hierarchy', 'h');
39 $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid');
40 $sandbox['max'] = $select
46 // Save the hierarchy.
47 $select = $database->select('taxonomy_term_hierarchy', 'h');
48 $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid');
50 ->fields('h', ['tid', 'parent'])
51 ->fields('d', ['vid', 'langcode'])
52 ->range($sandbox['current'], $sandbox['limit'])
53 ->orderBy('tid', 'ASC')
54 ->orderBy('parent', 'ASC')
59 $insert = $database->insert('taxonomy_term__parent')
60 ->fields(['bundle', 'entity_id', 'revision_id', 'langcode', 'delta', 'parent_target_id']);
62 foreach ($hierarchy as $row) {
63 if ($row->tid !== $sandbox['tid']) {
64 $sandbox['delta'] = 0;
65 $sandbox['tid'] = $row->tid;
69 'bundle' => $row->vid,
70 'entity_id' => $row->tid,
71 'revision_id' => $row->tid,
72 'langcode' => $row->langcode,
73 'delta' => $sandbox['delta'],
74 'parent_target_id' => $row->parent,
78 $sandbox['current']++;
83 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
85 if ($sandbox['#finished'] >= 1) {
86 // Update the entity type because the 'taxonomy_term_hierarchy' table is no
87 // longer part of its shared tables schema.
88 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
89 $definition_update_manager->updateEntityType($definition_update_manager->getEntityType('taxonomy_term'));
91 // \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::onEntityTypeUpdate()
92 // only deletes *known* entity tables (i.e. the base, data and revision
93 // tables), so we have to drop it manually.
94 $database->schema()->dropTable('taxonomy_term_hierarchy');
96 return t('Taxonomy term hierarchy has been converted to default entity reference storage.');
101 * Update views to use {taxonomy_term__parent} in relationships.
103 function taxonomy_update_8503() {
104 $config_factory = \Drupal::configFactory();
106 foreach ($config_factory->listAll('views.view.') as $id) {
107 $view = $config_factory->getEditable($id);
109 foreach (array_keys($view->get('display')) as $display_id) {
112 foreach (['relationships', 'filters', 'arguments'] as $handler_type) {
113 $base_path = "display.$display_id.display_options.$handler_type";
114 $handlers = $view->get($base_path);
120 foreach ($handlers as $handler_key => $handler_config) {
121 $table_path = "$base_path.$handler_key.table";
122 $field_path = "$base_path.$handler_key.field";
123 $table = $view->get($table_path);
124 $field = $view->get($field_path);
126 if (($table && ($table === 'taxonomy_term_hierarchy')) && ($field && ($field === 'parent'))) {
127 $view->set($table_path, 'taxonomy_term__parent');
128 $view->set($field_path, 'parent_target_id');
143 * Add the publishing status fields to taxonomy terms.
145 function taxonomy_update_8601() {
146 $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
147 $entity_type = $definition_update_manager->getEntityType('taxonomy_term');
149 // Bail out early if a field named 'status' is already installed.
150 if ($definition_update_manager->getFieldStorageDefinition('status', 'taxonomy_term')) {
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.', [
152 ':link' => 'https://www.drupal.org/node/2985366',
157 // Add the 'published' entity key to the taxonomy_term entity type.
158 $entity_keys = $entity_type->getKeys();
159 $entity_keys['published'] = 'status';
160 $entity_type->set('entity_keys', $entity_keys);
162 $definition_update_manager->updateEntityType($entity_type);
164 // Add the status field.
165 $status = BaseFieldDefinition::create('boolean')
166 ->setLabel(t('Publishing status'))
167 ->setDescription(t('A boolean indicating the published state.'))
168 ->setRevisionable(TRUE)
169 ->setTranslatable(TRUE)
170 ->setDefaultValue(TRUE);
172 $has_content_translation_status_field = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
173 if ($has_content_translation_status_field) {
174 $status->setInitialValueFromField('content_translation_status', TRUE);
177 $status->setInitialValue(TRUE);
179 $definition_update_manager->installFieldStorageDefinition('status', 'taxonomy_term', 'taxonomy_term', $status);
181 // Uninstall the 'content_translation_status' field if needed.
182 if ($has_content_translation_status_field) {
183 $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
184 $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
187 return t('The publishing status field has been added to taxonomy terms.');