View source
<?php
namespace Drupal\verf\Plugin\views\filter;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\views\Plugin\views\filter\InOperator;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityReference extends InOperator implements ContainerFactoryPluginInterface {
protected $entityTypeBundleInfo;
protected $languageManager;
protected $referenceableEntities;
protected $targetEntityStorage;
protected $targetEntityType;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, LanguageManagerInterface $language_manager, EntityStorageInterface $target_entity_storage, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeInterface $target_entity_type) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->targetEntityStorage = $target_entity_storage;
$this->targetEntityType = $target_entity_type;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('language_manager'), $entity_type_manager
->getStorage($configuration['verf_target_entity_type_id']), $container
->get('entity_type.bundle.info'), $entity_type_manager
->getDefinition($configuration['verf_target_entity_type_id']));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['verf_target_bundles'] = [
'default' => [],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if (!$this->targetEntityType
->hasKey('bundle')) {
return $form;
}
$options = [];
$bundleInfo = $this->entityTypeBundleInfo
->getBundleInfo($this->targetEntityType
->id());
foreach ($bundleInfo as $id => $info) {
$options[$id] = $info['label'];
}
$form['verf_target_bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Target entity bundles to filter by'),
'#options' => $options,
'#default_value' => array_filter($this->options['verf_target_bundles']),
];
return $form;
}
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
$cacheability_metdata = CacheableMetadata::createFromObject($this);
$cacheability_metdata
->applyTo($form);
return $form;
}
public function getValueOptions() {
if ($this->valueOptions !== NULL) {
return $this->valueOptions;
}
$this->valueOptions = [];
foreach ($this
->getReferenceableEntities() as $entity) {
$current_content_language_id = $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
if ($entity instanceof TranslatableInterface && $entity
->hasTranslation($current_content_language_id)) {
$entity = $entity
->getTranslation($current_content_language_id);
}
if (!$entity
->access('view label')) {
$this->valueOptions[$entity
->id()] = new TranslatableMarkup('- Restricted access -');
continue;
}
$this->valueOptions[$entity
->id()] = $entity
->label();
}
natcasesort($this->valueOptions);
return $this->valueOptions;
}
protected function getReferenceableEntities() {
if ($this->referenceableEntities !== NULL) {
return $this->referenceableEntities;
}
$target_ids = NULL;
$target_bundles = array_filter($this->options['verf_target_bundles']);
if ($this->targetEntityType
->hasKey('bundle') && $target_bundles) {
$query = $this->targetEntityStorage
->getQuery();
$query
->condition($this->targetEntityType
->getKey('bundle'), $target_bundles, 'IN');
$target_ids = $query
->execute();
}
$this->referenceableEntities = $this->targetEntityStorage
->loadMultiple($target_ids);
return $this->referenceableEntities;
}
public function getCacheTags() {
$cache_tags = Cache::mergeTags(parent::getCacheTags(), $this->view->storage
->getCacheTags());
$cache_tags = Cache::mergeTags($cache_tags, $this->targetEntityType
->getListCacheTags());
return $cache_tags;
}
public function getCacheContexts() {
$cache_contexts = Cache::mergeContexts(parent::getCacheContexts(), $this->view->storage
->getCacheContexts());
$cache_contexts = Cache::mergeContexts($cache_contexts, $this->targetEntityType
->getListCacheContexts());
return $cache_contexts;
}
public function getCacheMaxAge() {
$cache_max_age = Cache::mergeMaxAges(parent::getCacheMaxAge(), $this->view->storage
->getCacheMaxAge());
return $cache_max_age;
}
}