Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_machine_name.inc @ 76df55b7

1
<?php
2

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

    
8
/**
9
 * Field handler whichs allows to show machine name content as human name.
10
 * @ingroup views_field_handlers
11
 *
12
 * Definition items:
13
 * - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
14
 * - options arguments: An array of arguments to pass to the options callback.
15
 */
16
class views_handler_field_machine_name extends views_handler_field {
17
  /**
18
   * @var array Stores the available options.
19
   */
20
  var $value_options;
21

    
22
  function get_value_options() {
23
    if (isset($this->value_options)) {
24
      return;
25
    }
26

    
27
    if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
28
      if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
29
        $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
30
      }
31
      else {
32
        $this->value_options = call_user_func($this->definition['options callback']);
33
      }
34
    }
35
    else {
36
      $this->value_options = array();
37
    }
38
  }
39

    
40
  function option_definition() {
41
    $options = parent::option_definition();
42
    $options['machine_name'] = array('default' => FALSE, 'bool' => TRUE);
43

    
44
    return $options;
45
  }
46

    
47
  function options_form(&$form, &$form_state) {
48
    parent::options_form($form, $form_state);
49

    
50
    $form['machine_name'] = array(
51
      '#title' => t('Output machine name'),
52
      '#description' => t('Display field as machine name.'),
53
      '#type' => 'checkbox',
54
      '#default_value' => !empty($this->options['machine_name']),
55
    );
56
  }
57

    
58
  function pre_render(&$values) {
59
    $this->get_value_options();
60
  }
61

    
62
  function render($values) {
63
    $value = $values->{$this->field_alias};
64
    if (!empty($this->options['machine_name']) || !isset($this->value_options[$value])) {
65
      $result = check_plain($value);
66
    }
67
    else {
68
      $result = $this->value_options[$value];
69
    }
70

    
71
    return $result;
72
  }
73
}