You are here

function field_defaults_batch_process in Field Defaults 7

Same name and namespace in other branches
  1. 7.2 field_defaults.module \field_defaults_batch_process()

The batch processor.

@TODO: There is a chance of someone using LANGUAGE_NONE for the entity, but still having field translation. This still should work but is leaving erroneous records in the field I believe

1 string reference to 'field_defaults_batch_process'
field_defaults_batch_run in ./field_defaults.module
The batch callback.

File

./field_defaults.module, line 211
field_defaults.module

Code

function field_defaults_batch_process($variables, $langcode, &$context) {
  $entity_type = $variables['entity_type'];
  $bundle = $variables['bundle'];
  $field = $variables['field'];
  if (!isset($context['sandbox']['current'])) {
    $context['sandbox']['count'] = 0;
    $context['sandbox']['current'] = 0;
  }
  $entity_info = entity_get_info($entity_type);
  $entity_keys = $entity_info['entity keys'];

  // Get all entities to update
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $entity_type)
    ->propertyCondition($entity_info['entity keys']['id'], $context['sandbox']['current'], '>')
    ->propertyOrderBy($entity_info['entity keys']['id']);
  if (!empty($entity_keys['bundle'])) {
    $query
      ->propertyCondition($entity_keys['bundle'], $bundle);
  }
  if (!$variables['field_translation'] && !empty($entity_keys['language'])) {
    $query
      ->propertyCondition($entity_keys['language'], $langcode);

    // Set language for fields as LANGUAGE_NONE since we are updating based on
    // content and not field
    $langcode = LANGUAGE_NONE;
  }

  // Get the total amount of entities to process.
  if (!isset($context['sandbox']['total'])) {
    $context['sandbox']['total'] = $query
      ->count()
      ->execute();
    $query->count = FALSE;

    // If there are no entities, stop now
    if (!$context['sandbox']['total']) {
      $context['finished'] = 1;
      return;
    }
  }

  // Process 25 entities per iteration.
  $query
    ->range(0, 25);
  $result = $query
    ->execute();
  foreach ($result[$entity_type] as $entity_id => $entity) {

    // Account for revisions
    $revision = $entity_id;
    if (isset($entity->{$entity_keys['revision']})) {
      $revision = $entity->{$entity_keys['revision']};
    }

    // Need to account for Deltas
    foreach ($variables['default_value'] as $delta => $value) {
      $field_keys = array(
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
        'deleted' => 0,
        'delta' => $delta,
        'language' => $langcode,
      );
      $field_values = array(
        'bundle' => $bundle,
        'revision_id' => $revision,
      );

      // Values can be multiple columns as well depending on widget
      foreach ($value as $key => $data) {
        $field_values[$field . '_' . $key] = $data;
      }

      // Update everything
      db_merge('field_data_' . $field)
        ->key($field_keys)
        ->fields($field_values)
        ->execute();
      db_merge('field_revision_' . $field)
        ->key($field_keys)
        ->fields($field_values)
        ->execute();
    }
    $context['results'][] = t('Field defaults for field @field on @bundle @id updated.', array(
      '@field' => $field,
      '@bundle' => $entity_type . ': ' . $bundle,
      '@id' => $entity_id,
    ));
  }

  // reset cache on these updates
  $update_ids = array_keys($result[$entity_type]);
  entity_get_controller($entity_type)
    ->resetCache($update_ids);
  $context['sandbox']['count'] += count($update_ids);
  $context['sandbox']['current'] = max($update_ids);
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  }
}