You are here

field_group.install in Field Group 7.2

Same filename and directory in other branches
  1. 8.3 field_group.install
  2. 7 field_group.install

Fieldgroup module install file.

File

field_group.install
View source
<?php

/**
 * @file
 * Fieldgroup module install file.
 */

/**
 * Implements hook_schema().
 */
function field_group_schema() {
  $schema['field_group'] = array(
    'description' => t('Table that contains field group entries and settings.'),
    // CTools export definitions.
    'export' => array(
      'key' => 'identifier',
      'identifier' => 'field_group',
      'default hook' => 'field_group_info',
      'save callback' => 'field_group_group_save',
      'delete callback' => 'field_group_group_export_delete',
      'can disable' => TRUE,
      'api' => array(
        'owner' => 'field_group',
        'api' => 'field_group',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The primary identifier for a group',
        'no export' => TRUE,
      ),
      'identifier' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The unique string identifier for a group.',
      ),
      'group_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of this group.',
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'mode' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      // @todo 'parent_name' is redundant with the data in the 'children'
      // entry, brings a risk of inconsistent data. This should be removed from
      // the schema and pre-computed it if needed in field_group_get_groups().
      'parent_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The parent name for a group',
      ),
      'data' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => TRUE,
        'serialize' => TRUE,
        'description' => 'Serialized data containing the group properties that do not warrant a dedicated column.',
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'indexes' => array(
      'group_name' => array(
        'group_name',
      ),
    ),
    'unique keys' => array(
      'identifier' => array(
        'identifier',
      ),
    ),
  );
  return $schema;
}

/**
 * Utility function: fetch all the field_group definitions from the database.
 */
function _field_group_install_read_groups() {
  $groups = array();
  if (db_table_exists('content_group')) {
    $query = db_select('content_group', 'cg', array(
      'fetch' => PDO::FETCH_ASSOC,
    ))
      ->fields('cg')
      ->condition('group_type', 'standard');
    foreach ($query
      ->execute() as $record) {
      $record['settings'] = unserialize($record['settings']);
      $groups[$record['group_name']] = $record;
    }
    foreach ($groups as $key => $group) {
      $query2 = db_select('content_group_fields', 'cgf', array(
        'fetch' => PDO::FETCH_ASSOC,
      ))
        ->fields('cgf')
        ->condition('group_name', $group['group_name']);
      foreach ($query2
        ->execute() as $field) {
        $groups[$field['group_name']]['children'][] = $field;
      }
    }
  }
  return $groups;
}

/**
 * Implements of hook_install().
 *
 * Because this is a new module in D7, hook_update_N() doesn't help D6
 * users who upgrade to run the migration path. So, we try that here as
 * the module is being installed.
 */
function field_group_install() {
  $groups = _field_group_install_read_groups();
  module_load_include('module', 'field_group');
  if (!empty($groups)) {
    module_load_include('module', 'ctools');
    ctools_include('export');
    foreach ($groups as $group) {
      $group = (object) $group;
      $new = new stdClass();
      $new->group_name = $group->group_name;
      $new->entity_type = 'node';
      $new->bundle = $group->type_name;
      $new->label = $group->label;
      $new->parent_name = '';
      $new->children = array();
      foreach ($group->children as $child) {
        $new->children[] = $child['field_name'];
      }

      // The form.
      $new->id = NULL;
      $new->weight = $group->weight;
      $new->mode = 'form';
      $new->format_type = 'fieldset';
      $new->format_settings = array(
        'formatter' => preg_match("/fieldset/", $group->settings['form']['style']) ? 'collapsible' : 'collapsed',
        'instance_settings' => array(),
      );
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
      ctools_export_crud_save('field_group', $new);

      // The full node.
      $new->id = NULL;
      $new->weight = $group->weight;
      $new->mode = 'default';
      $new->format_type = $group->settings['display']['full']['format'];
      $new->format_settings = array(
        'formatter' => 'collapsible',
        'instance_settings' => array(),
      );
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
      ctools_export_crud_save('field_group', $new);

      // The teaser node.
      $new->id = NULL;
      $new->weight = $group->weight;
      $new->mode = 'teaser';
      $new->format_type = $group->settings['display']['teaser']['format'];
      $new->format_settings = array(
        'formatter' => 'collapsible',
        'instance_settings' => array(),
      );
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
      ctools_export_crud_save('field_group', $new);
    }
  }

  // Set weight to 1.
  db_update('system')
    ->fields(array(
    'weight' => 1,
  ))
    ->condition('name', 'field_group')
    ->execute();
}

/**
 * Update hook on the field_group table to add an unique identifier.
 */
function field_group_update_7001() {
  if (!db_field_exists('field_group', 'identifier')) {

    // Add the new string identifier field for ctools.
    db_add_field('field_group', 'identifier', array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
      'description' => 'The unique string identifier for a group.',
    ));
    module_load_include('module', 'field_group');
    _field_group_recreate_identifiers();
  }
  db_update('system')
    ->fields(array(
    'weight' => 1,
  ))
    ->condition('name', 'field_group')
    ->execute();
}

/**
 * Update hook to clear cache for new changes to take effect.
 */
function field_group_update_7002() {
  module_load_include('module', 'field_group');

  // This hook is called to satify people with older version of field_group.
  // This will recreate all identifiers for the field_groups known in database.
  // At the moment, we only trigger field_groups that are stored in the database, where
  // we should maybe get all field_groups as ctools has registered them.
  // See http://drupal.org/node/1169146.
  // See http://drupal.org/node/1018550.
  _field_group_recreate_identifiers();
}

/**
 * Update hook to recreate identifiers.
 * @see function field_group_update_7002.
 */
function field_group_update_7003() {
  module_load_include('module', 'field_group');
  _field_group_recreate_identifiers();
}

Functions

Namesort descending Description
field_group_install Implements of hook_install().
field_group_schema Implements hook_schema().
field_group_update_7001 Update hook on the field_group table to add an unique identifier.
field_group_update_7002 Update hook to clear cache for new changes to take effect.
field_group_update_7003 Update hook to recreate identifiers.
_field_group_install_read_groups Utility function: fetch all the field_group definitions from the database.