View source
<?php
namespace Drupal\entity_browser\Plugin\views\filter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\views\Plugin\views\filter\Bundle;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
class ContextualBundle extends Bundle {
protected $entityTypeManager;
protected $requestStack;
protected $selectionStorage;
protected $bundleInfoService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack, KeyValueStoreExpirableInterface $selection_storage, EntityTypeBundleInfoInterface $bundle_info_service) {
HandlerBase::__construct($configuration, $plugin_id, $plugin_definition);
$this->requestStack = $request_stack;
$this->entityTypeManager = $entity_type_manager;
$this->selectionStorage = $selection_storage;
$this->is_handler = TRUE;
$this->bundleInfoService = $bundle_info_service;
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->entityTypeId = $this
->getEntityType();
$this->entityType = $this->entityTypeManager
->getDefinition($this->entityTypeId);
$this->real_field = $this->entityType
->getKey('bundle');
$current_request = $this->requestStack
->getCurrentRequest();
if ($current_request->query
->has('uuid')) {
$uuid = $current_request->query
->get('uuid');
if ($storage = $this->selectionStorage
->get($uuid)) {
if (isset($storage['widget_context']) && !empty($storage['widget_context']['target_bundles'])) {
$this->value = $storage['widget_context']['target_bundles'];
}
}
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('request_stack'), $container
->get('entity_browser.selection_storage'), $container
->get('entity_type.bundle.info'));
}
protected function operatorForm(&$form, FormStateInterface $form_state) {
$form['operator'] = [
'#type' => 'hidden',
'#value' => 'in',
];
return $form;
}
public function query() {
if (empty($this->value)) {
return;
}
$this
->ensureMyTable();
$this->query
->addWhere($this->options['group'], "{$this->tableAlias}.{$this->realField}", array_values($this->value), 'in');
}
public function canExpose() {
return TRUE;
}
public function adminSummary() {
return NULL;
}
public function calculateDependencies() {
$dependencies = HandlerBase::calculateDependencies();
return $dependencies;
}
public function getValueOptions() {
$this->valueOptions = parent::getValueOptions();
if ($this->requestStack
->getCurrentRequest()->attributes
->get('_route') == 'views_ui.form_handler') {
return $this->valueOptions;
}
foreach ($this->valueOptions as $key => $value) {
if (!in_array($key, $this->value)) {
unset($this->valueOptions[$key]);
}
}
return $this->valueOptions;
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
$identifier = $this->options['expose']['identifier'];
if (!empty($input[$identifier]) && $input[$identifier] == 'All') {
return TRUE;
}
return parent::acceptExposedInput($input);
}
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
if ($this->requestStack
->getCurrentRequest()->attributes
->get('_route') == 'views_ui.form_handler') {
$form['value']['#default_value'] = array_combine(array_keys($form['value']['#options']), array_keys($form['value']['#options']));
$form['value']['#disabled'] = TRUE;
$form['value']['#description'] = $this
->t('You cannot edit this list because the options update in response to entity browser context.');
}
elseif (!empty($this->value) && count($this->value) === 1) {
$form['value'] = [
'#type' => 'hidden',
'#value' => array_values($this->value)[0],
];
}
}
}