TermName.php in Drupal 10
File
core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php
View source
<?php
namespace Drupal\taxonomy\Plugin\views\argument_validator;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\argument_validator\Entity;
class TermName extends Entity {
protected $termStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_type_bundle_info);
$this->multipleCapable = FALSE;
$this->termStorage = $entity_type_manager
->getStorage('taxonomy_term');
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['transform'] = [
'default' => FALSE,
];
return $options;
}
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 in term name filter values'),
'#default_value' => $this->options['transform'],
];
}
public function validateArgument($argument) {
if ($this->options['transform']) {
$argument = str_replace('-', ' ', $argument);
$this->argument->argument = $argument;
}
if (!empty($this->options['bundles'])) {
$terms = $this->termStorage
->loadByProperties([
'name' => $argument,
'vid' => $this->options['bundles'],
]);
}
else {
$terms = $this->termStorage
->loadByProperties([
'name' => $argument,
]);
}
foreach ($terms as $term) {
if ($this
->validateEntity($term)) {
return TRUE;
}
}
return FALSE;
}
}
Classes
Name |
Description |
TermName |
Validates whether a term name is a valid term argument. |