EntityBrowser.php in Entity Browser 8
File
src/Plugin/views/display/EntityBrowser.php
View source
<?php
namespace Drupal\entity_browser\Plugin\views\display;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
class EntityBrowser extends DisplayPluginBase {
public function execute() {
parent::execute();
$render = [
'view' => $this->view
->render(),
];
$this
->handleForm($render);
return $render;
}
public function ajaxEnabled() {
return TRUE;
}
public function getOption($option) {
if ($option == 'use_ajax') {
return TRUE;
}
else {
return parent::getOption($option);
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['use_ajax']['default'] = TRUE;
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
if (isset($options['use_ajax'])) {
$options['use_ajax']['value'] = $this
->t('Yes (Forced)');
}
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if (isset($form['use_ajax'])) {
$form['use_ajax'] = [
'#description' => $this
->t('Entity Browser requires Views to use AJAX.'),
'#type' => 'checkbox',
'#title' => $this
->t('Use AJAX'),
'#default_value' => 1,
'#disabled' => TRUE,
];
}
}
public function preview() {
return $this
->execute();
}
public function elementPreRender(array $element) {
$view = $element['#view'];
$empty = empty($view->result);
if (!is_array($element['#rows'])) {
$element['#rows'] = [
'#markup' => $element['#rows'],
];
}
$element['#header'] = $view->display_handler
->renderArea('header', $empty);
$element['#footer'] = $view->display_handler
->renderArea('footer', $empty);
$element['#empty'] = $empty ? $view->display_handler
->renderArea('empty', $empty) : [];
$element['#exposed'] = !empty($view->exposed_widgets) ? $view->exposed_widgets : [];
$element['#more'] = $view->display_handler
->renderMoreLink();
$element['#feed_icons'] = !empty($view->feedIcons) ? $view->feedIcons : [];
if ($view->display_handler
->renderPager()) {
$exposed_input = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
$element['#pager'] = $view
->renderPager($exposed_input);
}
if (!empty($view->attachment_before)) {
$element['#attachment_before'] = $view->attachment_before;
}
if (!empty($view->attachment_after)) {
$element['#attachment_after'] = $view->attachment_after;
}
return $element;
}
protected function handleForm(array &$render) {
if (!empty($this->view->field['entity_browser_select'])) {
$select = $this->view->field['entity_browser_select'];
$select
->viewsForm($render);
$render['#post_render'][] = [
get_class($this),
'postRender',
];
$substitutions = [];
foreach ($this->view->result as $row) {
$form_element_row_id = $select
->getRowId($row);
$substitutions[] = [
'placeholder' => '<!--form-item-entity_browser_select--' . $form_element_row_id . '-->',
'field_name' => 'entity_browser_select',
'row_id' => $form_element_row_id,
];
}
$render['#substitutions'] = [
'#type' => 'value',
'#value' => $substitutions,
];
}
}
public static function postRender($content, array $element) {
$search = $replace = [];
foreach ($element['#substitutions']['#value'] as $substitution) {
$field_name = $substitution['field_name'];
$row_id = $substitution['row_id'];
$search[] = $substitution['placeholder'];
$replace[] = isset($element[$field_name][$row_id]) ? \Drupal::service('renderer')
->render($element[$field_name][$row_id]) : '';
}
$substitutions = \Drupal::moduleHandler()
->invokeAll('views_form_substitutions');
foreach ($substitutions as $placeholder => $substitution) {
$search[] = $placeholder;
$replace[] = $substitution;
}
$search[] = '<form';
$replace[] = '<div';
$search[] = '</form>';
$replace[] = '</div>';
$content = str_replace($search, $replace, $content);
return $content;
}
}