You are here

function asin_update_7100 in Amazon Product Advertisement API 7

Same name and namespace in other branches
  1. 7.2 asin/asin.install \asin_update_7100()

Update widget settings for existing asin fields using the autocomplete widget.

File

asin/asin.install, line 81
Install, update and uninstall functions for the asin module.

Code

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'];
}