2007-10-05 16:07:22 +00:00
<?php
2009-05-13 19:42:18 +00:00
/**
* @file
* Install, update and uninstall functions for the taxonomy module.
*/
2013-10-13 11:34:58 +00:00
use Drupal\Core\Entity\FieldableDatabaseStorageController;
2013-08-18 21:16:19 +00:00
use Drupal\field\Entity\Field;
2012-12-19 17:06:30 +00:00
2007-10-05 16:07:22 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_schema().
2007-10-05 16:07:22 +00:00
*/
function taxonomy_schema() {
2009-01-14 21:16:21 +00:00
$schema['taxonomy_term_data'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores term information.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'tid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique term ID.',
2007-10-10 11:39:35 +00:00
),
2012-08-07 18:33:39 +00:00
'uuid' => array(
'description' => 'Unique Key: Universally unique identifier for this entity.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
2007-10-10 11:39:35 +00:00
'vid' => array(
2012-12-19 17:06:30 +00:00
'type' => 'varchar',
'length' => 255,
2007-10-10 11:39:35 +00:00
'not null' => TRUE,
2012-12-19 17:06:30 +00:00
'default' => '',
'description' => 'The ID of the vocabulary to which the term is assigned.',
2007-10-10 11:39:35 +00:00
),
2012-03-02 18:46:16 +00:00
'langcode' => array(
'description' => 'The {language}.langcode of this term.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
2007-10-10 11:39:35 +00:00
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'The term name.',
2007-10-10 11:39:35 +00:00
),
'description' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
2008-11-15 13:01:11 +00:00
'description' => 'A description of the term.',
2007-10-10 11:39:35 +00:00
),
2009-11-07 14:18:07 +00:00
'format' => array(
2010-10-20 01:15:58 +00:00
'type' => 'varchar',
'length' => 255,
2010-09-28 03:30:37 +00:00
'not null' => FALSE,
2013-01-17 22:22:50 +00:00
'description' => 'The filter format ID of the description.',
2009-11-07 14:18:07 +00:00
),
2007-10-10 11:39:35 +00:00
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'The weight of this term in relation to other terms.',
2007-10-10 11:39:35 +00:00
),
2013-09-04 18:31:38 +00:00
'changed' => array(
'description' => 'The Unix timestamp when the term was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
2007-10-05 16:07:22 +00:00
),
'primary key' => array('tid'),
2012-08-07 18:33:39 +00:00
'unique keys' => array(
'uuid' => array('uuid'),
),
2008-01-08 07:46:41 +00:00
'indexes' => array(
2013-01-09 18:22:09 +00:00
'taxonomy_tree' => array(array('vid', 64), 'weight', 'name'),
'vid_name' => array(array('vid', 64), 'name'),
2009-12-30 08:12:20 +00:00
'name' => array('name'),
2007-12-18 12:59:22 +00:00
),
2007-10-05 16:07:22 +00:00
);
2009-01-14 21:16:21 +00:00
$schema['taxonomy_term_hierarchy'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores the hierarchical relationship between terms.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'tid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2009-01-14 21:16:21 +00:00
'description' => 'Primary Key: The {taxonomy_term_data}.tid of the term.',
2007-10-10 11:39:35 +00:00
),
'parent' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2009-01-14 21:16:21 +00:00
'description' => "Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.",
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'indexes' => array(
'parent' => array('parent'),
),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
2010-08-22 13:55:53 +00:00
'taxonomy_term_data' => array(
'table' => 'taxonomy_term_data',
'columns' => array('tid' => 'tid'),
),
2009-06-01 22:07:10 +00:00
),
2007-10-05 16:07:22 +00:00
'primary key' => array('tid', 'parent'),
);
2009-10-08 07:58:47 +00:00
$schema['taxonomy_index'] = array(
'description' => 'Maintains denormalized information about node/term relationships.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2009-10-08 07:58:47 +00:00
'nid' => array(
'description' => 'The {node}.nid this record tracks.',
2007-10-10 11:39:35 +00:00
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
2009-10-08 07:58:47 +00:00
'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',
2007-12-08 14:06:23 +00:00
'not null' => TRUE,
2009-10-08 07:58:47 +00:00
'default'=> 0,
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
2007-12-18 12:59:22 +00:00
'indexes' => array(
2009-10-08 07:58:47 +00:00
'term_node' => array('tid', 'sticky', 'created'),
2010-03-26 16:53:33 +00:00
'nid' => array('nid'),
2007-12-18 12:59:22 +00:00
),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
2010-08-22 13:55:53 +00:00
'tracked_node' => array(
'table' => 'node',
'columns' => array('nid' => 'nid'),
),
'term' => array(
'table' => 'taxonomy_term_data',
'columns' => array('tid' => 'tid'),
),
2009-06-01 22:07:10 +00:00
),
2007-10-05 16:07:22 +00:00
);
return $schema;
}
2010-09-04 15:40:52 +00:00
/**
* Implements hook_field_schema().
*/
function taxonomy_field_schema($field) {
return array(
'columns' => array(
2013-06-19 19:07:06 +00:00
'target_id' => array(
2010-09-04 15:40:52 +00:00
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'indexes' => array(
2013-06-19 19:07:06 +00:00
'target_id' => array('target_id'),
2010-09-04 15:40:52 +00:00
),
2010-11-21 09:21:38 +00:00
'foreign keys' => array(
2013-06-19 19:07:06 +00:00
'target_id' => array(
2010-11-21 09:21:38 +00:00
'table' => 'taxonomy_term_data',
2013-06-19 19:07:06 +00:00
'columns' => array('target_id' => 'tid'),
2010-11-21 09:21:38 +00:00
),
),
2010-09-04 15:40:52 +00:00
);
}
2011-09-17 11:27:18 +00:00
2013-06-19 19:07:06 +00:00
/**
* Implements hook_update_dependencies().
*/
function taxonomy_update_dependencies() {
// Convert the 'tid' column of the taxonomy reference field to 'target_id'
2013-09-01 06:20:08 +00:00
// after the field tables have been reorganized.
2013-06-19 19:07:06 +00:00
$dependencies['taxonomy'][8007] = array(
2013-09-01 06:20:08 +00:00
'field' => 8006,
2013-06-19 19:07:06 +00:00
);
return $dependencies;
}
2011-09-17 11:27:18 +00:00
/**
* Remove the {taxonomy_vocabulary}.module field.
*/
function taxonomy_update_8000() {
db_drop_field('taxonomy_vocabulary', 'module');
2012-03-02 18:46:16 +00:00
}
/**
* 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')) {
2013-01-22 23:23:41 +00:00
// According to the documentation of db_change_field(), indices using the
2012-03-02 18:46:16 +00:00
// field should be dropped first; if the contrib module created any
2013-01-22 23:23:41 +00:00
// indices, it is its responsibility to drop them in an update function
2012-03-02 18:46:16 +00:00
// 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.
2013-06-29 10:56:53 +00:00
$langcode_field['initial'] = language_default()->id;
2012-03-02 18:46:16 +00:00
db_add_field($table, 'langcode', $langcode_field);
}
}
}
2012-08-07 18:33:39 +00:00
/**
* 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);
}
}
2012-10-08 16:39:59 +00:00
/**
2012-10-17 16:40:55 +00:00
* Generate a UUID for all terms.
2012-10-08 16:39:59 +00:00
*/
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']);
}
2012-12-13 12:18:30 +00:00
/**
* Moves taxonomy settings from variable to config.
*/
function taxonomy_update_8004() {
update_variables_to_config('taxonomy.settings', array(
'taxonomy_override_selector' => 'override_selector',
2012-12-27 22:20:48 +00:00
'taxonomy_terms_per_page_admin' => 'terms_per_page_admin',
2012-12-13 12:18:30 +00:00
'taxonomy_maintain_index_table' => 'maintain_index_table',
));
}
2012-12-19 17:06:30 +00:00
/**
* Convert vocabularies into configuration.
*/
function taxonomy_update_8005() {
2013-09-24 22:16:05 +00:00
$uuid = \Drupal::service('uuid');
2012-12-19 17:06:30 +00:00
$result = db_query('SELECT * FROM {taxonomy_vocabulary}');
foreach ($result as $vocabulary) {
2013-09-16 03:58:06 +00:00
$config = \Drupal::config('taxonomy.vocabulary.' . $vocabulary->machine_name)
2012-12-19 17:06:30 +00:00
->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() {
2013-01-09 18:22:09 +00:00
db_drop_index('taxonomy_term_data', 'taxonomy_tree');
db_drop_index('taxonomy_term_data', 'vid_name');
2012-12-19 17:06:30 +00:00
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.',
));
2013-01-09 18:22:09 +00:00
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'));
2012-12-19 17:06:30 +00:00
$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();
}
}
2013-06-19 19:07:06 +00:00
/**
* 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) {
2013-09-16 03:58:06 +00:00
$field_config = \Drupal::config($config_name);
2013-06-19 19:07:06 +00:00
// Only update taxonomy reference fields that use the default SQL storage.
2013-09-01 06:20:08 +00:00
if ($field_config->get('type') == 'taxonomy_term_reference') {
2013-06-19 19:07:06 +00:00
$field = new Field($field_config->get());
2013-10-13 11:34:58 +00:00
if (db_table_exists(FieldableDatabaseStorageController::_fieldTableName($field))) {
2013-09-02 21:29:44 +00:00
$tables = array(
2013-10-13 11:34:58 +00:00
FieldableDatabaseStorageController::_fieldTableName($field),
FieldableDatabaseStorageController::_fieldRevisionTableName($field),
2013-09-02 21:29:44 +00:00
);
2013-06-19 19:07:06 +00:00
2013-09-02 21:29:44 +00:00
foreach ($tables as $table_name) {
db_change_field($table_name, $field->name . '_tid', $field->name . '_target_id', array(
'description' => 'The ID of the target entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
));
2013-06-19 19:07:06 +00:00
2013-09-02 21:29:44 +00:00
// Change the index.
db_drop_index($table_name, $field->name . '_tid');
db_add_index($table_name, $field->name . '_target_id', array($field->name . '_target_id'));
}
2013-06-19 19:07:06 +00:00
2013-09-02 21:29:44 +00:00
// 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();
}
2013-06-19 19:07:06 +00:00
}
}
}
2013-09-04 18:31:38 +00:00
/**
* Add a changed column for taxonomy terms.
*/
function taxonomy_update_8008() {
$spec = array(
'description' => 'The Unix timestamp when the term was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'initial' => REQUEST_TIME,
);
db_add_field('taxonomy_term_data', 'changed', $spec);
}
2013-11-07 10:49:51 +00:00
/**
* Convert numeric IDs to UUIDs to ensure config deployability.
*/
function taxonomy_update_8009() {
foreach (config_get_storage_names_with_prefix('field.instance.') as $instance_config_name) {
$instance_config = \Drupal::config($instance_config_name);
if ($instance_config->get('field_type') == 'taxonomy_term_reference') {
$default_value = $instance_config->get('default_value');
$ids = array();
// Load all referenced taxonomy_terms in default_value.
foreach ($default_value as $delta => $value) {
$ids[] = $value['tid'];
}
if ($ids) {
$entities = db_select('taxonomy_term_data', 't')
->fields('t', array('tid', 'uuid'))
->condition('t.tid', $ids)
->execute()->fetchAllAssoc('tid');
// Convert IDs to UUIDs and save the new default_value.
foreach ($default_value as $delta => &$value) {
$value = array(
'target_uuid' => $entities[$value['tid']]->uuid,
'revision_id' => '',
);
}
$instance_config->set('default_value', $default_value)->save();
}
}
}
}