View source
<?php
namespace Drupal\context\Plugin\Condition;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewInclusion extends ConditionPluginBase implements ContainerFactoryPluginInterface {
private $entityTypeManager;
private $currentRouteMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, CurrentRouteMatch $currentRouteMatch) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->currentRouteMatch = $currentRouteMatch;
}
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('current_route_match'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$views = $this->entityTypeManager
->getStorage('view')
->loadMultiple();
$options = [];
foreach ($views as $key => $view) {
foreach ($view
->get('display') as $display) {
if ($display['display_plugin'] === 'page') {
$viewRoute = 'view-' . $key . '-' . $display['id'];
$options[$viewRoute] = $view
->label() . ' - ' . $display['display_title'];
}
}
}
$configuration = $this
->getConfiguration();
$form['views_pages'] = [
'#title' => $this
->t('Views pages'),
'#type' => 'select',
'#options' => $options,
'#multiple' => TRUE,
'#default_value' => isset($configuration['view_inclusion']) && !empty($configuration['view_inclusion']) ? array_keys($configuration['view_inclusion']) : [],
];
$form = parent::buildConfigurationForm($form, $form_state);
$form['negate']['#access'] = FALSE;
return $form;
}
public function defaultConfiguration() {
return [
'view_inclusion' => [],
] + parent::defaultConfiguration();
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['view_inclusion'] = array_filter($form_state
->getValue('views_pages'));
$this->configuration['negate'] = FALSE;
}
public function summary() {
if (empty($this->configuration['view_inclusion'])) {
return $this
->t('No views page is selected.');
}
return $this
->t('Return true on the following views pages: @pages', [
'@pages' => str_replace('-', '.', implode(', ', $this->configuration['view_inclusion'])),
]);
}
public function evaluate() {
if (empty($this->configuration['view_inclusion'])) {
return TRUE;
}
$route = str_replace('.', '-', $this->currentRouteMatch
->getRouteName());
return in_array($route, $this->configuration['view_inclusion'], TRUE);
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'url.path';
return $contexts;
}
}