EntityReference.php 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Plugin\views\style\EntityReference.
 */

namespace Drupal\entity_reference\Plugin\views\style;

10
use Drupal\Component\Utility\Xss;
11
use Drupal\Core\Form\FormStateInterface;
12 13 14 15 16 17 18
use Drupal\views\Plugin\views\style\StylePluginBase;

/**
 * EntityReference style plugin.
 *
 * @ingroup views_style_plugins
 *
19
 * @ViewsStyle(
20 21 22 23
 *   id = "entity_reference",
 *   title = @Translation("Entity Reference list"),
 *   help = @Translation("Returns results as a PHP array of labels and rendered rows."),
 *   theme = "views_view_unformatted",
24
 *   register_theme = FALSE,
25
 *   display_types = {"entity_reference"}
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 * )
 */
class EntityReference extends StylePluginBase {

  /**
   * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesRowPlugin.
   */
  protected $usesRowPlugin = TRUE;

  /**
   * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesFields.
   */
  protected $usesFields = TRUE;

  /**
   * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesGrouping.
   */
  protected $usesGrouping = FALSE;

  /**
   * Overrides \Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::defineOptions().
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
50
    $options['search_fields'] = array('default' => array());
51 52 53 54 55 56 57

    return $options;
  }

  /**
   * Overrides \Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::buildOptionsForm().
   */
58
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
59 60 61 62 63
    parent::buildOptionsForm($form, $form_state);

    $options = $this->displayHandler->getFieldLabels(TRUE);
    $form['search_fields'] = array(
      '#type' => 'checkboxes',
64
      '#title' => $this->t('Search fields'),
65 66 67
      '#options' => $options,
      '#required' => TRUE,
      '#default_value' => $this->options['search_fields'],
68
      '#description' => $this->t('Select the field(s) that will be searched when using the autocomplete widget.'),
69 70 71 72 73 74 75 76 77 78 79 80 81
      '#weight' => -3,
    );
  }

  /**
   * Overrides \Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::render().
   */
  public function render() {
    if (!empty($this->view->live_preview)) {
      return parent::render();
    }

    // Group the rows according to the grouping field, if specified.
82
    $sets = $this->renderGrouping($this->view->result, $this->options['grouping']);
83 84 85 86 87 88 89 90 91 92

    // Grab the alias of the 'id' field added by
    // entity_reference_plugin_display.
    $id_field_alias = $this->view->storage->get('base_field');

    // @todo We don't display grouping info for now. Could be useful for select
    // widget, though.
    $results = array();
    foreach ($sets as $records) {
      foreach ($records as $values) {
93
        $results[$values->{$id_field_alias}] = $this->view->rowPlugin->render($values);
94
        // Sanitize HTML, remove line breaks and extra whitespace.
95 96 97
        $results[$values->{$id_field_alias}]['#post_render'][] = function ($html, array $elements) {
          return Xss::filterAdmin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $html)));
        };
98 99 100 101 102 103
      }
    }
    return $results;
  }

  /**
104
   * {@inheritdoc}
105
   */
106
  public function evenEmpty() {
107 108 109
    return TRUE;
  }
}