Project

General

Profile

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

root / drupal7 / sites / all / modules / entity / views / handlers / entity_views_handler_field_options.inc @ 7d7b5830

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the entity_views_handler_field_options class.
6
 */
7

    
8
/**
9
 * A handler to provide proper displays for values chosen from a set of options.
10
 *
11
 * This handler may only be used in conjunction with data selection based Views
12
 * tables or other base tables using a query plugin that supports data
13
 * selection.
14
 *
15
 * @see entity_views_field_definition()
16
 * @ingroup views_field_handlers
17
 */
18
class entity_views_handler_field_options extends views_handler_field {
19

    
20
  /**
21
   * Stores the entity type of the result entities.
22
   */
23
  public $entity_type;
24

    
25
  /**
26
   * Stores the result entities' metadata wrappers.
27
   */
28
  public $wrappers = array();
29

    
30
  /**
31
   * Stores the current value when rendering list fields.
32
   */
33
  public $current_value;
34

    
35
  /**
36
   * The key / name mapping for the options.
37
   */
38
  public $option_list;
39

    
40
  /**
41
   * Overridden to add the field for the entity ID (if necessary).
42
   */
43
  public function query() {
44
    EntityFieldHandlerHelper::query($this);
45
  }
46

    
47
  /**
48
   * Adds a click-sort to the query.
49
   */
50
  public function click_sort($order) {
51
    EntityFieldHandlerHelper::click_sort($this, $order);
52
  }
53

    
54
  /**
55
   * Load the entities for all rows that are about to be displayed.
56
   */
57
  public function pre_render(&$values) {
58
    parent::pre_render($values);
59
    EntityFieldHandlerHelper::pre_render($this, $values);
60
  }
61

    
62
  /**
63
   * Overridden to use a metadata wrapper.
64
   */
65
  public function get_value($values, $field = NULL) {
66
    return EntityFieldHandlerHelper::get_value($this, $values, $field);
67
  }
68

    
69
  /**
70
   * Specifies the options this handler uses.
71
   */
72
  public function option_definition() {
73
    $options = parent::option_definition();
74
    $options += EntityFieldHandlerHelper::option_definition($this);
75
    $options['format_name'] = array('default' => TRUE);
76
    return $options;
77
  }
78

    
79
  /**
80
   * Returns an option form for setting this handler's options.
81
   */
82
  public function options_form(&$form, &$form_state) {
83
    parent::options_form($form, $form_state);
84
    EntityFieldHandlerHelper::options_form($this, $form, $form_state);
85

    
86
    $form['format_name'] = array(
87
      '#title' => t('Use human-readable name'),
88
      '#type' => 'checkbox',
89
      '#description' => t("If this is checked, the values' names will be displayed instead of their internal identifiers."),
90
      '#default_value' => $this->options['format_name'],
91
      '#weight' => -5,
92
    );
93
  }
94

    
95
  public function render($values) {
96
    return EntityFieldHandlerHelper::render($this, $values);
97
  }
98

    
99
  /**
100
   * Render a single field value.
101
   */
102
  public function render_single_value($value, $values) {
103
    if (!isset($this->option_list)) {
104
      $this->option_list = array();
105
      $callback = $this->definition['options callback'];
106
      if (is_callable($callback['function'])) {
107
        // If a selector is used, get the name of the selected field.
108
        $field_name = EntityFieldHandlerHelper::get_selector_field_name($this->real_field);
109
        $this->option_list = call_user_func($callback['function'], $field_name, $callback['info'], 'view');
110
      }
111
    }
112
    if ($this->options['format_name'] && isset($this->option_list[$value])) {
113
      $value = $this->option_list[$value];
114
    }
115
    // Sanitization is handled by the wrapper, see
116
    // EntityFieldHandlerHelper::get_value().
117
    return $value;
118
  }
119

    
120
}