Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::build_filters().
Add some options for filter by taxonomy terms.
Overrides WizardPluginBase::build_filters
protected function build_filters(&$form, &$form_state) {
parent::build_filters($form, $form_state);
$entity_info = $this->entity_info;
$selected_bundle = static::getSelected($form_state, array(
'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_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($entity_info['bundles']);
// 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 = array(
$selected_bundle,
);
}
$tag_fields = array();
foreach ($bundles as $bundle) {
foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
// We define "tag-like" taxonomy fields as ones that use the
// "Autocomplete term widget (tagging)" widget.
if ($instance['widget']['type'] == 'taxonomy_autocomplete') {
$tag_fields[] = $instance['field_name'];
}
}
}
$tag_fields = array_unique($tag_fields);
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 (in_array('field_tags', $tag_fields)) {
$tag_field_name = 'field_tags';
}
else {
$tag_field_name = reset($tag_fields);
}
// Add the autocomplete textfield to the wizard.
$form['displays']['show']['tagged_with'] = array(
'#type' => 'textfield',
'#title' => t('tagged with'),
'#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
'#size' => 30,
'#maxlength' => 1024,
'#field_name' => $tag_field_name,
'#element_validate' => array(
'views_ui_taxonomy_autocomplete_validate',
),
);
}
}