TermName.php in Drupal 10
File
core/modules/taxonomy/src/Plugin/views/field/TermName.php
View source
<?php
namespace Drupal\taxonomy\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\views\ResultRow;
class TermName extends EntityField {
public function getItems(ResultRow $values) {
$items = parent::getItems($values);
if ($this->options['convert_spaces']) {
foreach ($items as &$item) {
$name = $item['raw']
->get('value')
->getValue();
$item['rendered']['#context']['value'] = str_replace(' ', '-', $name);
}
}
return $items;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['convert_spaces'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['convert_spaces'] = [
'#title' => $this
->t('Convert spaces in term names to hyphens'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['convert_spaces']),
];
parent::buildOptionsForm($form, $form_state);
}
}
Classes
Name |
Description |
TermName |
Displays taxonomy term names and allows converting spaces to hyphens. |