TermMachineName.php in Taxonomy Machine Name 8
File
src/Plugin/views/argument_validator/TermMachineName.php
View source
<?php
namespace Drupal\taxonomy_machine_name\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 TermMachineName extends Entity {
protected $termStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager, $entity_type_bundle_info);
$this->multipleCapable = FALSE;
$this->termStorage = $entity_manager
->getStorage('taxonomy_term');
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['transform'] = [
'default' => TRUE,
];
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 underscores in term name filter values'),
'#default_value' => $this->options['transform'],
];
}
public function validateArgument($argument) {
if ($this->options['transform']) {
$argument = str_replace('-', '_', $argument);
}
$properties = [
'machine_name' => $argument,
];
if ($bundles = array_filter($this->options['bundles'])) {
$properties['vid'] = $bundles;
}
$terms = $this->termStorage
->loadByProperties($properties);
if (!$terms) {
return FALSE;
}
foreach ($terms as $term) {
if (!$this
->validateEntity($term)) {
return FALSE;
}
}
$this->argument->argument = $term
->id();
if (!($this->argument->validated_title = $term
->getName())) {
$this->argument->validated_title = $this
->t('No name');
}
return TRUE;
}
}
Classes
Name |
Description |
TermMachineName |
Validates whether a term machine name is a valid term argument. |