Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 20 |
| TermName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 20 |
| getItems | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 9 |
|||
| defineOptions | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
| buildOptionsForm | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 7 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\taxonomy\Plugin\views\field\TermName. | |
| */ | |
| namespace Drupal\taxonomy\Plugin\views\field; | |
| use Drupal\Core\Form\FormStateInterface; | |
| use Drupal\views\Plugin\views\field\Field; | |
| use Drupal\views\ResultRow; | |
| /** | |
| * Displays taxonomy term names and allows converting spaces to hyphens. | |
| * | |
| * @ingroup views_field_handlers | |
| * | |
| * @ViewsField("term_name") | |
| */ | |
| class TermName extends Field { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getItems(ResultRow $values) { | |
| $items = parent::getItems($values); | |
| if ($this->options['convert_spaces']) { | |
| foreach ($items as &$item) { | |
| // Replace spaces with hyphens. | |
| $name = $item['raw']->get('value')->getValue(); | |
| // @todo Add link support https://www.drupal.org/node/2567745 | |
| $item['rendered']['#context']['value'] = str_replace(' ', '-', $name); | |
| } | |
| } | |
| return $items; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| protected function defineOptions() { | |
| $options = parent::defineOptions(); | |
| $options['convert_spaces'] = array('default' => FALSE); | |
| return $options; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| $form['convert_spaces'] = array( | |
| '#title' => $this->t('Convert spaces in term names to hyphens'), | |
| '#type' => 'checkbox', | |
| '#default_value' => !empty($this->options['convert_spaces']), | |
| ); | |
| parent::buildOptionsForm($form, $form_state); | |
| } | |
| } |