taxonomy.install 11.6 KB
Newer Older
1
2
<?php

3
4
5
6
7
/**
 * @file
 * Install, update and uninstall functions for the taxonomy module.
 */

8
use Drupal\Component\Uuid\Uuid;
9
use Drupal\field\Plugin\Core\Entity\Field;
10

11
/**
12
 * Implements hook_uninstall().
13
14
 */
function taxonomy_uninstall() {
15
  // Remove taxonomy_term bundles.
16
17
18
  $config_names = config_get_storage_names_with_prefix('taxonomy.vocabulary.');
  foreach ($config_names as $config_name) {
    $vid = substr($config_name, strlen('taxonomy.vocabulary.'));
19
    entity_invoke_bundle_hook('delete', 'taxonomy_term', $vid);
20
  }
21
22
}

23
/**
24
 * Implements hook_schema().
25
26
 */
function taxonomy_schema() {
27
  $schema['taxonomy_term_data'] = array(
28
    'description' => 'Stores term information.',
29
    'fields' => array(
30
31
32
33
      'tid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
34
        'description' => 'Primary Key: Unique term ID.',
35
      ),
36
37
38
39
40
41
      'uuid' => array(
        'description' => 'Unique Key: Universally unique identifier for this entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ),
42
      'vid' => array(
43
44
        'type' => 'varchar',
        'length' => 255,
45
        'not null' => TRUE,
46
47
        'default' => '',
        'description' => 'The ID of the vocabulary to which the term is assigned.',
48
      ),
49
50
51
52
53
54
55
      'langcode' => array(
        'description' => 'The {language}.langcode of this term.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
56
57
58
59
60
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
61
        'description' => 'The term name.',
62
63
64
65
66
      ),
      'description' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
67
        'description' => 'A description of the term.',
68
      ),
69
      'format' => array(
70
71
        'type' => 'varchar',
        'length' => 255,
72
        'not null' => FALSE,
73
        'description' => 'The filter format ID of the description.',
74
      ),
75
76
77
78
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
79
        'description' => 'The weight of this term in relation to other terms.',
80
      ),
81
82
    ),
    'primary key' => array('tid'),
83
84
85
    'unique keys' => array(
      'uuid' => array('uuid'),
    ),
86
    'indexes' => array(
87
88
      'taxonomy_tree' => array(array('vid', 64), 'weight', 'name'),
      'vid_name' => array(array('vid', 64), 'name'),
89
      'name' => array('name'),
90
    ),
91
92
  );

93
  $schema['taxonomy_term_hierarchy'] = array(
94
    'description' => 'Stores the hierarchical relationship between terms.',
95
    'fields' => array(
96
97
98
99
100
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
101
        'description' => 'Primary Key: The {taxonomy_term_data}.tid of the term.',
102
103
104
105
106
107
      ),
      'parent' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
108
        'description' => "Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.",
109
      ),
110
111
112
113
    ),
    'indexes' => array(
      'parent' => array('parent'),
    ),
114
    'foreign keys' => array(
115
116
117
118
      'taxonomy_term_data' => array(
        'table' => 'taxonomy_term_data',
        'columns' => array('tid' => 'tid'),
      ),
119
    ),
120
121
122
    'primary key' => array('tid', 'parent'),
  );

123
124
  $schema['taxonomy_index'] = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
125
    'fields' => array(
126
127
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
128
129
130
131
132
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
      'tid' => array(
         'description' => 'The term ID.',
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node is sticky.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
150
        'not null' => TRUE,
151
        'default'=> 0,
152
      ),
153
    ),
154
    'indexes' => array(
155
      'term_node' => array('tid', 'sticky', 'created'),
156
      'nid' => array('nid'),
157
    ),
158
    'foreign keys' => array(
159
160
161
162
163
164
165
166
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'term' => array(
        'table' => 'taxonomy_term_data',
        'columns' => array('tid' => 'tid'),
      ),
167
    ),
168
169
170
171
172
  );

  return $schema;
}

173
174
175
176
177
178
/**
 * Implements hook_field_schema().
 */
function taxonomy_field_schema($field) {
  return array(
    'columns' => array(
179
      'target_id' => array(
180
181
182
183
184
185
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
186
      'target_id' => array('target_id'),
187
    ),
188
    'foreign keys' => array(
189
      'target_id' => array(
190
        'table' => 'taxonomy_term_data',
191
        'columns' => array('target_id' => 'tid'),
192
193
      ),
    ),
194
195
  );
}
196

197
198
199
200
201
202
203
204
205
206
207
208
/**
 * Implements hook_update_dependencies().
 */
function taxonomy_update_dependencies() {
  // Convert the 'tid' column of the taxonomy reference field to 'target_id'
  // after fields and instances have been moved to the config system.
  $dependencies['taxonomy'][8007] = array(
    'field' => 8003,
  );
  return $dependencies;
}

209
210
211
212
213
/**
 * Remove the {taxonomy_vocabulary}.module field.
 */
function taxonomy_update_8000() {
  db_drop_field('taxonomy_vocabulary', 'module');
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
}

/**
 * Adds langcode field to {taxonomy_term_data} and {taxonomy_vocabulary}.
 *
 * @see http://drupal.org/node/1454538
 */
function taxonomy_update_8001() {
  $descriptions = array(
    'taxonomy_term_data' => 'The {language}.langcode of this term.',
    'taxonomy_vocabulary' => 'The {language}.langcode of this vocabulary.',
  );
  foreach ($descriptions as $table => $description) {
    $langcode_field = array(
      'description' => $description,
      'type' => 'varchar',
      'length' => 12,
      'not null' => TRUE,
      'default' => '',
    );

    // If a Drupal 7 contrib module already added a langcode field to support
    // internationalization, keep it, but standardize the specification.
    // Otherwise, add the field.
    if (db_field_exists($table, 'langcode')) {
239
      // According to the documentation of db_change_field(), indices using the
240
      // field should be dropped first; if the contrib module created any
241
      // indices, it is its responsibility to drop them in an update function
242
243
244
245
246
247
248
249
250
251
252
253
254
      // that runs before this one, which it can enforce via
      // hook_update_dependencies().
      db_change_field($table, 'langcode', 'langcode', $langcode_field);
    }
    else {
      // When updating from a site that did not already have taxonomy
      // internationalization, initialize all existing vocabularies and terms as
      // being in the site's default language.
      $langcode_field['initial'] = language_default()->langcode;
      db_add_field($table, 'langcode', $langcode_field);
    }
  }
}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

/**
 * Create a UUID column for taxonomy terms.
 */
function taxonomy_update_8002() {
  $spec = array(
    'description' => 'Unique Key: Universally unique identifier for this entity.',
    'type' => 'varchar',
    'length' => 128,
    'not null' => FALSE,
  );
  $keys = array(
    'unique keys' => array(
      'uuid' => array('uuid'),
    ),
  );
  // Account for sites having the contributed UUID module installed.
  if (db_field_exists('taxonomy_term_data', 'uuid')) {
    db_change_field('taxonomy_term_data', 'uuid', 'uuid', $spec, $keys);
  }
  else {
    db_add_field('taxonomy_term_data', 'uuid', $spec, $keys);
  }
}
279
280

/**
281
 * Generate a UUID for all terms.
282
283
284
285
286
287
288
289
290
291
292
293
294
 */
function taxonomy_update_8003(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['last'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(tid) FROM {taxonomy_term_data} WHERE uuid IS NULL')->fetchField();
  }

  $tids = db_query_range('SELECT tid FROM {taxonomy_term_data} WHERE tid > :tid AND uuid IS NULL ORDER BY tid ASC', 0, 10, array(':tid' => $sandbox['last']))->fetchCol();
  update_add_uuids($sandbox, 'taxonomy_term_data', 'tid', $tids);

  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}
295
296
297
298
299
300
301

/**
 * Moves taxonomy settings from variable to config.
 */
function taxonomy_update_8004() {
  update_variables_to_config('taxonomy.settings', array(
    'taxonomy_override_selector' => 'override_selector',
302
    'taxonomy_terms_per_page_admin' => 'terms_per_page_admin',
303
304
305
306
    'taxonomy_maintain_index_table' => 'maintain_index_table',
  ));
}

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
 * Convert vocabularies into configuration.
 */
function taxonomy_update_8005() {
  $uuid = new Uuid();

  $result = db_query('SELECT * FROM {taxonomy_vocabulary}');
  foreach ($result as $vocabulary) {
    $config = config('taxonomy.vocabulary.' . $vocabulary->machine_name)
      ->set('vid', $vocabulary->machine_name)
      ->set('name', $vocabulary->name)
      ->set('uuid', !empty($vocabulary->uuid) ? $vocabulary->uuid : $uuid->generate())
      ->set('description', $vocabulary->description)
      ->set('hierarchy', $vocabulary->hierarchy)
      ->set('weight', $vocabulary->weight)
      ->set('langcode', $vocabulary->langcode)
      ->save();
  }
}

/**
 * Change {taxonomy_term_data}.vid into a string holding the vocabulary machine name.
 */
function taxonomy_update_8006() {
331
332
  db_drop_index('taxonomy_term_data', 'taxonomy_tree');
  db_drop_index('taxonomy_term_data', 'vid_name');
333
334
335
336
337
338
339
  db_change_field('taxonomy_term_data', 'vid', 'vid', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The ID of the vocabulary to which the term is assigned.',
  ));
340
341
342
  db_add_index('taxonomy_term_data', 'taxonomy_tree', array(array('vid', 64), 'weight', 'name'));
  db_add_index('taxonomy_term_data', 'vid_name', array(array('vid', 64), 'name'));

343
344
345
346
347
348
349
350
  $map = db_query('SELECT vid, machine_name FROM {taxonomy_vocabulary}')->fetchAllKeyed();
  foreach ($map as $vid => $machine_name) {
    db_update('taxonomy_term_data')
      ->condition('vid', $vid)
      ->fields(array('vid' => $machine_name))
      ->execute();
  }
}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

/**
 * Update taxonomy_term_reference field tables to use target_id instead of tid.
 */
function taxonomy_update_8007() {
  foreach (config_get_storage_names_with_prefix('field.field.') as $config_name) {
    $field_config = config($config_name);
    // Only update taxonomy reference fields that use the default SQL storage.
    if ($field_config->get('type') == 'taxonomy_term_reference' && $field_config->get('storage.type') == 'field_sql_storage') {
      $field = new Field($field_config->get());

      $tables = array(
        _field_sql_storage_tablename($field),
        _field_sql_storage_revision_tablename($field),
      );

      foreach ($tables as $table_name) {
        db_change_field($table_name, $field->id() . '_tid', $field->id() . '_target_id', array(
          'description' => 'The ID of the target entity.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
        ));

        // Change the index.
        db_drop_index($table_name, $field->id() . '_tid');
        db_add_index($table_name, $field->id() . '_target_id', array($field->id() . '_target_id'));
      }

      // Update the indexes in field config as well.
      $indexes = $field_config->get('indexes');
      unset($indexes['tid']);
      $indexes['target_id'] = array('target_id');
      $field_config->set('indexes', $indexes);
      $field_config->save();
    }
  }
}