You are here

function file_entity_pathauto_bulk_update_batch_process in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.pathauto.inc \file_entity_pathauto_bulk_update_batch_process()

Batch processing callback; Generate aliases for files.

1 string reference to 'file_entity_pathauto_bulk_update_batch_process'
file_entity_pathauto in ./file_entity.pathauto.inc
Implements hook_pathauto().

File

./file_entity.pathauto.inc, line 52
Pathauto integration for files.

Code

function file_entity_pathauto_bulk_update_batch_process(&$context) {
  if (!isset($context['sandbox']['current'])) {
    $context['sandbox']['count'] = 0;
    $context['sandbox']['current'] = 0;
  }
  $query = db_select('file_managed', 'fm');
  $query
    ->leftJoin('url_alias', 'ua', "CONCAT('file/', fm.fid) = ua.source");
  $query
    ->addField('fm', 'fid');
  $query
    ->isNull('ua.source');
  $query
    ->condition('fm.fid', $context['sandbox']['current'], '>');
  $query
    ->orderBy('fm.fid');
  $query
    ->addTag('pathauto_bulk_update');
  $query
    ->addMetaData('entity', 'file');

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

    // If there are no files to update, the stop immediately.
    if (!$context['sandbox']['total']) {
      $context['finished'] = 1;
      return;
    }
  }
  $query
    ->range(0, 25);
  $fids = $query
    ->execute()
    ->fetchCol();
  pathauto_file_update_alias_multiple($fids, 'bulkupdate');
  $context['sandbox']['count'] += count($fids);
  $context['sandbox']['current'] = max($fids);
  $context['message'] = t('Updated alias for file @fid.', array(
    '@fid' => end($fids),
  ));
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  }
}