EntityReferenceRevisions.php in Entity Reference Revisions 8
File
src/Plugin/views/display/EntityReferenceRevisions.php
View source
<?php
namespace Drupal\entity_reference_revisions\Plugin\views\display;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
class EntityReferenceRevisions extends DisplayPluginBase {
protected $usesAJAX = FALSE;
protected $usesPager = FALSE;
protected $usesAttachments = FALSE;
protected function defineOptions() {
$options = parent::defineOptions();
$options['style']['contains']['type'] = array(
'default' => 'entity_reference_revisions',
);
$options['defaults']['default']['style'] = FALSE;
$options['row']['contains']['type'] = array(
'default' => 'entity_reference_revisions',
);
$options['defaults']['default']['row'] = FALSE;
$options['defaults']['default']['cache'] = FALSE;
$options['title']['default'] = '';
$options['defaults']['default']['title'] = FALSE;
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
unset($options['query']);
unset($options['title']);
}
public function getType() {
return 'entity_reference_revisions';
}
public function execute() {
return $this->view
->render($this->display['id']);
}
public function render() {
if (!empty($this->view->result) && $this->view->style_plugin
->evenEmpty()) {
return $this->view->style_plugin
->render($this->view->result);
}
return '';
}
public function usesExposed() {
return FALSE;
}
public function query() {
if (!empty($this->view->live_preview)) {
return;
}
$id_field = $this->view->storage
->get('base_field');
$this->id_field_alias = $this->view->query
->addField($this->view->storage
->get('base_table'), $id_field);
$options = $this
->getOption('entity_reference_revisions_options');
if (isset($options['match'])) {
$style_options = $this
->getOption('style');
$value = \Drupal::database()
->escapeLike($options['match']) . '%';
if ($options['match_operator'] != 'STARTS_WITH') {
$value = '%' . $value;
}
$conditions = new Condition('OR');
foreach ($style_options['options']['search_fields'] as $field_alias) {
if (!empty($field_alias)) {
$field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
$conditions
->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
}
}
$this->view->query
->addWhere(0, $conditions);
}
if (!empty($options['ids'])) {
$this->view->query
->addWhere(0, $id_field, $options['ids']);
}
$this->view
->setItemsPerPage($options['limit']);
}
public function validate() {
$errors = parent::validate();
$style = $this
->getOption('style');
if (!isset($style['options']['search_fields'])) {
$errors[] = $this
->t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference Revisions list format.', array(
'@display' => $this->display['display_title'],
));
}
else {
$fields = array_keys($this->handlers['field']);
foreach ($style['options']['search_fields'] as $field_alias => $enabled) {
if ($enabled && !in_array($field_alias, $fields)) {
$errors[] = $this
->t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference Revisions list format.', array(
'@display' => $this->display['display_title'],
'%field' => $field_alias,
));
}
}
}
return $errors;
}
}