You are here

field_weight.install in Field display weights (per node) 7.2

Same filename and directory in other branches
  1. 7 field_weight.install

Field weight module install file.

File

field_weight.install
View source
<?php

/**
 * @file
 * 
 * Field weight module install file.
 */

/**
 * Implements hook_schema().
 */
function field_weight_schema() {
  $schema['field_weight'] = array(
    'description' => 'Field weight table.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The bundle type of this node.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'field_weights' => array(
        'description' => 'Serialised array of keyed array containing field_name => weight.',
        'type' => 'blob',
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The identifier for a node revision.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'nid',
      'vid',
    ),
  );
  return $schema;
}

/**
 * Add the vid field. Change the primary key.
 */
function field_weight_update_7101() {
  $field_weight = drupal_get_schema_unprocessed('field_weight', 'field_weight');
  $vid_spec = $field_weight['fields']['vid'];
  $vid_spec['not null'] = FALSE;

  // Temporary to avoid error
  if (!db_field_exists('field_weight', 'vid')) {
    db_add_field('field_weight', 'vid', $vid_spec);
  }

  // Make the primary key complex. We do this before filling vid with
  // values because we'll get a duplicate key error otherwise.
  db_drop_primary_key('field_weight');
  db_add_primary_key('field_weight', array(
    'nid',
    'vid',
  ));

  // Populate vid with real default values.
  $existing_nids = db_query("select nid, type, field_weights from {field_weight}");
  foreach ($existing_nids as $row) {
    $vid = db_query("select vid from {node} where nid = :nid", array(
      ':nid' => $row->nid,
    ))
      ->fetchField();
    db_update('field_weight')
      ->condition('nid', $row->nid)
      ->fields(array(
      'vid' => $vid,
    ))
      ->execute();

    // And make copies of the row for all other revisions
    $other_revisions = db_query("select vid from {node_revision} where nid = :nid and vid != :vid", array(
      ':nid' => $row->nid,
      ':vid' => $vid,
    ));
    foreach ($other_revisions as $vid_row) {
      $row->vid = $vid_row->vid;
      db_insert('field_weight')
        ->fields((array) $row)
        ->execute();
    }
  }

  // Change vid to be not-not null.
  $vid_spec['not null'] = TRUE;
  db_change_field('field_weight', 'vid', 'vid', $vid_spec);
}

/**
 * Implements hook_uninstall().
 */
function field_weight_uninstall() {
  variable_del('field_weight_node_types');
}

Functions

Namesort descending Description
field_weight_schema Implements hook_schema().
field_weight_uninstall Implements hook_uninstall().
field_weight_update_7101 Add the vid field. Change the primary key.