SynonymsEntity.php in Synonyms 2.0.x
File
modules/synonyms_views_argument_validator/src/Plugin/views/argument_validator/SynonymsEntity.php
View source
<?php
namespace Drupal\synonyms_views_argument_validator\Plugin\views\argument_validator;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\synonyms\ProviderInterface\FindInterface;
use Drupal\views\Plugin\views\argument_validator\Entity;
class SynonymsEntity extends Entity {
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(FindInterface::COLUMN_SYNONYM_PLACEHOLDER, $argument, '=');
foreach (\Drupal::service('synonyms.provider_service')
->findSynonyms($condition, $entity_type, empty($this->options['bundles']) ? NULL : $this->options['bundles']) 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;
}
}