You are here

class Term in Views (for Drupal 7) 8.3

Validate whether an argument is an acceptable node.

Plugin annotation


@Plugin(
  id = "taxonomy_term",
  module = "taxonomy",
  title = @Translation("Taxonomy term")
  )

Hierarchy

Expanded class hierarchy of Term

1 string reference to 'Term'
taxonomy_views_data in modules/taxonomy.views.inc
Implements hook_views_data().

File

lib/Views/taxonomy/Plugin/views/argument_validator/Term.php, line 24
Definition of Views\taxonomy\Plugin\views\argument_validator\Term.

Namespace

Views\taxonomy\Plugin\views\argument_validator
View source
class Term extends ArgumentValidatorPluginBase {
  public function init(ViewExecutable $view, &$argument, $options) {
    parent::init($view, $argument, $options);

    // Convert legacy vids option to machine name vocabularies.
    if (!empty($this->options['vids'])) {
      $vocabularies = taxonomy_vocabulary_get_names();
      foreach ($this->options['vids'] as $vid) {
        if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
          $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
        }
      }
    }
  }
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['vocabularies'] = array(
      'default' => array(),
    );
    $options['type'] = array(
      'default' => 'tid',
    );
    $options['transform'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    return $options;
  }
  public function buildOptionsForm(&$form, &$form_state) {
    $vocabularies = taxonomy_vocabulary_get_names();
    $options = array();
    foreach ($vocabularies as $voc) {
      $options[$voc->machine_name] = check_plain($voc->name);
    }
    $form['vocabularies'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
      '#suffix' => '</div>',
      '#title' => t('Vocabularies'),
      '#options' => $options,
      '#default_value' => $this->options['vocabularies'],
      '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
    );
    $form['type'] = array(
      '#type' => 'select',
      '#title' => t('Filter value type'),
      '#options' => array(
        'tid' => t('Term ID'),
        'tids' => t('Term IDs separated by , or +'),
        'name' => t('Term name'),
        'convert' => t('Term name converted to Term ID'),
      ),
      '#default_value' => $this->options['type'],
      '#description' => t('Select the form of this filter value; if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name" as the filter.'),
    );
    $form['transform'] = array(
      '#type' => 'checkbox',
      '#title' => t('Transform dashes in URL to spaces in term name filter values'),
      '#default_value' => $this->options['transform'],
    );
  }
  public function submitOptionsForm(&$form, &$form_state, &$options = array()) {

    // Filter unselected items so we don't unnecessarily store giant arrays.
    $options['vocabularies'] = array_filter($options['vocabularies']);
  }
  function validate_argument($argument) {
    $vocabularies = array_filter($this->options['vocabularies']);
    $type = $this->options['type'];
    $transform = $this->options['transform'];
    switch ($type) {
      case 'tid':
        if (!is_numeric($argument)) {
          return FALSE;
        }

        // @todo Deal with missing addTag('term access') that was removed when
        // the db_select that was replaced by the entity_load.
        $term = entity_load('taxonomy_term', $argument);
        if (!$term) {
          return FALSE;
        }
        $this->argument->validated_title = check_plain($term->name);
        return empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]);
      case 'tids':

        // An empty argument is not a term so doesn't pass.
        if (empty($argument)) {
          return FALSE;
        }
        $tids = new stdClass();
        $tids->value = $argument;
        $tids = $this
          ->breakPhrase($argument, $tids);
        if ($tids->value == array(
          -1,
        )) {
          return FALSE;
        }
        $test = drupal_map_assoc($tids->value);
        $titles = array();

        // check, if some tids already verified
        static $validated_cache = array();
        foreach ($test as $tid) {
          if (isset($validated_cache[$tid])) {
            if ($validated_cache[$tid] === FALSE) {
              return FALSE;
            }
            else {
              $titles[] = $validated_cache[$tid];
              unset($test[$tid]);
            }
          }
        }

        // if unverified tids left - verify them and cache results
        if (count($test)) {
          $result = entity_load_multiple('taxonomy_term', $test);
          foreach ($result as $term) {
            if ($vocabularies && empty($vocabularies[$term->vocabulary_machine_name])) {
              $validated_cache[$term
                ->id()] = FALSE;
              return FALSE;
            }
            $titles[] = $validated_cache[$term
              ->id()] = check_plain($term->name);
            unset($test[$term
              ->id()]);
          }
        }

        // Remove duplicate titles
        $titles = array_unique($titles);
        $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);

        // If this is not empty, we did not find a tid.
        return empty($test);
      case 'name':
      case 'convert':
        $terms = entity_load_multiple_by_properties('taxonomy_term', array(
          'name' => $argument,
        ));
        $term = reset($terms);
        if ($transform) {
          $term->name = str_replace(' ', '-', $term->name);
        }
        if ($term && (empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]))) {
          if ($type == 'convert') {
            $this->argument->argument = $term
              ->id();
          }
          $this->argument->validated_title = check_plain($term->name);
          return TRUE;
        }
        return FALSE;
    }
  }
  function process_summary_arguments(&$args) {
    $type = $this->options['type'];
    $transform = $this->options['transform'];
    $vocabularies = array_filter($this->options['vocabularies']);
    if ($type == 'convert') {
      $arg_keys = array_flip($args);
      $result = entity_load_multiple('taxonomy_term', $args);
      if ($transform) {
        foreach ($result as $term) {
          $term->name = str_replace(' ', '-', $term->name);
        }
      }
      foreach ($result as $tid => $term) {
        $args[$arg_keys[$tid]] = $term;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArgumentValidatorPluginBase::access public function Determine if the administrator has the privileges to use this plugin 1
ArgumentValidatorPluginBase::check_access function If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input.
ArgumentValidatorPluginBase::validateOptionsForm public function Provide the default form form for validating options Overrides PluginBase::validateOptionsForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$definition public property Plugins's definition
PluginBase::$displayHandler public property The display object this plugin is for.
PluginBase::$options public property Options for this plugin will be held here.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$usesOptions protected property Denotes whether the plugin has an additional options form. 8
PluginBase::$view public property The top object of a view. 1
PluginBase::additionalThemeFunctions public function Provide a list of additional theme functions for the theme information page
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::destroy public function Clears a plugin. 2
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::pluginTitle public function Return the human readable name of the display.
PluginBase::query public function Add anything to the query that we might need to. 13
PluginBase::setOptionDefaults protected function
PluginBase::summaryTitle public function Returns the summary of the settings in the display. 6
PluginBase::themeFunctions public function Provide a full list of possible theme templates used by this style. 1
PluginBase::unpackOptions public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
PluginBase::usesOptions public function Returns the usesOptions property. 8
PluginBase::validate public function Validate that the plugin is correct and can be saved. 4
PluginBase::__construct public function Constructs a Plugin object. Overrides PluginBase::__construct 2
Term::buildOptionsForm public function Provide the default form for setting options. Overrides ArgumentValidatorPluginBase::buildOptionsForm
Term::defineOptions protected function Retrieve the options when this is a new access control plugin Overrides ArgumentValidatorPluginBase::defineOptions
Term::init public function Initialize this plugin with the view and the argument it is linked to. Overrides ArgumentValidatorPluginBase::init
Term::process_summary_arguments function Process the summary arguments for displaying. Overrides ArgumentValidatorPluginBase::process_summary_arguments
Term::submitOptionsForm public function Provide the default form form for submitting options Overrides ArgumentValidatorPluginBase::submitOptionsForm
Term::validate_argument function Overrides ArgumentValidatorPluginBase::validate_argument