3 namespace Drupal\taxonomy\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\EntityField;
7 use Drupal\views\ResultRow;
10 * Displays taxonomy term names and allows converting spaces to hyphens.
12 * @ingroup views_field_handlers
14 * @ViewsField("term_name")
16 class TermName extends EntityField {
21 public function getItems(ResultRow $values) {
22 $items = parent::getItems($values);
23 if ($this->options['convert_spaces']) {
24 foreach ($items as &$item) {
25 // Replace spaces with hyphens.
26 $name = $item['raw']->get('value')->getValue();
27 // @todo Add link support https://www.drupal.org/node/2567745
28 $item['rendered']['#context']['value'] = str_replace(' ', '-', $name);
38 protected function defineOptions() {
39 $options = parent::defineOptions();
40 $options['convert_spaces'] = ['default' => FALSE];
47 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
48 $form['convert_spaces'] = [
49 '#title' => $this->t('Convert spaces in term names to hyphens'),
50 '#type' => 'checkbox',
51 '#default_value' => !empty($this->options['convert_spaces']),
54 parent::buildOptionsForm($form, $form_state);