Mercurial > hg > isophonics-drupal-site
comparison core/modules/taxonomy/taxonomy.install @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Install, update and uninstall functions for the taxonomy module. | |
6 */ | |
7 | |
8 use Drupal\Core\Field\BaseFieldDefinition; | |
9 use Drupal\Core\Site\Settings; | |
10 | |
11 /** | |
12 * Convert the custom taxonomy term hierarchy storage to a default storage. | |
13 */ | |
14 function taxonomy_update_8501() { | |
15 $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); | |
16 | |
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); | |
21 } | |
22 | |
23 /** | |
24 * Copy hierarchy from {taxonomy_term_hierarchy} to {taxonomy_term__parent}. | |
25 */ | |
26 function taxonomy_update_8502(&$sandbox) { | |
27 $database = \Drupal::database(); | |
28 | |
29 if (!isset($sandbox['current'])) { | |
30 // Set batch ops sandbox. | |
31 $sandbox['current'] = 0; | |
32 $sandbox['tid'] = -1; | |
33 $sandbox['delta'] = 0; | |
34 $sandbox['limit'] = Settings::get('entity_update_batch_size', 50); | |
35 | |
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 | |
41 ->countQuery() | |
42 ->execute() | |
43 ->fetchField(); | |
44 } | |
45 | |
46 // Save the hierarchy. | |
47 $select = $database->select('taxonomy_term_hierarchy', 'h'); | |
48 $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid'); | |
49 $hierarchy = $select | |
50 ->fields('h', ['tid', 'parent']) | |
51 ->fields('d', ['vid', 'langcode']) | |
52 ->range($sandbox['current'], $sandbox['limit']) | |
53 ->orderBy('tid', 'ASC') | |
54 ->orderBy('parent', 'ASC') | |
55 ->execute() | |
56 ->fetchAll(); | |
57 | |
58 // Restore data. | |
59 $insert = $database->insert('taxonomy_term__parent') | |
60 ->fields(['bundle', 'entity_id', 'revision_id', 'langcode', 'delta', 'parent_target_id']); | |
61 | |
62 foreach ($hierarchy as $row) { | |
63 if ($row->tid !== $sandbox['tid']) { | |
64 $sandbox['delta'] = 0; | |
65 $sandbox['tid'] = $row->tid; | |
66 } | |
67 | |
68 $insert->values([ | |
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, | |
75 ]); | |
76 | |
77 $sandbox['delta']++; | |
78 $sandbox['current']++; | |
79 } | |
80 | |
81 $insert->execute(); | |
82 | |
83 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']); | |
84 | |
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')); | |
90 | |
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'); | |
95 | |
96 return t('Taxonomy term hierarchy has been converted to default entity reference storage.'); | |
97 } | |
98 } | |
99 | |
100 /** | |
101 * Update views to use {taxonomy_term__parent} in relationships. | |
102 */ | |
103 function taxonomy_update_8503() { | |
104 $config_factory = \Drupal::configFactory(); | |
105 | |
106 foreach ($config_factory->listAll('views.view.') as $id) { | |
107 $view = $config_factory->getEditable($id); | |
108 | |
109 foreach (array_keys($view->get('display')) as $display_id) { | |
110 $changed = FALSE; | |
111 | |
112 foreach (['relationships', 'filters', 'arguments'] as $handler_type) { | |
113 $base_path = "display.$display_id.display_options.$handler_type"; | |
114 $handlers = $view->get($base_path); | |
115 | |
116 if (!$handlers) { | |
117 continue; | |
118 } | |
119 | |
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); | |
125 | |
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'); | |
129 | |
130 $changed = TRUE; | |
131 } | |
132 } | |
133 } | |
134 | |
135 if ($changed) { | |
136 $view->save(TRUE); | |
137 } | |
138 } | |
139 } | |
140 } | |
141 | |
142 /** | |
143 * Add the publishing status fields to taxonomy terms. | |
144 */ | |
145 function taxonomy_update_8601() { | |
146 $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); | |
147 $entity_type = $definition_update_manager->getEntityType('taxonomy_term'); | |
148 | |
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', | |
153 ])); | |
154 return $message; | |
155 } | |
156 | |
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); | |
161 | |
162 $definition_update_manager->updateEntityType($entity_type); | |
163 | |
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); | |
171 | |
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); | |
175 } | |
176 else { | |
177 $status->setInitialValue(TRUE); | |
178 } | |
179 $definition_update_manager->installFieldStorageDefinition('status', 'taxonomy_term', 'taxonomy_term', $status); | |
180 | |
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); | |
185 } | |
186 | |
187 return t('The publishing status field has been added to taxonomy terms.'); | |
188 } |