You are here

class NodeTermData in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php \Drupal\taxonomy\Plugin\views\relationship\NodeTermData
  2. 9 core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php \Drupal\taxonomy\Plugin\views\relationship\NodeTermData

Relationship handler to return the taxonomy terms of nodes.

Plugin annotation

@ViewsRelationship("node_term_data");

Hierarchy

  • class \Drupal\views\Plugin\views\relationship\RelationshipPluginBase extends \Drupal\views\Plugin\views\HandlerBase
    • class \Drupal\taxonomy\Plugin\views\relationship\NodeTermData

Expanded class hierarchy of NodeTermData

Related topics

File

core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php, line 18

Namespace

Drupal\taxonomy\Plugin\views\relationship
View source
class NodeTermData extends RelationshipPluginBase {

  /**
   * The vocabulary storage.
   *
   * @var \Drupal\taxonomy\VocabularyStorageInterface
   */
  protected $vocabularyStorage;

  /**
   * Constructs a NodeTermData object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   *   The vocabulary storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->vocabularyStorage = $vocabulary_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager')
      ->getStorage('taxonomy_vocabulary'));
  }
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['vids'] = [
      'default' => [],
    ];
    return $options;
  }
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $vocabularies = $this->vocabularyStorage
      ->loadMultiple();
    $options = [];
    foreach ($vocabularies as $voc) {
      $options[$voc
        ->id()] = $voc
        ->label();
    }
    $form['vids'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Vocabularies'),
      '#options' => $options,
      '#default_value' => $this->options['vids'],
      '#description' => $this
        ->t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
    ];
    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {

    // Transform the #type = checkboxes value to a numerically indexed array,
    // because the config schema expects a sequence, not a mapping.
    $vids = $form_state
      ->getValue([
      'options',
      'vids',
    ]);
    $form_state
      ->setValue([
      'options',
      'vids',
    ], array_values(array_filter($vids)));
  }

  /**
   * Called to implement a relationship in a query.
   */
  public function query() {
    $this
      ->ensureMyTable();
    $def = $this->definition;
    $def['table'] = 'taxonomy_term_field_data';
    if (!array_filter($this->options['vids'])) {
      $taxonomy_index = $this->query
        ->addTable('taxonomy_index', $this->relationship);
      $def['left_table'] = $taxonomy_index;
      $def['left_field'] = 'tid';
      $def['field'] = 'tid';
      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
    }
    else {

      // If vocabularies are supplied join a subselect instead
      $def['left_table'] = $this->tableAlias;
      $def['left_field'] = 'nid';
      $def['field'] = 'nid';
      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
      $def['adjusted'] = TRUE;
      $query = Database::getConnection()
        ->select('taxonomy_term_field_data', 'td');
      $query
        ->addJoin($def['type'], 'taxonomy_index', 'tn', '[tn].[tid] = [td].[tid]');
      $query
        ->condition('td.vid', array_filter($this->options['vids']), 'IN');
      if (empty($this->query->options['disable_sql_rewrite'])) {
        $query
          ->addTag('taxonomy_term_access');
      }
      $query
        ->fields('td');
      $query
        ->fields('tn', [
        'nid',
      ]);
      $def['table formula'] = $query;
    }
    $join = \Drupal::service('plugin.manager.views.join')
      ->createInstance('standard', $def);

    // use a short alias for this:
    $alias = $def['table'] . '_' . $this->table;
    $this->alias = $this->query
      ->addRelationship($alias, $join, 'taxonomy_term_field_data', $this->relationship);
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    foreach ($this->options['vids'] as $vocabulary_id) {
      if ($vocabulary = $this->vocabularyStorage
        ->load($vocabulary_id)) {
        $dependencies[$vocabulary
          ->getConfigDependencyKey()][] = $vocabulary
          ->getConfigDependencyName();
      }
    }
    return $dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeTermData::$vocabularyStorage protected property The vocabulary storage.
NodeTermData::buildOptionsForm public function Overrides RelationshipPluginBase::buildOptionsForm
NodeTermData::calculateDependencies public function Overrides RelationshipPluginBase::calculateDependencies
NodeTermData::create public static function
NodeTermData::defineOptions protected function Overrides RelationshipPluginBase::defineOptions
NodeTermData::query public function Called to implement a relationship in a query. Overrides RelationshipPluginBase::query
NodeTermData::submitOptionsForm public function
NodeTermData::__construct public function Constructs a NodeTermData object.
RelationshipPluginBase::$alias public property The relationship alias.
RelationshipPluginBase::init public function Overrides \Drupal\views\Plugin\views\HandlerBase::init().
RelationshipPluginBase::usesGroupBy public function