You are here

image_field_caption.install in Image Field Caption 7.2

Same filename and directory in other branches
  1. 8 image_field_caption.install
  2. 7 image_field_caption.install

Image field caption defines a new database schema for adding a caption to images.

File

image_field_caption.install
View source
<?php

/**
 * @file
 * Image field caption defines a new database schema
 * for adding a caption to images.
 */

/**
 * Implements hook_schema().
 */
function image_field_caption_schema() {
  $schema['field_image_field_caption'] = array(
    'description' => 'The base table for the image_field_caption module.',
    'fields' => array(
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The entity type this data is attached to',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
      ),
      'entity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity id this data is attached to',
      ),
      'revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
      ),
      // @todo Consider storing language as integer.
      'language' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The language for this data item.',
      ),
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The sequence number for this data item, used for multi-value fields',
      ),
      'caption' => array(
        'description' => 'The caption text.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'caption_format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'field_name',
      'entity_type',
      'entity_id',
      'delta',
      'language',
    ),
    'indexes' => array(
      'entity_type' => array(
        'entity_type',
      ),
      'bundle' => array(
        'bundle',
      ),
      'entity_id' => array(
        'entity_id',
      ),
      'revision_id' => array(
        'revision_id',
      ),
      'language' => array(
        'language',
      ),
    ),
  );
  $schema['field_image_field_caption_revision'] = array(
    'description' => 'The base table for the image_field_caption module.',
    'fields' => array(
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The entity type this data is attached to',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
      ),
      'entity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity id this data is attached to',
      ),
      'revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity revision id this data is attached to',
      ),
      // @todo Consider storing language as integer.
      'language' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The language for this data item.',
      ),
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The sequence number for this data item, used for multi-value fields',
      ),
      'caption' => array(
        'description' => 'The caption text.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'caption_format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'field_name',
      'entity_type',
      'entity_id',
      'revision_id',
      'delta',
      'language',
    ),
    'indexes' => array(
      'entity_type' => array(
        'entity_type',
      ),
      'bundle' => array(
        'bundle',
      ),
      'entity_id' => array(
        'entity_id',
      ),
      'revision_id' => array(
        'revision_id',
      ),
      'language' => array(
        'language',
      ),
    ),
  );
  return $schema;
}

/**
 * Creates new schema
 */
function image_field_caption_update_7200() {
  $schema['field_image_field_caption'] = array(
    'description' => 'The base table for the image_field_caption module.',
    'fields' => array(
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The entity type this data is attached to',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
      ),
      'entity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity id this data is attached to',
      ),
      'revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
      ),
      // @todo Consider storing language as integer.
      'language' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The language for this data item.',
      ),
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The sequence number for this data item, used for multi-value fields',
      ),
      'caption' => array(
        'description' => 'The caption text.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'caption_format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'field_name',
      'entity_type',
      'entity_id',
      'delta',
      'language',
    ),
    'indexes' => array(
      'entity_type' => array(
        'entity_type',
      ),
      'bundle' => array(
        'bundle',
      ),
      'entity_id' => array(
        'entity_id',
      ),
      'revision_id' => array(
        'revision_id',
      ),
      'language' => array(
        'language',
      ),
    ),
  );
  $schema['field_image_field_caption_revision'] = array(
    'description' => 'The base table for the image_field_caption module.',
    'fields' => array(
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The entity type this data is attached to',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
      ),
      'entity_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity id this data is attached to',
      ),
      'revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The entity revision id this data is attached to',
      ),
      // @todo Consider storing language as integer.
      'language' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The language for this data item.',
      ),
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The sequence number for this data item, used for multi-value fields',
      ),
      'caption' => array(
        'description' => 'The caption text.',
        'type' => 'text',
        'not null' => TRUE,
      ),
      'caption_format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'field_name',
      'entity_type',
      'entity_id',
      'revision_id',
      'delta',
      'language',
    ),
    'indexes' => array(
      'entity_type' => array(
        'entity_type',
      ),
      'bundle' => array(
        'bundle',
      ),
      'entity_id' => array(
        'entity_id',
      ),
      'revision_id' => array(
        'revision_id',
      ),
      'language' => array(
        'language',
      ),
    ),
  );
  db_create_table('field_image_field_caption', $schema['field_image_field_caption']);
  db_create_table('field_image_field_caption_revision', $schema['field_image_field_caption_revision']);
}

/**
 * Migrate data to new format
 */
function image_field_caption_update_7201() {

  // Only node was handled previously by the module
  $bundle_type = 'node';
  $query = db_select('image_field_caption', 't')
    ->fields('t');
  $results = $query
    ->execute();
  foreach ($results as $row) {
    $instances = field_info_instances('node');
    foreach ($instances as $bundle => $fields) {
      foreach ($fields as $field_name => $field) {

        // Skip any deleted and non image widget fields.
        if ($field['deleted'] == 1) {
          continue;
        }
        if ($field['widget']['type'] != 'image_image') {
          continue;
        }
        $field = field_info_field($field_name);
        $query = new EntityFieldQuery();
        $entity_data = $query
          ->entityCondition('entity_type', $bundle_type)
          ->entityCondition('bundle', $bundle)
          ->fieldCondition($field, 'fid', $row->fid)
          ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
          ->execute();
        if (isset($entity_data[$bundle_type])) {

          // Enable setting
          $instance = field_info_instance($bundle_type, $field_name, $bundle);
          $instance['settings']['image_field_caption'] = 1;
          field_update_instance($instance);
          foreach ($entity_data[$bundle_type] as $entity_id => $entity) {
            $entities_load = array(
              $entity_id => $entity,
            );
            field_attach_load($bundle_type, $entities_load);
            foreach ($entity->{$field_name} as $langcode => $items) {
              foreach ($items as $delta => $item) {
                if ($item['fid'] == $row->fid) {
                  $columns = array(
                    'field_name',
                    'entity_type',
                    'entity_id',
                    'revision_id',
                    'bundle',
                    'delta',
                    'language',
                    'caption',
                    'caption_format',
                  );
                  list($id, $vid, $bundle) = entity_extract_ids($bundle_type, $entity);
                  $record = array(
                    'field_name' => $field_name,
                    'entity_type' => $bundle_type,
                    'entity_id' => $id,
                    'revision_id' => $vid,
                    'bundle' => $bundle,
                    'delta' => $delta,
                    'language' => $langcode,
                    'caption' => $row->caption,
                    'caption_format' => filter_default_format(),
                  );
                  $query = db_insert('field_image_field_caption')
                    ->fields($columns);
                  $query
                    ->values($record);
                  $query
                    ->execute();
                  $revision_query = db_insert('field_image_field_caption_revision')
                    ->fields($columns);
                  $revision_query
                    ->values($record);
                  $revision_query
                    ->execute();
                }
              }
            }
          }
        }
      }
    }
  }
  drupal_flush_all_caches();
}

/**
 * Remove old data
 */
function image_field_caption_update_7202() {
  db_drop_table('image_field_caption');
}

/**
 * Make the revision caption field non mandatory
 */
function image_field_caption_update_7203() {
  $caption_field_revision = array(
    'description' => 'The caption text.',
    'type' => 'text',
    'not null' => FALSE,
  );
  db_change_field('field_image_field_caption_revision', 'caption', 'caption', $caption_field_revision);
}

/**
 * Move the instance setting from ['image_field_caption'] to ['image_field_caption']['enabled'].
 * Reason: ['image_field_caption'] becomes the wrapper for additional settings.
 */
function image_field_caption_update_7204() {

  // Note: requires Drupal 7.22 or newer as field_info_field_map()
  // function was introduce in 7.22.
  // Grab all of the fields.
  $fields = field_info_field_map();
  foreach ($fields as $field_name => $field) {

    // Only modify 'image' type fields.
    if ($field['type'] == 'image') {
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {

          // Load the instance.
          $instance = field_info_instance($entity_type, $field_name, $bundle);

          // If the setting is not an array, then it is an old settings.
          if (isset($instance['settings']['image_field_caption']) && !is_array($instance['settings']['image_field_caption'])) {
            $enabled_value = $instance['settings']['image_field_caption'];
            unset($instance['settings']['image_field_caption']);
            $instance['settings']['image_field_caption'] = array(
              'enabled' => $enabled_value,
            );
            field_update_instance($instance);
          }
        }
      }
    }
  }
}

/**
 * Set image_field_caption_wrapper defaults on field instances.
 */
function image_field_caption_update_7205() {

  // Grab all of the fields.
  $fields = field_info_field_map();
  foreach ($fields as $field_name => $field) {

    // Only modify 'image' type fields.
    if ($field['type'] == 'image') {
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {

          // Load the instance and skip any that no longer exist.
          $instance = field_info_instance($entity_type, $field_name, $bundle);
          if (!$instance) {
            continue;
          }
          if (!isset($instance['settings']['image_field_caption']['image_field_caption_wrapper'])) {
            $instance['settings']['image_field_caption']['image_field_caption_wrapper'] = array(
              'image_field_caption_default' => array(
                'value' => '',
                'format' => 'plain_text',
              ),
            );
            field_update_instance($instance);
            print "Updated {$field_name}\n";
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
image_field_caption_schema Implements hook_schema().
image_field_caption_update_7200 Creates new schema
image_field_caption_update_7201 Migrate data to new format
image_field_caption_update_7202 Remove old data
image_field_caption_update_7203 Make the revision caption field non mandatory
image_field_caption_update_7204 Move the instance setting from ['image_field_caption'] to ['image_field_caption']['enabled']. Reason: ['image_field_caption'] becomes the wrapper for additional settings.
image_field_caption_update_7205 Set image_field_caption_wrapper defaults on field instances.