views_handler_field_prerender_list.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_prerender_list.
  5. */
  6. /**
  7. * Field handler to provide a list of items.
  8. *
  9. * The items are expected to be loaded by a child object during pre_render,
  10. * and 'my field' is expected to be the pointer to the items in the list.
  11. *
  12. * Items to render should be in a list in $this->items
  13. *
  14. * @ingroup views_field_handlers
  15. */
  16. class views_handler_field_prerender_list extends views_handler_field {
  17. /**
  18. * Stores all items which are used to render the items.
  19. *
  20. * It should be keyed first by the id of the base table, for example nid.
  21. * The second key is the id of the thing which is displayed multiple times
  22. * per row, for example the tid.
  23. *
  24. * @var array
  25. */
  26. public $items = array();
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function option_definition() {
  31. $options = parent::option_definition();
  32. $options['type'] = array('default' => 'separator');
  33. $options['separator'] = array('default' => ', ');
  34. return $options;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function options_form(&$form, &$form_state) {
  40. $form['type'] = array(
  41. '#type' => 'radios',
  42. '#title' => t('Display type'),
  43. '#options' => array(
  44. 'ul' => t('Unordered list'),
  45. 'ol' => t('Ordered list'),
  46. 'separator' => t('Simple separator'),
  47. ),
  48. '#default_value' => $this->options['type'],
  49. );
  50. $form['separator'] = array(
  51. '#type' => 'textfield',
  52. '#title' => t('Separator'),
  53. '#default_value' => $this->options['separator'],
  54. '#dependency' => array('radio:options[type]' => array('separator')),
  55. );
  56. parent::options_form($form, $form_state);
  57. }
  58. /**
  59. * Render the field.
  60. *
  61. * This function is deprecated, but left in for older systems that have not
  62. * yet or won't update their prerender list fields. If a render_item method
  63. * exists, this will not get used by advanced_render.
  64. */
  65. public function render($values) {
  66. $field = $this->get_value($values);
  67. if (!empty($this->items[$field])) {
  68. if ($this->options['type'] == 'separator') {
  69. return implode($this->sanitize_value($this->options['separator']), $this->items[$field]);
  70. }
  71. else {
  72. return theme('item_list',
  73. array(
  74. 'items' => $this->items[$field],
  75. 'title' => NULL,
  76. 'type' => $this->options['type'],
  77. ));
  78. }
  79. }
  80. }
  81. /**
  82. * Render all items in this field together.
  83. *
  84. * When using advanced render, each possible item in the list is rendered
  85. * individually. Then the items are all pasted together.
  86. */
  87. public function render_items($items) {
  88. if (!empty($items)) {
  89. if ($this->options['type'] == 'separator') {
  90. return implode($this->sanitize_value($this->options['separator'], 'xss_admin'), $items);
  91. }
  92. else {
  93. return theme('item_list',
  94. array(
  95. 'items' => $items,
  96. 'title' => NULL,
  97. 'type' => $this->options['type'],
  98. ));
  99. }
  100. }
  101. }
  102. /**
  103. * Return an array of items for the field.
  104. *
  105. * Items should be stored in the result array, if possible, as an array with
  106. * 'value' as the actual displayable value of the item, plus any items that
  107. * might be found in the 'alter' options array for creating links, such as
  108. * 'path', 'fragment', 'query' etc, such a thing is to be made. Additionally,
  109. * items that might be turned into tokens should also be in this array.
  110. *
  111. * @param mixed $values
  112. *
  113. * @return array
  114. * The items.
  115. */
  116. public function get_items($values) {
  117. // Only the parent get_value returns a single field.
  118. $field = parent::get_value($values);
  119. if (!empty($this->items[$field])) {
  120. return $this->items[$field];
  121. }
  122. return array();
  123. }
  124. /**
  125. * Get the value that's supposed to be rendered.
  126. *
  127. * @param object $values
  128. * An object containing all retrieved values.
  129. * @param string $field
  130. * Optional name of the field where the value is stored.
  131. * @param bool $raw
  132. * Use the raw data and not the data defined in pre_render
  133. */
  134. public function get_value($values, $field = NULL, $raw = FALSE) {
  135. if ($raw) {
  136. return parent::get_value($values, $field);
  137. }
  138. $item = $this->get_items($values);
  139. $item = (array) $item;
  140. if (isset($field) && isset($item[$field])) {
  141. return $item[$field];
  142. }
  143. return $item;
  144. }
  145. /**
  146. * Determine if advanced rendering is allowed.
  147. *
  148. * By default, advanced rendering will NOT be allowed if the class
  149. * inheriting from this does not implement a 'render_items' method.
  150. *
  151. * @return bool
  152. * Whether or not the the render method exists.
  153. */
  154. public function allow_advanced_render() {
  155. // Note that the advanced render bits also use the presence of
  156. // this method to determine if it needs to render items as a list.
  157. return method_exists($this, 'render_item');
  158. }
  159. }