Project

General

Profile

Paste
Download (2.63 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / file_entity / file_entity.pathauto.inc @ a8cee257

1
<?php
2

    
3
/**
4
 * @file
5
 * Pathauto integration for files.
6
 *
7
 * @ingroup pathauto
8
 */
9

    
10
/**
11
 * Implements hook_path_alias_types().
12
 */
13
function file_entity_path_alias_types() {
14
  $objects['file/'] = t('Files');
15

    
16
  return $objects;
17
}
18

    
19
/**
20
 * Implements hook_pathauto().
21
 */
22
function file_entity_pathauto($op) {
23
  // Allow Pathauto Entity settings to override File Entity's pathauto.
24
  if (module_exists('pathauto_entity')) {
25
    return;
26
  }
27

    
28
  switch ($op) {
29
    case 'settings':
30
      $settings = array();
31
      $settings['module'] = 'file';
32
      $settings['token_type'] = 'file';
33
      $settings['groupheader'] = t('File paths');
34
      $settings['patterndescr'] = t('Default path pattern (applies to all file types with blank patterns below)');
35
      $settings['patterndefault'] = '';
36
      $settings['batch_update_callback'] = 'file_entity_pathauto_bulk_update_batch_process';
37
      $settings['batch_file'] = drupal_get_path('module', 'file_entity') . '/file_entity.pathauto.inc';
38

    
39
      foreach (file_type_get_enabled_types() as $file_type => $type) {
40
        $settings['patternitems'][$file_type] = t('Pattern for all @file_type paths.', array('@file_type' => $type->label));
41
      }
42
      return (object) $settings;
43

    
44
    default:
45
      break;
46
  }
47
}
48

    
49
/**
50
 * Batch processing callback; Generate aliases for files.
51
 */
52
function file_entity_pathauto_bulk_update_batch_process(&$context) {
53
  if (!isset($context['sandbox']['current'])) {
54
    $context['sandbox']['count'] = 0;
55
    $context['sandbox']['current'] = 0;
56
  }
57

    
58
  $query = db_select('file_managed', 'fm');
59
  $query->leftJoin('url_alias', 'ua', "CONCAT('file/', fm.fid) = ua.source");
60
  $query->addField('fm', 'fid');
61
  $query->isNull('ua.source');
62
  $query->condition('fm.fid', $context['sandbox']['current'], '>');
63
  $query->orderBy('fm.fid');
64
  $query->addTag('pathauto_bulk_update');
65
  $query->addMetaData('entity', 'file');
66

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

    
71
    // If there are no files to update, the stop immediately.
72
    if (!$context['sandbox']['total']) {
73
      $context['finished'] = 1;
74
      return;
75
    }
76
  }
77

    
78
  $query->range(0, 25);
79
  $fids = $query->execute()->fetchCol();
80

    
81
  pathauto_file_update_alias_multiple($fids, 'bulkupdate');
82
  $context['sandbox']['count'] += count($fids);
83
  $context['sandbox']['current'] = max($fids);
84
  $context['message'] = t('Updated alias for file @fid.', array('@fid' => end($fids)));
85

    
86
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
87
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
88
  }
89
}