views_handler_field_prerender_list.inc

  1. 3.x handlers/views_handler_field_prerender_list.inc
  2. 2.x handlers/views_handler_field_prerender_list.inc

Definition of views_handler_field_prerender_list.

File

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