function Node::buildFilters
Same name in other branches
- 9 core/modules/node/src/Plugin/views/wizard/Node.php \Drupal\node\Plugin\views\wizard\Node::buildFilters()
- 10 core/modules/node/src/Plugin/views/wizard/Node.php \Drupal\node\Plugin\views\wizard\Node::buildFilters()
- 11.x 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 248
Class
- Node
- Tests creating node views with the wizard.
Namespace
Drupal\node\Plugin\views\wizardCode
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 ($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.
$target_bundles = $tag_fields[$tag_field_name]->getSetting('handler_settings')['target_bundles'];
$form['displays']['show']['tagged_with'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('tagged with'),
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => $target_bundles,
],
'#tags' => TRUE,
'#size' => 30,
'#maxlength' => 1024,
];
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.