Projet

Général

Profil

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

root / drupal7 / sites / all / modules / file_entity / views / views_handler_field_file_link_usage.inc @ 59ae487e

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_file_link_usage.
6
 */
7

    
8
/**
9
 * Field handler to present a link to view the usage of a file.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_file_link_usage extends views_handler_field_file_link {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  function option_definition() {
19
    $options = parent::option_definition();
20
    $options['count_entities_once'] = array('default' => TRUE);
21
    return $options;
22
  }
23

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  function options_form(&$form, &$form_state) {
28
    $form['count_entities_once'] = array(
29
      '#type' => 'checkbox',
30
      '#title' => t('Count each unique entity once'),
31
      '#description' => t('Files can be used multiple times for an entity, especially when an entity is revisionable.'),
32
      '#default_value' => !empty($this->options['count_entities_once']),
33
    );
34
    parent::options_form($form, $form_state);
35
  }
36

    
37
  /**
38
   * Renders the link.
39
   */
40
  function render_link($file, $values) {
41
    // Ensure user has access to update this file.
42
    if (!file_entity_access('update', $file)) {
43
      return;
44
    }
45

    
46
    $this->options['alter']['make_link'] = TRUE;
47
    $this->options['alter']['path'] = "file/$file->fid/usage";
48
    $this->options['alter']['query'] = drupal_get_destination();
49

    
50
    // Get total count for each file.
51
    $total_count = 0;
52
    $file_usage = file_usage_list($file);
53
    $count_entities_once = !empty($this->options['count_entities_once']);
54
    foreach ($file_usage as $module => $usage) {
55
      foreach ($usage as $entity_type => $entity_ids) {
56
        if ($count_entities_once) {
57
          // Just count each unique entity once.
58
          $total_count += count($entity_ids);
59
        }
60
        else {
61
          // Count multiple usages for each entity.
62
          foreach ($entity_ids as $id => $count) {
63
            $total_count += $count;
64
          }
65
        }
66
      }
67
    }
68

    
69
    $text = !empty($this->options['text']) ? $this->options['text'] : format_plural((int) $total_count, '1 place', '@count places');
70
    return $text;
71
  }
72
}