View source
<?php
namespace Drupal\synonyms_views_argument_validator\Plugin\views\argument_validator;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\synonyms\SynonymsProviderInterface\SynonymsFindProviderInterface;
use Drupal\synonyms\SynonymsService\BehaviorService;
use Drupal\views\Plugin\views\argument_validator\Entity;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SynonymsEntity extends Entity {
protected $entityTypeManager;
protected $behaviorService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, BehaviorService $behavior_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_type_bundle_info);
$this->entityTypeManager = $entity_type_manager;
$this->behaviorService = $behavior_service;
}
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('entity_type.bundle.info'), $container
->get('synonyms.behaviors'));
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['transform'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Transform dashes in URL to spaces.'),
'#default_value' => $this->options['transform'],
];
}
public function validateArgument($argument) {
if ($this->options['transform']) {
$argument = str_replace('-', ' ', $argument);
}
$entity_type = $this->entityTypeManager
->getDefinition($this->definition['entity_type']);
if ($entity_type
->hasKey('label') || $entity_type
->id() == 'user') {
$query = $this->entityTypeManager
->getStorage($entity_type
->id())
->getQuery();
$label_column = $entity_type
->id() == 'user' ? 'name' : $entity_type
->getKey('label');
$query
->condition($label_column, $argument, '=');
if ($entity_type
->hasKey('bundle') && !empty($this->options['bundles'])) {
$query
->condition($entity_type
->getKey('bundle'), $this->options['bundles'], 'IN');
}
$result = $query
->execute();
if (!empty($result)) {
$entities = $this->entityTypeManager
->getStorage($entity_type
->id())
->loadMultiple($result);
foreach ($entities as $entity) {
if ($this
->validateEntity($entity)) {
$this->argument->argument = $entity
->id();
return TRUE;
}
}
}
}
$condition = new Condition('AND');
$condition
->condition(SynonymsFindProviderInterface::COLUMN_SYNONYM_PLACEHOLDER, $argument, '=');
foreach ($this->behaviorService
->getSynonymConfigEntities('synonyms.behavior.autocomplete', $entity_type
->id(), empty($this->options['bundles']) ? NULL : $this->options['bundles']) as $synonym_config) {
foreach ($synonym_config
->getProviderPluginInstance()
->synonymsFind(clone $condition) as $synonym) {
$entity = $this->entityTypeManager
->getStorage($entity_type
->id())
->load($synonym->entity_id);
if ($this
->validateEntity($entity)) {
$this->argument->argument = $entity
->id();
return TRUE;
}
}
}
return FALSE;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['transform'] = [
'default' => FALSE,
];
return $options;
}
}