Projet

Général

Profil

Paste
Télécharger (2,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / file_entity / file_entity.pathauto.inc @ 66c11afc

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
  switch ($op) {
24
    case 'settings':
25
      $settings = array();
26
      $settings['module'] = 'file';
27
      $settings['token_type'] = 'file';
28
      $settings['groupheader'] = t('File paths');
29
      $settings['patterndescr'] = t('Default path pattern (applies to all file types with blank patterns below)');
30
      $settings['patterndefault'] = '';
31
      $settings['batch_update_callback'] = 'file_entity_pathauto_bulk_update_batch_process';
32
      $settings['batch_file'] = drupal_get_path('module', 'file_entity') . '/file_entity.pathauto.inc';
33

    
34
      foreach (file_type_get_enabled_types() as $file_type => $type) {
35
        $settings['patternitems'][$file_type] = t('Pattern for all @file_type paths.', array('@file_type' => $type->label));
36
      }
37
      return (object) $settings;
38

    
39
    default:
40
      break;
41
  }
42
}
43

    
44
/**
45
 * Batch processing callback; Generate aliases for files.
46
 */
47
function file_entity_pathauto_bulk_update_batch_process(&$context) {
48
  if (!isset($context['sandbox']['current'])) {
49
    $context['sandbox']['count'] = 0;
50
    $context['sandbox']['current'] = 0;
51
  }
52

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

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

    
66
    // If there are no files to update, the stop immediately.
67
    if (!$context['sandbox']['total']) {
68
      $context['finished'] = 1;
69
      return;
70
    }
71
  }
72

    
73
  $query->range(0, 25);
74
  $fids = $query->execute()->fetchCol();
75

    
76
  pathauto_file_update_alias_multiple($fids, 'bulkupdate');
77
  $context['sandbox']['count'] += count($fids);
78
  $context['sandbox']['current'] = max($fids);
79
  $context['message'] = t('Updated alias for file @fid.', array('@fid' => end($fids)));
80

    
81
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
82
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
83
  }
84
}