You are here

function cer_batch_update_existing_entities in Corresponding Entity References 7

Batch Operation Callback

See also

cer_batch_index_remaining()

1 string reference to 'cer_batch_update_existing_entities'
cer_batch_index_remaining in ./cer.module
Submit a batch job to index the remaining, unindexed content.

File

./cer.module, line 201
Module file providing the "corresponding entity reference" module main functions.

Code

function cer_batch_update_existing_entities($type, $limit, &$context) {

  // If we are on our first time through.
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_entity'] = 0;
    $count = 0;
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $type)
      ->entityCondition('entity_id', 0, '>')
      ->addMetaData('account', user_load(1));

    // Run the query as user 1.
    $result = $query
      ->execute();
    if (isset($result[$type])) {
      $ids = array_keys($result[$type]);
      $count += count($ids);
    }
    $context['sandbox']['max'] = $count;
  }
  $ids = array();
  $args = $type;
  $args['current_entity'] = $context['sandbox']['current_entity'];
  $labels = _cer_entity_ids();
  $label = $labels[$type];
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $type)
    ->entityCondition('entity_id', $args['current_entity'], '>')
    ->propertyOrderBy($label, 'ASC')
    ->range(0, $limit)
    ->addMetaData('account', user_load(1));

  // Run the query as user 1.
  $result = $query
    ->execute();
  if (isset($result[$type])) {
    $ids = array_keys($result[$type]);
  }
  foreach ($ids as $id) {
    $entity = entity_load($type, array(
      $id,
    ));
    $entity = $entity[$id];
    cer_processing_entity('update', $entity, $type, TRUE);

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_entity'] = $entity->{$label};
    $context['message'] = t('Processed @current of @total entities', array(
      '@current' => $context['sandbox']['progress'],
      '@total' => $context['sandbox']['max'],
    ));
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }

  // Put the total into the results section when we're finished so we can show
  // it to the admin.
  if ($context['finished']) {
    $context['results']['count'] = $context['sandbox']['progress'];
  }
}