You are here

class TermName in Drupal 10

Same name in this branch
  1. 10 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName
  2. 10 core/modules/taxonomy/src/Plugin/views/field/TermName.php \Drupal\taxonomy\Plugin\views\field\TermName
Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName
  2. 9 core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php \Drupal\taxonomy\Plugin\views\argument_validator\TermName

Validates whether a term name is a valid term argument.

Plugin annotation


@ViewsArgumentValidator(
  id = "taxonomy_term_name",
  title = @Translation("Taxonomy term name"),
  entity_type = "taxonomy_term"
)

Hierarchy

  • class \Drupal\views\Plugin\views\argument_validator\Entity extends \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase
    • class \Drupal\taxonomy\Plugin\views\argument_validator\TermName

Expanded class hierarchy of TermName

File

core/modules/taxonomy/src/Plugin/views/argument_validator/TermName.php, line 19

Namespace

Drupal\taxonomy\Plugin\views\argument_validator
View source
class TermName extends Entity {

  /**
   * The taxonomy term storage.
   *
   * @var \Drupal\taxonomy\TermStorageInterface
   */
  protected $termStorage;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_type_bundle_info);

    // Not handling exploding term names.
    $this->multipleCapable = FALSE;
    $this->termStorage = $entity_type_manager
      ->getStorage('taxonomy_term');
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['transform'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['transform'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Transform dashes in URL to spaces in term name filter values'),
      '#default_value' => $this->options['transform'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validateArgument($argument) {
    if ($this->options['transform']) {
      $argument = str_replace('-', ' ', $argument);
      $this->argument->argument = $argument;
    }

    // If bundles is set then restrict the loaded terms to the given bundles.
    if (!empty($this->options['bundles'])) {
      $terms = $this->termStorage
        ->loadByProperties([
        'name' => $argument,
        'vid' => $this->options['bundles'],
      ]);
    }
    else {
      $terms = $this->termStorage
        ->loadByProperties([
        'name' => $argument,
      ]);
    }

    // $terms are already bundle tested but we need to test access control.
    foreach ($terms as $term) {
      if ($this
        ->validateEntity($term)) {
        return TRUE;
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$entityTypeBundleInfo protected property The entity bundle info.
Entity::$entityTypeManager protected property The entity type manager.
Entity::$multipleCapable protected property If this validator can handle multiple arguments.
Entity::calculateDependencies public function 1
Entity::create public static function
Entity::getContextDefinition public function
Entity::submitOptionsForm public function 1
Entity::validateEntity protected function Validates an individual entity against class access settings. 1
TermName::$termStorage protected property The taxonomy term storage.
TermName::buildOptionsForm public function Overrides Entity::buildOptionsForm
TermName::defineOptions protected function Overrides Entity::defineOptions
TermName::validateArgument public function Overrides Entity::validateArgument
TermName::__construct public function Constructs a \Drupal\views\Plugin\views\argument_validator\Entity object. Overrides Entity::__construct