Projet

Général

Profil

Paste
Télécharger (4,85 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_prerender_list.inc @ 4003efde

1
<?php
2

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

    
8
/**
9
 * Field handler to provide a list of items.
10
 *
11
 * The items are expected to be loaded by a child object during pre_render,
12
 * and 'my field' is expected to be the pointer to the items in the list.
13
 *
14
 * Items to render should be in a list in $this->items
15
 *
16
 * @ingroup views_field_handlers
17
 */
18
class views_handler_field_prerender_list extends views_handler_field {
19

    
20
  /**
21
   * Stores all items which are used to render the items.
22
   *
23
   * It should be keyed first by the id of the base table, for example nid.
24
   * The second key is the id of the thing which is displayed multiple times
25
   * per row, for example the tid.
26
   *
27
   * @var array
28
   */
29
  public $items = array();
30

    
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function option_definition() {
35
    $options = parent::option_definition();
36

    
37
    $options['type'] = array('default' => 'separator');
38
    $options['separator'] = array('default' => ', ');
39

    
40
    return $options;
41
  }
42

    
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public function options_form(&$form, &$form_state) {
47
    $form['type'] = array(
48
      '#type' => 'radios',
49
      '#title' => t('Display type'),
50
      '#options' => array(
51
        'ul' => t('Unordered list'),
52
        'ol' => t('Ordered list'),
53
        'separator' => t('Simple separator'),
54
      ),
55
      '#default_value' => $this->options['type'],
56
    );
57

    
58
    $form['separator'] = array(
59
      '#type' => 'textfield',
60
      '#title' => t('Separator'),
61
      '#default_value' => $this->options['separator'],
62
      '#dependency' => array('radio:options[type]' => array('separator')),
63
    );
64
    parent::options_form($form, $form_state);
65
  }
66

    
67
  /**
68
   * Render the field.
69
   *
70
   * This function is deprecated, but left in for older systems that have not
71
   * yet or won't update their prerender list fields. If a render_item method
72
   * exists, this will not get used by advanced_render.
73
   */
74
  public function render($values) {
75
    $field = $this->get_value($values);
76
    if (!empty($this->items[$field])) {
77
      if ($this->options['type'] == 'separator') {
78
        return implode($this->sanitize_value($this->options['separator']), $this->items[$field]);
79
      }
80
      else {
81
        return theme('item_list',
82
          array(
83
            'items' => $this->items[$field],
84
            'title' => NULL,
85
            'type' => $this->options['type'],
86
          ));
87
      }
88
    }
89
  }
90

    
91
  /**
92
   * Render all items in this field together.
93
   *
94
   * When using advanced render, each possible item in the list is rendered
95
   * individually. Then the items are all pasted together.
96
   */
97
  public function render_items($items) {
98
    if (!empty($items)) {
99
      if ($this->options['type'] == 'separator') {
100
        return implode($this->sanitize_value($this->options['separator'], 'xss_admin'), $items);
101
      }
102
      else {
103
        return theme('item_list',
104
          array(
105
            'items' => $items,
106
            'title' => NULL,
107
            'type' => $this->options['type'],
108
          ));
109
      }
110
    }
111
  }
112

    
113
  /**
114
   * Return an array of items for the field.
115
   *
116
   * Items should be stored in the result array, if possible, as an array with
117
   * 'value' as the actual displayable value of the item, plus any items that
118
   * might be found in the 'alter' options array for creating links, such as
119
   * 'path', 'fragment', 'query' etc, such a thing is to be made. Additionally,
120
   * items that might be turned into tokens should also be in this array.
121
   *
122
   * @param mixed $values
123
   *
124
   * @return array
125
   *   The items.
126
   */
127
  public function get_items($values) {
128
    // Only the parent get_value returns a single field.
129
    $field = parent::get_value($values);
130
    if (!empty($this->items[$field])) {
131
      return $this->items[$field];
132
    }
133

    
134
    return array();
135
  }
136

    
137
  /**
138
   * Get the value that's supposed to be rendered.
139
   *
140
   * @param object $values
141
   *   An object containing all retrieved values.
142
   * @param string $field
143
   *   Optional name of the field where the value is stored.
144
   * @param bool $raw
145
   *   Use the raw data and not the data defined in pre_render
146
   */
147
  public function get_value($values, $field = NULL, $raw = FALSE) {
148
    if ($raw) {
149
      return parent::get_value($values, $field);
150
    }
151
    $item = $this->get_items($values);
152
    $item = (array) $item;
153
    if (isset($field) && isset($item[$field])) {
154
      return $item[$field];
155
    }
156
    return $item;
157
  }
158

    
159
  /**
160
   * Determine if advanced rendering is allowed.
161
   *
162
   * By default, advanced rendering will NOT be allowed if the class
163
   * inheriting from this does not implement a 'render_items' method.
164
   *
165
   * @return bool
166
   *   Whether or not the the render method exists.
167
   */
168
  public function allow_advanced_render() {
169
    // Note that the advanced render bits also use the presence of
170
    // this method to determine if it needs to render items as a list.
171
    return method_exists($this, 'render_item');
172
  }
173

    
174
}