You are here

asin.install in Amazon Product Advertisement API 7.2

Same filename and directory in other branches
  1. 6 asin/asin.install
  2. 7 asin/asin.install

Install, update and uninstall functions for the asin module.

File

asin/asin.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the asin module.
 *
 */

/**
 * Implements of hook_install().
 */
function asin_install() {
}

/**
 * Implements of hook_uninstall().
 */
function asin_uninstall() {
}

/**
* Implements hook_field_schema().
*/
function asin_field_schema($field) {
  $columns = array(
    'asin' => array(
      'type' => 'varchar',
      'length' => 32,
      'not null' => FALSE,
    ),
  );
  return array(
    'columns' => $columns,
  );
}

/**
 * Fix the formatter machine names for asin field displays.
 */
function asin_update_7000(&$sandbox) {
  if (!isset($sandbox['progress'])) {

    // Find all ASIN field instances.
    foreach (field_info_fields() as $field_name => $field) {
      if ($field['type'] == 'asin') {
        foreach ($field['bundles'] as $entity_type => $bundles) {
          foreach ($bundles as $bundle) {
            $sandbox['instances'][] = array(
              'entity_type' => $entity_type,
              'bundle' => $bundle,
              'field_name' => $field_name,
            );
          }
        }
      }
    }
    $sandbox['progress'] = 0;
    $sandbox['max'] = count($sandbox['instances']);

    // If there are no field instances to update, return.
    if (!$sandbox['max']) {
      return;
    }
  }
  $instance = $sandbox['instances'][$sandbox['progress']];
  $instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  $updated = FALSE;
  foreach ($instance['display'] as &$display) {
    if (in_array($display['type'], array(
      'default',
      'details',
      'thumbnail',
      'medium',
      'large',
      'inline',
    ))) {
      $updated = TRUE;
      $display['type'] = 'asin_' . $display['type'];
    }
  }
  if ($updated) {

    // Only trigger instance update if we actually changed anything.
    field_update_instance($instance);
  }
  $sandbox['progress']++;
  $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}

/**
 * Update widget settings for existing asin fields using the autocomplete widget.
 */
function asin_update_7100(&$sandbox) {
  if (!isset($sandbox['progress'])) {

    // Find all ASIN field instances.
    foreach (field_info_fields() as $field_name => $field) {
      if ($field['type'] == 'asin') {
        foreach ($field['bundles'] as $entity_type => $bundles) {
          foreach ($bundles as $bundle) {
            $sandbox['instances'][] = array(
              'entity_type' => $entity_type,
              'bundle' => $bundle,
              'field_name' => $field_name,
            );
          }
        }
      }
    }
    $sandbox['progress'] = 0;
    $sandbox['max'] = count($sandbox['instances']);

    // If there are no field instances to update, return.
    if (!$sandbox['max']) {
      return;
    }
  }
  $instance = $sandbox['instances'][$sandbox['progress']];
  $instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  $updated = FALSE;
  if ($instance['widget']['type'] == 'asin_autocomplete') {
    if (empty($instance['widget']['settings'])) {
      $instance['widget']['settings'] = array(
        'widget_settings' => array(
          'locale' => variable_get('amazon_default_locale', 'US'),
          'productgroup' => 'All',
        ),
      );
      $updated = TRUE;
    }
  }
  if ($updated) {

    // Only trigger instance update if we actually changed anything.
    field_update_instance($instance);
  }
  $sandbox['progress']++;
  $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}

/**
 * Rebuild menu because asin autocomplete callback path has changed.
 */
function asin_update_7101() {
  menu_rebuild();
}

/**
 * Update widget settings for existing asin fields using the text widget.
 */
function asin_update_7102(&$sandbox) {
  if (!isset($sandbox['progress'])) {

    // Find all ASIN field instances.
    foreach (field_info_fields() as $field_name => $field) {
      if ($field['type'] == 'asin') {
        foreach ($field['bundles'] as $entity_type => $bundles) {
          foreach ($bundles as $bundle) {
            $sandbox['instances'][] = array(
              'entity_type' => $entity_type,
              'bundle' => $bundle,
              'field_name' => $field_name,
            );
          }
        }
      }
    }
    $sandbox['progress'] = 0;
    $sandbox['max'] = count($sandbox['instances']);

    // If there are no field instances to update, return.
    if (!$sandbox['max']) {
      return;
    }
  }
  $instance = $sandbox['instances'][$sandbox['progress']];
  $instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  $updated = FALSE;
  if ($instance['widget']['type'] == 'asin_text') {
    if (!isset($instance['widget']['settings']['widget_settings']['locale'])) {
      $instance['widget']['settings'] = array(
        'widget_settings' => array(
          'locale' => variable_get('amazon_default_locale', 'US'),
        ),
      );
      $updated = TRUE;
    }
  }
  if ($updated) {

    // Only trigger instance update if we actually changed anything.
    field_update_instance($instance);
  }
  $sandbox['progress']++;
  $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}

Functions

Namesort descending Description
asin_field_schema Implements hook_field_schema().
asin_install Implements of hook_install().
asin_uninstall Implements of hook_uninstall().
asin_update_7000 Fix the formatter machine names for asin field displays.
asin_update_7100 Update widget settings for existing asin fields using the autocomplete widget.
asin_update_7101 Rebuild menu because asin autocomplete callback path has changed.
asin_update_7102 Update widget settings for existing asin fields using the text widget.