View source
<?php
namespace Drupal\other_view_filter\Plugin\views\filter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\views\ViewEntityInterface;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OtherView extends InOperator {
protected $valueFormType = 'select';
protected $viewStorage;
protected $defaultArgs;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $view_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->viewStorage = $view_storage;
}
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')
->getStorage('view'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->defaultArgs = $view->args;
$this->valueTitle = $this
->t('View: display');
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['operator']['default'] = 'not in';
$options['inherit_contextual_filters'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['inherit_contextual_filters'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Inherit contextual filter(s)'),
'#default_value' => $this->options['inherit_contextual_filters'],
'#description' => $this
->t('Use the parent contextual filter(s) for other views result.'),
];
}
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
if (isset($form['value']['#options']['all'])) {
unset($form['value']['#options']['all']);
}
$form['user_warning'] = [
'#markup' => $this
->t('Using more than 1 view in "@input" section will strongly decrease your site performance.', [
'@input' => $this
->t('View: display'),
]),
'#prefix' => '<div class="messages messages--warning">',
'#suffix' => '</div>',
'#weight' => -999,
];
}
public function getValueOptions() {
if (isset($this->valueOptions)) {
return $this->valueOptions;
}
$this->valueOptions = [];
$views = $this->viewStorage
->loadMultiple();
foreach ($views as $view) {
if ($view
->get('base_table') === $this->table && $view
->get('base_field') === $this->realField) {
foreach ($view
->get('display') as $display_id => $display) {
$this->valueOptions[$view
->id() . ':' . $display_id] = $view
->label() . ': ' . $display['display_title'];
}
}
}
return $this->valueOptions;
}
protected function opSimple() {
if (empty($this->value)) {
return;
}
$this
->ensureMyTable();
$results = $this
->getOtherViewsResults($this->value);
if ($results) {
$this->value = $results;
parent::opSimple();
}
elseif ($this->operator === 'in') {
$this->query
->addWhere($this->options['group'], 1, 2, '=');
}
}
protected function getOtherViewsResults(array $views) {
$results = [];
if (empty($views)) {
return $results;
}
foreach ($views as $view_display) {
list($name, $display) = explode(':', $view_display, 2);
if ($name && $display) {
$view = $this->viewStorage
->load($name);
if (!$view instanceof ViewEntityInterface) {
continue;
}
$view = $view
->getExecutable();
if (!$view instanceof ViewExecutable || !$view
->access($display)) {
continue;
}
if ($this->options['inherit_contextual_filters'] && !empty($this->defaultArgs)) {
$view
->setArguments($this->defaultArgs);
}
$view
->setDisplay($display);
$view
->preExecute();
$view
->execute();
if (empty($view->result)) {
continue;
}
foreach ($view->result as $row) {
if (isset($row->{$this->realField})) {
$results[$row->{$this->realField}] = $row->{$this->realField};
}
}
}
}
return $results ? array_values($results) : [];
}
}