f851cbb6a005ed59d1382efc0280213d80cb847d
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / PrerenderList.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
7
8 /**
9  * Field handler to provide a list of items.
10  *
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.
13  *
14  * Items to render should be in a list in $this->items
15  *
16  * @ingroup views_field_handlers
17  */
18 abstract class PrerenderList extends FieldPluginBase implements MultiItemsFieldHandlerInterface {
19
20   /**
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.
25    *
26    * @var array
27    */
28   public $items = [];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function defineOptions() {
34     $options = parent::defineOptions();
35
36     $options['type'] = ['default' => 'separator'];
37     $options['separator'] = ['default' => ', '];
38
39     return $options;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
46     $form['type'] = [
47       '#type' => 'radios',
48       '#title' => $this->t('Display type'),
49       '#options' => [
50         'ul' => $this->t('Unordered list'),
51         'ol' => $this->t('Ordered list'),
52         'separator' => $this->t('Simple separator'),
53       ],
54       '#default_value' => $this->options['type'],
55     ];
56
57     $form['separator'] = [
58       '#type' => 'textfield',
59       '#title' => $this->t('Separator'),
60       '#default_value' => $this->options['separator'],
61       '#states' => [
62         'visible' => [
63           ':input[name="options[type]"]' => ['value' => 'separator'],
64         ],
65       ],
66     ];
67     parent::buildOptionsForm($form, $form_state);
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function renderItems($items) {
74     if (!empty($items)) {
75       if ($this->options['type'] == 'separator') {
76         $render = [
77           '#type' => 'inline_template',
78           '#template' => '{{ items|safe_join(separator) }}',
79           '#context' => [
80             'items' => $items,
81             'separator' => $this->sanitizeValue($this->options['separator'], 'xss_admin')
82           ]
83         ];
84       }
85       else {
86         $render = [
87           '#theme' => 'item_list',
88           '#items' => $items,
89           '#title' => NULL,
90           '#list_type' => $this->options['type'],
91         ];
92       }
93       return drupal_render($render);
94     }
95   }
96
97   /**
98    * {@inheritdoc}
99    *
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.
106    */
107   public function getItems(ResultRow $values) {
108     $field = $this->getValue($values);
109     if (!empty($this->items[$field])) {
110       return $this->items[$field];
111     }
112
113     return [];
114   }
115
116 }