You are here

protected function Node::buildFilters in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/src/Plugin/views/wizard/Node.php \Drupal\node\Plugin\views\wizard\Node::buildFilters()
  2. 9 core/modules/node/src/Plugin/views/wizard/Node.php \Drupal\node\Plugin\views\wizard\Node::buildFilters()

Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().

Add some options for filter by taxonomy terms.

Overrides WizardPluginBase::buildFilters

File

core/modules/node/src/Plugin/views/wizard/Node.php, line 244

Class

Node
Tests creating node views with the wizard.

Namespace

Drupal\node\Plugin\views\wizard

Code

protected function buildFilters(&$form, FormStateInterface $form_state) {
  parent::buildFilters($form, $form_state);
  if (isset($form['displays']['show']['type'])) {
    $selected_bundle = static::getSelected($form_state, [
      'show',
      'type',
    ], 'all', $form['displays']['show']['type']);
  }

  // Add the "tagged with" filter to the view.
  // We construct this filter using taxonomy_index.tid (which limits the
  // filtering to a specific vocabulary) rather than
  // taxonomy_term_field_data.name (which matches terms in any vocabulary).
  // This is because it is a more commonly-used filter that works better with
  // the autocomplete UI, and also to avoid confusion with other vocabularies
  // on the site that may have terms with the same name but are not used for
  // free tagging.
  // The downside is that if there *is* more than one vocabulary on the site
  // that is used for free tagging, the wizard will only be able to make the
  // "tagged with" filter apply to one of them (see below for the method it
  // uses to choose).
  // Find all "tag-like" taxonomy fields associated with the view's
  // entities. If a particular entity type (i.e., bundle) has been
  // selected above, then we only search for taxonomy fields associated
  // with that bundle. Otherwise, we use all bundles.
  $bundles = array_keys($this->bundleInfoService
    ->getBundleInfo($this->entityTypeId));

  // Double check that this is a real bundle before using it (since above
  // we added a dummy option 'all' to the bundle list on the form).
  if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
    $bundles = [
      $selected_bundle,
    ];
  }
  $tag_fields = [];
  foreach ($bundles as $bundle) {
    $display = $this->entityDisplayRepository
      ->getFormDisplay($this->entityTypeId, $bundle);
    $taxonomy_fields = array_filter($this->entityFieldManager
      ->getFieldDefinitions($this->entityTypeId, $bundle), function (FieldDefinitionInterface $field_definition) {
      return $field_definition
        ->getType() == 'entity_reference' && $field_definition
        ->getSetting('target_type') == 'taxonomy_term';
    });
    foreach ($taxonomy_fields as $field_name => $field) {
      $widget = $display
        ->getComponent($field_name);

      // We define "tag-like" taxonomy fields as ones that use the
      // "Autocomplete (Tags style)" widget.
      if (!empty($widget) && $widget['type'] == 'entity_reference_autocomplete_tags') {
        $tag_fields[$field_name] = $field;
      }
    }
  }
  if (!empty($tag_fields)) {

    // If there is more than one "tag-like" taxonomy field available to
    // the view, we can only make our filter apply to one of them (as
    // described above). We choose 'field_tags' if it is available, since
    // that is created by the Standard install profile in core and also
    // commonly used by contrib modules; thus, it is most likely to be
    // associated with the "main" free-tagging vocabulary on the site.
    if (array_key_exists('field_tags', $tag_fields)) {
      $tag_field_name = 'field_tags';
    }
    else {
      $tag_field_name = key($tag_fields);
    }

    // Add the autocomplete textfield to the wizard.
    $form['displays']['show']['tagged_with'] = [
      '#type' => 'entity_autocomplete',
      '#title' => $this
        ->t('tagged with'),
      '#target_type' => 'taxonomy_term',
      '#tags' => TRUE,
      '#size' => 30,
      '#maxlength' => 1024,
    ];
    $target_bundles = $tag_fields[$tag_field_name]
      ->getSetting('handler_settings')['target_bundles'] ?? FALSE;
    if (!$target_bundles) {
      $target_bundles = array_keys($this->bundleInfoService
        ->getBundleInfo('taxonomy_term'));
    }
    $form['displays']['show']['tagged_with']['#selection_settings']['target_bundles'] = $target_bundles;
  }
}