Projet

Général

Profil

Paste
Télécharger (1,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / system / views_handler_field_file_extension.inc @ 5d12d676

1
<?php
2

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

    
8
/**
9
 * Returns a pure file extension of the file, for example 'module'.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_file_extension extends views_handler_field {
14

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

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function options_form(&$form, &$form_state) {
28
    parent::options_form($form, $form_state);
29
    $form['extension_detect_tar'] = array(
30
      '#type' => 'checkbox',
31
      '#title' => t('Detect if tar is part of the extension'),
32
      '#description' => t("See if the previous extension is '.tar' and if so, add that, so we see 'tar.gz' or 'tar.bz2' instead of just 'gz'."),
33
      '#default_value' => $this->options['extension_detect_tar'],
34
    );
35
  }
36

    
37
  /**
38
   * {@inheritdoc}
39
   */
40
  public function render($values) {
41
    $value = $this->get_value($values);
42
    if (!$this->options['extension_detect_tar']) {
43
      if (preg_match('/\.([^\.]+)$/', $value, $match)) {
44
        return $this->sanitize_value($match[1]);
45
      }
46
    }
47
    else {
48
      $file_parts = explode('.', basename($value));
49
      // If there is an extension.
50
      if (count($file_parts) > 1) {
51
        $extension = array_pop($file_parts);
52
        $last_part_in_name = array_pop($file_parts);
53
        if ($last_part_in_name === 'tar') {
54
          $extension = 'tar.' . $extension;
55
        }
56
        return $this->sanitize_value($extension);
57
      }
58
    }
59
  }
60

    
61
}