eefc3e3d3fe19fae6c1d0122cdea343eca2f99d3
[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    * {@inheritdoc}
36    */
37   protected function defineOptions() {
38     $options = parent::defineOptions();
39     $options['convert_spaces'] = ['default' => FALSE];
40     return $options;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
47     $form['convert_spaces'] = [
48       '#title' => $this->t('Convert spaces in term names to hyphens'),
49       '#type' => 'checkbox',
50       '#default_value' => !empty($this->options['convert_spaces']),
51     ];
52
53     parent::buildOptionsForm($form, $form_state);
54   }
55
56 }