3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
9 * Field handler to provide a list of items.
11 * The items are expected to be loaded by a child object during preRender,
12 * and 'my field' is expected to be the pointer to the items in the list.
14 * Items to render should be in a list in $this->items
16 * @ingroup views_field_handlers
18 abstract class PrerenderList extends FieldPluginBase implements MultiItemsFieldHandlerInterface {
21 * Stores all items which are used to render the items.
22 * It should be keyed first by the id of the base table, for example nid.
23 * The second key is the id of the thing which is displayed multiple times
24 * per row, for example the tid.
33 protected function defineOptions() {
34 $options = parent::defineOptions();
36 $options['type'] = ['default' => 'separator'];
37 $options['separator'] = ['default' => ', '];
45 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
48 '#title' => $this->t('Display type'),
50 'ul' => $this->t('Unordered list'),
51 'ol' => $this->t('Ordered list'),
52 'separator' => $this->t('Simple separator'),
54 '#default_value' => $this->options['type'],
57 $form['separator'] = [
58 '#type' => 'textfield',
59 '#title' => $this->t('Separator'),
60 '#default_value' => $this->options['separator'],
63 ':input[name="options[type]"]' => ['value' => 'separator'],
67 parent::buildOptionsForm($form, $form_state);
73 public function renderItems($items) {
75 if ($this->options['type'] == 'separator') {
77 '#type' => 'inline_template',
78 '#template' => '{{ items|safe_join(separator) }}',
81 'separator' => $this->sanitizeValue($this->options['separator'], 'xss_admin')
87 '#theme' => 'item_list',
90 '#list_type' => $this->options['type'],
93 return drupal_render($render);
100 * Items should be stored in the result array, if possible, as an array
101 * with 'value' as the actual displayable value of the item, plus
102 * any items that might be found in the 'alter' options array for
103 * creating links, such as 'path', 'fragment', 'query' etc, such a thing
104 * is to be made. Additionally, items that might be turned into tokens
105 * should also be in this array.
107 public function getItems(ResultRow $values) {
108 $field = $this->getValue($values);
109 if (!empty($this->items[$field])) {
110 return $this->items[$field];