taxonomy_update_7004

Versions
7
taxonomy_update_7004()

Move taxonomy vocabulary associations for nodes to fields and field instances.

Code

modules/taxonomy/taxonomy.install, line 253

<?php
function taxonomy_update_7004() {
  $taxonomy_index = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      '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',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default'=> 0,
      ),
    ),
    'indexes' => array(
      'term_node' => array('tid', 'sticky', 'created'),
    ),
    'foreign keys' => array(
      'node' => 'nid',
      'taxonomy_term_data' => 'tid',
    ),
  );
  db_create_table('taxonomy_index', $taxonomy_index);

  // Use an inline version of Drupal 6 taxonomy_get_vocabularies() here since
  // we can no longer rely on $vocabulary->nodes from the API function.
  $result = db_query('SELECT v.*, n.type FROM {taxonomy_vocabulary} v LEFT JOIN {taxonomy_vocabulary_node_type} n ON v.vid = n.vid ORDER BY v.weight, v.name');
  $vocabularies = array();
  foreach ($result as $record) {
    // If no node types are associated with a vocabulary, the LEFT JOIN will
    // return a NULL value for type.
    if (isset($record->type)) {
      $node_types[$record->vid][$record->type] = $record->type;
      unset($record->type);
      $record->nodes = $node_types[$record->vid];
    }
    elseif (!isset($record->nodes)) {
      $record->nodes = array();
    }
    $vocabularies[$record->vid] = $record;
  }

  foreach ($vocabularies as $vocabulary) {
    $field_name = 'taxonomy_' . $vocabulary->machine_name;
    $field = array(
      'field_name' => $field_name,
      'type' => 'taxonomy_term',
      'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
      'settings' => array(
        'required' => $vocabulary->required ? TRUE : FALSE,
        'allowed_values' => array(
          array(
            'vid' => $vocabulary->vid,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    foreach ($vocabulary->nodes as $bundle) {
      $instance = array(
        'label' => $vocabulary->name,
        'field_name' => $field_name,
        'bundle' => $bundle,
        'object_type' => 'node',
        'description' => $vocabulary->help,
        'widget' => array(
          'type' => $vocabulary->tags ? 'taxonomy_autocomplete' : 'select',
        ),
      );
      field_create_instance($instance);
    }
  }
  db_drop_table('taxonomy_vocabulary_node_type');
  $fields = array('help', 'multiple', 'required', 'tags');
  foreach ($fields as $field) {
    db_drop_field('taxonomy_vocabulary', $field);
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.