class TermName

Same name in this branch
  1. 9 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName
Same name and namespace in other branches
  1. 11.x core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName
  2. 11.x core/modules/taxonomy/src/Plugin/views/field/TermName.php \Drupal\taxonomy\Plugin\views\field\TermName
  3. 10 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName
  4. 10 core/modules/taxonomy/src/Plugin/views/field/TermName.php \Drupal\taxonomy\Plugin\views\field\TermName
  5. 8.9.x core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName
  6. 8.9.x core/modules/taxonomy/src/Plugin/views/field/TermName.php \Drupal\taxonomy\Plugin\views\field\TermName

Displays taxonomy term names and allows converting spaces to hyphens.

Plugin annotation

@ViewsField("term_name");

Hierarchy

Expanded class hierarchy of TermName

Related topics

File

core/modules/taxonomy/src/Plugin/views/field/TermName.php, line 16

Namespace

Drupal\taxonomy\Plugin\views\field
View source
class TermName extends EntityField {
  
  /**
   * {@inheritdoc}
   */
  public function getItems(ResultRow $values) {
    $items = parent::getItems($values);
    if ($this->options['convert_spaces']) {
      foreach ($items as &$item) {
        // Replace spaces with hyphens.
        $name = str_replace(' ', '-', $item['raw']->get('value')
          ->getValue());
        empty($this->options['settings']['link_to_entity']) ? $item['rendered']['#context']['value'] = $name : $item['rendered']['#title']['#context']['value'] = $name;
      }
    }
    return $items;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['convert_spaces'] = [
      'default' => FALSE,
    ];
    return $options;
  }
  
  /**
   * {@inheritdoc}
   */
  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);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.