PrerenderList.php 3.48 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\field\PrerenderList.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\field;
9

10
use Drupal\Component\Annotation\PluginID;
dawehner's avatar
dawehner committed
11

merlinofchaos's avatar
merlinofchaos committed
12 13 14
/**
 * Field handler to provide a list of items.
 *
15
 * The items are expected to be loaded by a child object during preRender,
merlinofchaos's avatar
merlinofchaos committed
16 17 18 19 20
 * and 'my field' is expected to be the pointer to the items in the list.
 *
 * Items to render should be in a list in $this->items
 *
 * @ingroup views_field_handlers
21
 *
22
 * @PluginID("prerender_list")
dawehner's avatar
dawehner committed
23
 */
24
class PrerenderList extends FieldPluginBase {
25

merlinofchaos's avatar
merlinofchaos committed
26 27 28 29 30 31 32 33 34 35
  /**
   * Stores all items which are used to render the items.
   * It should be keyed first by the id of the base table, for example nid.
   * The second key is the id of the thing which is displayed multiple times
   * per row, for example the tid.
   *
   * @var array
   */
  var $items = array();

36 37
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
38 39 40 41 42 43 44

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

    return $options;
  }

45
  public function buildOptionsForm(&$form, &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    $form['type'] = array(
      '#type' => 'radios',
      '#title' => t('Display type'),
      '#options' => array(
        'ul' => t('Unordered list'),
        'ol' => t('Ordered list'),
        'separator' => t('Simple separator'),
      ),
      '#default_value' => $this->options['type'],
    );

    $form['separator'] = array(
      '#type' => 'textfield',
      '#title' => t('Separator'),
      '#default_value' => $this->options['separator'],
61 62 63 64 65
      '#states' => array(
        'visible' => array(
          ':input[name="options[type]"]' => array('value' => 'separator'),
        ),
      ),
merlinofchaos's avatar
merlinofchaos committed
66
    );
67
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
68 69 70 71 72 73 74 75
  }

  /**
   * Render all items in this field together.
   *
   * When using advanced render, each possible item in the list is rendered
   * individually. Then the items are all pasted together.
   */
76
  protected function renderItems($items) {
merlinofchaos's avatar
merlinofchaos committed
77 78
    if (!empty($items)) {
      if ($this->options['type'] == 'separator') {
79
        return implode($this->sanitizeValue($this->options['separator'], 'xss_admin'), $items);
merlinofchaos's avatar
merlinofchaos committed
80 81
      }
      else {
82 83 84 85
        $item_list = array(
          '#theme' => 'item_list',
          '#items' => $items,
          '#title' => NULL,
86
          '#list_type' => $this->options['type'],
87 88
        );
        return drupal_render($item_list);
merlinofchaos's avatar
merlinofchaos committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102
      }
    }
  }

  /**
   * Return an array of items for the field.
   *
   * Items should be stored in the result array, if possible, as an array
   * with 'value' as the actual displayable value of the item, plus
   * any items that might be found in the 'alter' options array for
   * creating links, such as 'path', 'fragment', 'query' etc, such a thing
   * is to be made. Additionally, items that might be turned into tokens
   * should also be in this array.
   */
103
  public function getItems($values) {
104
    $field = $this->getValue($values);
merlinofchaos's avatar
merlinofchaos committed
105 106 107 108 109 110 111 112 113 114 115
    if (!empty($this->items[$field])) {
      return $this->items[$field];
    }

    return array();
  }

  /**
   * Determine if advanced rendering is allowed.
   *
   * By default, advanced rendering will NOT be allowed if the class
116
   * inheriting from this does not implement a 'renderItems' method.
merlinofchaos's avatar
merlinofchaos committed
117
   */
118
  protected function allowAdvancedRender() {
merlinofchaos's avatar
merlinofchaos committed
119 120 121 122
    // Note that the advanced render bits also use the presence of
    // this method to determine if it needs to render items as a list.
    return method_exists($this, 'render_item');
  }
123

merlinofchaos's avatar
merlinofchaos committed
124
}