3 namespace Drupal\views\Plugin\views\style;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
9 * EntityReference style plugin.
11 * @ingroup views_style_plugins
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"}
22 class EntityReference extends StylePluginBase {
27 protected $usesRowPlugin = TRUE;
32 protected $usesFields = TRUE;
37 protected $usesGrouping = FALSE;
42 protected function defineOptions() {
43 $options = parent::defineOptions();
44 $options['search_fields'] = ['default' => []];
52 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
53 parent::buildOptionsForm($form, $form_state);
55 $options = $this->displayHandler->getFieldLabels(TRUE);
56 $form['search_fields'] = [
57 '#type' => 'checkboxes',
58 '#title' => $this->t('Search fields'),
59 '#options' => $options,
61 '#default_value' => $this->options['search_fields'],
62 '#description' => $this->t('Select the field(s) that will be searched when using the autocomplete widget.'),
70 public function render() {
71 if (!empty($this->view->live_preview)) {
72 return parent::render();
75 // Group the rows according to the grouping field, if specified.
76 $sets = $this->renderGrouping($this->view->result, $this->options['grouping']);
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');
82 // @todo We don't display grouping info for now. Could be useful for select
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)));
100 public function evenEmpty() {