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

File

handlers/views_handler_field_prerender_list.inc
View source
  1. <?php
  2. /**
  3. * Field handler to provide a list of items.
  4. *
  5. * The items are expected to be loaded by a child object during pre_render,
  6. * and 'my field' is expected to be the pointer to the items in the list.
  7. *
  8. * Items to render should be in a list in $this->items
  9. *
  10. * @ingroup views_field_handlers
  11. */
  12. class views_handler_field_prerender_list extends views_handler_field {
  13. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['type'] = array('default' => 'separator');
  16. $options['separator'] = array('default' => ', ');
  17. return $options;
  18. }
  19. function options_form(&$form, &$form_state) {
  20. parent::options_form($form, $form_state);
  21. $form['type'] = array(
  22. '#type' => 'radios',
  23. '#title' => t('Display type'),
  24. '#options' => array(
  25. 'ul' => t('Unordered list'),
  26. 'ol' => t('Ordered list'),
  27. 'separator' => t('Simple separator'),
  28. ),
  29. '#default_value' => $this->options['type'],
  30. );
  31. $form['separator'] = array(
  32. '#type' => 'textfield',
  33. '#title' => t('Separator'),
  34. '#default_value' => $this->options['separator'],
  35. '#process' => array('views_process_dependency'),
  36. '#dependency' => array('radio:options[type]' => array('separator')),
  37. );
  38. }
  39. /**
  40. * Render the field.
  41. *
  42. * This function is deprecated, but left in for older systems that have not
  43. * yet or won't update their prerender list fields. If a render_item method
  44. * exists, this will not get used by advanced_render.
  45. */
  46. function render($values) {
  47. $field = $values->{$this->field_alias};
  48. if (!empty($this->items[$field])) {
  49. if ($this->options['type'] == 'separator') {
  50. return implode(check_plain($this->options['separator']), $this->items[$field]);
  51. }
  52. else {
  53. return theme('item_list', $this->items[$field], NULL, $this->options['type']);
  54. }
  55. }
  56. }
  57. /**
  58. * Render all items in this field together.
  59. *
  60. * When using advanced render, each possible item in the list is rendered
  61. * individually. Then the items are all pasted together.
  62. */
  63. function render_items($items) {
  64. if (!empty($items)) {
  65. if ($this->options['type'] == 'separator') {
  66. return implode(check_plain($this->options['separator']), $items);
  67. }
  68. else {
  69. return theme('item_list', $items, NULL, $this->options['type']);
  70. }
  71. }
  72. }
  73. /**
  74. * Return an array of items for the field.
  75. *
  76. * Items should be stored in the result array, if possible, as an array
  77. * with 'value' as the actual displayable value of the item, plus
  78. * any items that might be found in the 'alter' options array for
  79. * creating links, such as 'path', 'fragment', 'query' etc, such a thing
  80. * is to be made. Additionally, items that might be turned into tokens
  81. * should also be in this array.
  82. */
  83. function get_items($values) {
  84. $field = $values->{$this->field_alias};
  85. if (!empty($this->items[$field])) {
  86. return $this->items[$field];
  87. }
  88. return array();
  89. }
  90. /**
  91. * Determine if advanced rendering is allowed.
  92. *
  93. * By default, advanced rendering will NOT be allowed if the class
  94. * inheriting from this does not implement a 'render_items' method.
  95. */
  96. function allow_advanced_render() {
  97. // Note that the advanced render bits also use the presence of
  98. // this method to determine if it needs to render items as a list.
  99. return method_exists($this, 'render_item');
  100. }
  101. }