0a777e9c1cfe0a79ca4051e4c8087a85329e4288
[yaffs-website] / web / core / modules / taxonomy / src / Plugin / views / field / TermName.php
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\EntityField;
7 use Drupal\views\ResultRow;
8
9 /**
10  * Displays taxonomy term names and allows converting spaces to hyphens.
11  *
12  * @ingroup views_field_handlers
13  *
14  * @ViewsField("term_name")
15  */
16 class TermName extends EntityField {
17
18   /**
19    * {@inheritdoc}
20    */
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);
29       }
30     }
31     return $items;
32   }
33
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function defineOptions() {
39     $options = parent::defineOptions();
40     $options['convert_spaces'] = ['default' => FALSE];
41     return $options;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
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']),
52     ];
53
54     parent::buildOptionsForm($form, $form_state);
55   }
56
57 }