You are here

function field_defaults_batch_process in Field Defaults 7.2

Same name and namespace in other branches
  1. 7 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 268
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();
  if (isset($result[$entity_type])) {
    $update_ids = array_keys($result[$entity_type]);
    $entities = entity_load($entity_type, $update_ids);
    foreach ($entities as $entity_id => $entity) {

      // Check for existing data.
      if ($variables['no_overwrite'] && !empty($entity->{$field}[$langcode][0])) {
        continue;
      }

      // Need to account for Deltas
      foreach ($variables['default_value'] as $delta => $value) {

        // Values can be multiple columns as well depending on widget
        foreach ($value as $key => $data) {

          // support tokens.
          if (module_exists('token')) {
            $data = token_replace($data, [
              $entity_type => $entity,
            ]);
          }
          $entity->{$field}[$langcode][$delta][$key] = $data;
        }

        // @TODO: Need to Catch?
        entity_save($entity_type, $entity);
      }
      $context['results'][] = t('Field defaults for field @field on @bundle @id updated.', [
        '@field' => $field,
        '@bundle' => $entity_type . ': ' . $bundle,
        '@id' => $entity_id,
      ]);
    }
  }

  // Track progress.
  $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'];
  }
}