View source
<?php
namespace Drupal\elasticsearch_connector_views\Plugin\views\field;
use Drupal\Core\Form\FormHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\Field;
use Drupal\views\Plugin\views\field\MultiItemsFieldHandlerInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
class ElasticsearchViewsEntityField extends Field {
use ElasticsearchViewsFieldTrait;
protected $parentPath;
protected $fallbackHandler;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
$fallback_handler_id = !empty($this->definition['fallback_handler']) ? $this->definition['fallback_handler'] : 'elasticsearch_connector_views_standard';
$this->fallbackHandler = Views::handlerManager('field')
->getHandler($options, $fallback_handler_id);
$options += array(
'fallback_options' => array(),
);
$fallback_options = $options['fallback_options'] + $options;
$this->fallbackHandler
->init($view, $display, $fallback_options);
parent::init($view, $display, $options);
}
public function query() {
if (!$this->options['field_rendering']) {
$this->fallbackHandler
->query();
return;
}
$parent_path = $this
->getParentPath();
$property_path = $parent_path ? "{$parent_path}:_object" : '_object';
$combined_property_path = Utility::createCombinedId($this
->getDatasourceId(), $property_path);
$this
->addRetrievedProperty($combined_property_path);
}
protected function getParentPath() {
if (!isset($this->parentPath)) {
$combined_property_path = $this
->getCombinedPropertyPath();
list(, $property_path) = Utility::splitCombinedId($combined_property_path);
list($this->parentPath) = Utility::splitPropertyPath($property_path);
}
return $this->parentPath;
}
public function defineOptions() {
$options = parent::defineOptions();
$options['field_rendering'] = array(
'default' => TRUE,
);
$options['fallback_handler'] = array(
'default' => $this->fallbackHandler
->getPluginId(),
);
$options['fallback_options'] = array(
'contains' => $this->fallbackHandler
->defineOptions(),
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['field_rendering'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Use entity field rendering'),
'#description' => $this
->t("If checked, Drupal's built-in field rendering mechanism will be used for rendering this field's values, which requires the entity to be loaded. If unchecked, a type-specific, entity-independent rendering mechanism will be used."),
'#default_value' => $this->options['field_rendering'],
);
$form['parent_options'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Render settings'),
'#states' => array(
'visible' => array(
':input[name="options[field_rendering]"]' => array(
'checked' => TRUE,
),
),
),
);
parent::buildOptionsForm($form, $form_state);
$parent_keys = array(
'multiple_field_settings',
'click_sort_column',
'type',
'field_api_classes',
'settings',
);
foreach ($parent_keys as $key) {
if (!empty($form[$key])) {
$form[$key]['#fieldset'] = 'parent_options';
}
}
if (!empty($form['settings'])) {
FormHelper::rewriteStatesSelector($form['settings'], "fields[field_boolean][settings_edit_form]", 'options');
}
$fallback_form = array();
$this->fallbackHandler
->buildOptionsForm($fallback_form, $form_state);
$parent_keys[] = '#pre_render';
$remove_from_fallback = array_diff_key($form, array_flip($parent_keys));
$fallback_form = array_diff_key($fallback_form, $remove_from_fallback);
if ($fallback_form) {
FormHelper::rewriteStatesSelector($fallback_form, '"options[', '"options[fallback_options][');
$form['fallback_options'] = $fallback_form;
$form['fallback_options']['#type'] = 'fieldset';
$form['fallback_options']['#title'] = $this
->t('Render settings');
$form['fallback_options']['#states']['visible'][':input[name="options[field_rendering]"]'] = array(
'checked' => FALSE,
);
}
}
public function preRender(&$values) {
if ($this->options['field_rendering']) {
parent::preRender($values);
}
else {
$this->fallbackHandler
->preRender($values);
}
}
public function render(ResultRow $values) {
if (!$this->options['field_rendering']) {
return $this->fallbackHandler
->render($values);
}
return parent::render($values);
}
public function render_item($count, $item) {
if (!$this->options['field_rendering']) {
if ($this->fallbackHandler instanceof MultiItemsFieldHandlerInterface) {
return $this->fallbackHandler
->render_item($count, $item);
}
return '';
}
return parent::render_item($count, $item);
}
protected function getEntityFieldRenderer() {
if (!isset($this->entityFieldRenderer)) {
if (!empty($this->view->field)) {
foreach ($this->view->field as $field) {
if (isset($field->entityFieldRenderer)) {
if ($field->entityFieldRenderer instanceof EntityFieldRenderer && $field->entityFieldRenderer
->compatibleWithField($this)) {
$this->entityFieldRenderer = $field->entityFieldRenderer;
break;
}
}
}
}
if (!isset($this->entityFieldRenderer)) {
$entity_type = $this->entityManager
->getDefinition($this
->getEntityType());
$this->entityFieldRenderer = new EntityFieldRenderer($this->view, $this->relationship, $this->languageManager, $entity_type, $this->entityManager);
$this->entityFieldRenderer
->setDatasourceId($this
->getDatasourceId());
}
}
return $this->entityFieldRenderer;
}
public function getItems(ResultRow $values) {
return array();
}
public function renderItems($items) {
if (!$this->options['field_rendering']) {
if ($this->fallbackHandler instanceof MultiItemsFieldHandlerInterface) {
return $this->fallbackHandler
->renderItems($items);
}
return '';
}
return parent::renderItems($items);
}
}