You are here

bundle_inherit.install in Bundle Inherit 7

Bundle Inherit module install file.

File

bundle_inherit.install
View source
<?php

/**
 * @file
 * Bundle Inherit module install file.
 */

/**
 * Implements hook_schema().
 */
function bundle_inherit_schema() {
  $schema['bundle_inherit'] = array(
    'description' => 'Holds info about hierarchy relations between entity types.',
    'fields' => array(
      'entity_type' => array(
        'description' => 'The entity type of the bundles.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'bundle' => array(
        'description' => 'Child bundle name.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'bundle_parent' => array(
        'description' => 'Parent bundle name.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'entity_type',
      'bundle',
    ),
    'indexes' => array(
      'parent' => array(
        'entity_type',
        'bundle_parent',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function bundle_inherit_install() {
  $entity_types = entity_get_info();

  // Set empty string as a parent bundle to the bundles which already exist
  foreach ($entity_types as $type_name => $type) {
    foreach ($type['bundles'] as $bundle_name => $bundle) {
      db_insert('bundle_inherit')
        ->fields(array(
        'entity_type' => $type_name,
        'bundle' => $bundle_name,
        'bundle_parent' => '',
      ))
        ->execute();
    }
  }
}

/**
 * Implements hook_uninstall().
 *
 * We should remove 'locked' state from all inherited fields instances.
 *
 * @todo
 *   Maybe some better way to implement this function exists (talking about
 *   performance). But anyway we are getting info about instances from cache
 *   so I think that this implementation is more stable and logicaly loocking.
 */
function bundle_inherit_uninstall() {
  $records = db_select('bundle_inherit', 'bi')
    ->fields('bi')
    ->execute();
  foreach ($records as $record) {
    $instances = field_info_instances($record->entity_type, $record->bundle_parent);
    foreach ($instances as $instance) {
      $inherited_instance = field_info_instance($record->entity_type, $instance['field_name'], $record->bundle);
      $inherited_instance['locked'] = FALSE;
      field_update_instance($inherited_instance);
    }
  }
}

/**
 * Rename bundle_hierarchy table to bundle_inherit.
 *
 * Creates the new bundle_inherit table, copies over bundle_hierarchy data to
 * the bundle_inherit table and, finally, drop the old bundle_hierarchy table.
 */
function bundle_inherit_update_7000() {

  // Find out if we're running MySQL 5.5.
  if ($result = db_query("SELECT version();")
    ->fetchField()) {
    if (version_compare($result, '5.5', '>=')) {
      if (db_query("RENAME TABLE bundle_hierarchy TO bundle_inherit")) {
        return t('Renamed bundle_hierarchy table to bundle_inherit.');
      }
    }
  }

  // If the script gets here we're either not running MySQL, or not running
  // MySQL < 5.5 or for whatever reason the query didn't work.
  // We must copy and rename the table manually.
  // Get table from the schema definition, force a rebuild to make sure the
  // updated schema definition will be found.
  $table = drupal_get_schema('bundle_inherit', TRUE);
  try {
    db_create_table('bundle_inherit', $table);
  } catch (DatabaseSchemaObjectExistsException $e) {

    // Do nothing, the table already exists.
  }

  // Grab all the data from the old table.
  $data = db_query("SELECT * FROM bundle_hierarchy")
    ->fetchAll();
  foreach ($data as $record) {
    if (!drupal_write_record('bundle_inherit', $record)) {

      // If we encounter a single error, stop immediately.
      return FALSE;
    }
  }
  return t('Renamed bundle_hierarchy table to bundle_inherit.');
}

Functions

Namesort descending Description
bundle_inherit_install Implements hook_install().
bundle_inherit_schema Implements hook_schema().
bundle_inherit_uninstall Implements hook_uninstall().
bundle_inherit_update_7000 Rename bundle_hierarchy table to bundle_inherit.