Backup of db before drupal security update
[yaffs-website] / web / core / modules / views / src / Plugin / views / style / EntityReference.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\style;
4
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * EntityReference style plugin.
10  *
11  * @ingroup views_style_plugins
12  *
13  * @ViewsStyle(
14  *   id = "entity_reference",
15  *   title = @Translation("Entity Reference list"),
16  *   help = @Translation("Returns results as a PHP array of labels and rendered rows."),
17  *   theme = "views_view_unformatted",
18  *   register_theme = FALSE,
19  *   display_types = {"entity_reference"}
20  * )
21  */
22 class EntityReference extends StylePluginBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   protected $usesRowPlugin = TRUE;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected $usesFields = TRUE;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected $usesGrouping = FALSE;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function defineOptions() {
43     $options = parent::defineOptions();
44     $options['search_fields'] = ['default' => []];
45
46     return $options;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
53     parent::buildOptionsForm($form, $form_state);
54
55     $options = $this->displayHandler->getFieldLabels(TRUE);
56     $form['search_fields'] = [
57       '#type' => 'checkboxes',
58       '#title' => $this->t('Search fields'),
59       '#options' => $options,
60       '#required' => TRUE,
61       '#default_value' => $this->options['search_fields'],
62       '#description' => $this->t('Select the field(s) that will be searched when using the autocomplete widget.'),
63       '#weight' => -3,
64     ];
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function render() {
71     if (!empty($this->view->live_preview)) {
72       return parent::render();
73     }
74
75     // Group the rows according to the grouping field, if specified.
76     $sets = $this->renderGrouping($this->view->result, $this->options['grouping']);
77
78     // Grab the alias of the 'id' field added by
79     // entity_reference_plugin_display.
80     $id_field_alias = $this->view->storage->get('base_field');
81
82     // @todo We don't display grouping info for now. Could be useful for select
83     // widget, though.
84     $results = [];
85     foreach ($sets as $records) {
86       foreach ($records as $values) {
87         $results[$values->{$id_field_alias}] = $this->view->rowPlugin->render($values);
88         // Sanitize HTML, remove line breaks and extra whitespace.
89         $results[$values->{$id_field_alias}]['#post_render'][] = function ($html, array $elements) {
90           return Xss::filterAdmin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $html)));
91         };
92       }
93     }
94     return $results;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function evenEmpty() {
101     return TRUE;
102   }
103
104 }