Problem/Motivation

When creating exposed grouped filters in a view, if a group is named and using autocomplete widget to add group items (can be taxonomy terms or users), the form throws the error on save:

The value is required if label for this item is defined.

Here is the screenshot of the error:
image 1

The problem behind this is that array of arrays is not recognized here:

$min_values = $operators[$group['operator']]['values'];
$actual_values = count(array_filter($group['value'], 'static::arrayFilterZero'));

In case autocomplete, it has the following data format:

[
  0 => [
    ' target_id' => 1
  ] ,
]

but the code above expects it to be:

[
  1 => 1
]

so it doesn't pass the filtering in static::arrayFilterZero

Affected plugins:

  1. \Drupal\user\Plugin\views\filter\Name (#2920039: Views' User Name exposed group filter validation)
  2. \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid (this issue)

Steps to reproduce

  1. Install Drupal with "Standard" profile
  2. Open content view (/admin/structure/views/view/content)
  3. Add an exposed grouped filter by "Tags" (Taxonomy). Make sure the group item is using autocomplete widget
  4. Add at least one item to the group configuration
  5. Submit

Proposed resolution

Convert values into array with ids, which is expected by base filter plugin.

Remaining tasks

1) Wait for #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID;
2) Review/commit;

CommentFileSizeAuthor
#65 2576927-65.patch8.54 KBrubens.arjr
#64 2576927-diff_61-64.txt1.68 KBmatroskeen
#64 2576927-64.patch8 KBmatroskeen
#61 2576927-diff_59-61.txt4.15 KBmatroskeen
#61 2576927-61.patch8.28 KBmatroskeen
#59 2576927-diff_57-59.txt904 bytesmatroskeen
#59 2576927-59.patch6.19 KBmatroskeen
#57 2576927-57.patch6.15 KBmatroskeen
#57 2576927-57-test_only.patch3.05 KBmatroskeen
#47 2576927-47.patch5.98 KBlendude
#47 interdiff-2576927-46-47.txt885 byteslendude
#47 2576927-47-TEST_ONLY.patch2.88 KBlendude
#46 2576927-45.patch5.98 KBlendude
#46 2576927-45-TEST_ONLY.patch2.88 KBlendude
#32 2576927-32-taxonomy-group-filter-array-8.3.x.patch6.02 KBkbasarab
Screen Shot 2015-09-29 at 9.57.15 AM.png179.84 KBtkoleary
Screen Shot 2015-09-29 at 9.57.37 AM.png195.35 KBtkoleary
#8 2576927-8-grouped-taxonomy-filters.patch2.88 KBmikeker
#15 taxonomy_group_filter-2576927-15.patch2.25 KBlendude
#17 interdiff-2576927-15-17.txt1.82 KBlendude
#17 taxonomy_group_filter-2576927-17.patch2.84 KBlendude
#18 taxonomy_group_filter-2576927-18-TEST_ONLY.patch2.9 KBlendude
#18 taxonomy_group_filter-2576927-18.patch5.82 KBlendude
#22 2576927-22-taxonomy-group-filter.patch6.34 KBmikeker
#25 2576927-25-taxonomy-group-filter.patch5.85 KBlendude
#29 2576927-25-taxonomy-group-filter-array-reroll.patch5.78 KBlendude
#29 2576927-25-taxonomy-group-filter-array-reroll.patch5.78 KBlendude
#31 interdiff.txt636 byteskbasarab
#31 2576927-30-taxonomy-group-filter-array-8.3.x.patch5.97 KBkbasarab
#31 2576927-30-taxonomy-group-filter-array.patch6.02 KBkbasarab

Issue fork drupal-2576927

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tkoleary created an issue. See original summary.

tkoleary’s picture

Issue summary: View changes
tkoleary’s picture

Issue summary: View changes
tkoleary’s picture

Issue summary: View changes
tkoleary’s picture

Issue summary: View changes
tkoleary’s picture

Issue summary: View changes
tkoleary’s picture

Issue summary: View changes
mikeker’s picture

Priority: Major » Normal
Status: Needs work » Needs review
Issue tags: -views, -taxonomy +VDC
StatusFileSize
new2.88 KB

I thin the underlying problem is that the autocomplete widget is completely broken in this case. If you switch to the dropdown widget, the grouped filter works correctly. As such I don't think this qualifies as Major (isolated impact, has a workaround).

There is also a nomenclature issue in that "title" in the error refers to fields in the column "label." One or the other needs to change.

Agreed! And it's missing "the" before "title." Attached patch fixes that, but does nothing about the underlying problem. I'll look into that later...

Status: Needs review » Needs work

The last submitted patch, 8: 2576927-8-grouped-taxonomy-filters.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: 2576927-8-grouped-taxonomy-filters.patch, failed testing.

mikeker’s picture

Version: 8.1.x-dev » 8.0.x-dev
Status: Needs work » Needs review

grr...

lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

@mikeker I'm looking into this, but can I suggest that we move your patch to a new issue? I think it's a good documentation fix and RTBC in itself. Fixing the TaxonomyIndexTid filter handler for grouping is going to take much more work to fix (it's a mess).

Anyway, this needs more work.

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new2.25 KB

First stab at a fix partial. I didn't include the patch in #8 (see #14).

Manual testing lets me save the value and it rebuilds the form like it should. The filter looks good too, but it doesn't actually filter anything when you select a value. So that needs work. And tests.

mikeker’s picture

#14: @Lendude, makes sense -- I've filed a followup in #2633678: Improve grouped filter form and fix validation problems.

Agreed, TaxonomyIndexTid is going to be a bit of work... Thanks for taking that on and good luck!

lendude’s picture

Now with working filter. Still needs tests.

lendude’s picture

Now with tests. When this is fixed though you run into #2369119: Fatal error when trying to save a View with grouped filters using other than string values. So you can't actually save the View until that is fixed.

Because of that I now only test the output in the preview because that works fine.

Interdiff is the test only patch.

The last submitted patch, 18: taxonomy_group_filter-2576927-18-TEST_ONLY.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Status: Needs review » Needs work

The last submitted patch, 18: taxonomy_group_filter-2576927-18.patch, failed testing.

mikeker’s picture

Version: 8.1.x-dev » 8.2.x-dev
Status: Needs work » Needs review
StatusFileSize
new6.34 KB

Rerolled #18.

Status: Needs review » Needs work

The last submitted patch, 22: 2576927-22-taxonomy-group-filter.patch, failed testing.

The last submitted patch, 22: 2576927-22-taxonomy-group-filter.patch, failed testing.

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new5.85 KB

@mikeker thanks for the reroll! forgot to remove a couple of merge tags.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dagmar’s picture

Status: Needs review » Needs work

Needs a re-roll

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new5.78 KB
new5.78 KB

array() => [] reroll, nothing else.

edit: not sure why that got uploaded twice, same patch, ignore one.

dagmar’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -265,13 +265,30 @@ protected function valueValidate($form, FormStateInterface $form_state) {
    +      if (!empty($form_state->getValue(['options', 'value']))) {
    

    This !empty should not be neccesary

  2. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -398,4 +419,21 @@ public function calculateDependencies() {
    +  public function buildExposedFiltersGroupForm(&$form, FormStateInterface $form_state) {
    ...
    +    return parent::buildExposedFiltersGroupForm($form, $form_state);
    

    I never saw this pattern in views before. I know this probably will work without side effects, but usually what we do is call the parent method at the beginning and then do the other modifications.

  3. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -398,4 +419,21 @@ public function calculateDependencies() {
    +    if ($this->options['type'] == 'textfield') {
    

    Hm. Is this the only way we have to check that a field is using autocomplete?

  4. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -398,4 +419,21 @@ public function calculateDependencies() {
    +          $terms = Term::loadMultiple(($item['value']));
    

    Double parenthesis here

kbasarab’s picture

Updated this for 8.4.x and 8.3.x.

  • Added a check for the all filter in validateExposed method. This was throwing an undefined index notice.
  • Changed location of test for 8.4.x so it applies cleanly to 8.4.x. 8.3.x patch retains the original location
  • Looks like most of the comments from dagmar were already addressed in #29. I verified the parent:: return but if we move this to top of method then the grouping will return just the entity ID in the values field and throw and unknown entity error upon save.
kbasarab’s picture

Rerolls for 8.3.5 support.

mikeker’s picture

Status: Needs work » Needs review

Let's see what the testbots have to say.

The last submitted patch, 31: 2576927-30-taxonomy-group-filter-array-8.3.x.patch, failed testing. View results

ericshell’s picture

#32 has worked for me.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

golddragon007’s picture

#31's 8.4.x doesn't work for me, I get this error when I tried to filter counted relationship data.

Before patch:
InvalidArgumentException: The configuration property display.page_search_ideas.display_options.filters.title.value.value doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 76 of D:\phptest\theideaproject\web\core\lib\Drupal\Core\Config\Schema\ArrayElement.php).

After patch:
InvalidArgumentException: The configuration property display.page_search_ideas.display_options.filters.title.value.min doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 76 of D:\phptest\theideaproject\web\core\lib\Drupal\Core\Config\Schema\ArrayElement.php).

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rfmarcelino’s picture

Core 9.1.8 has this applied. I would recommend changing the status to Fixed.

lendude’s picture

StatusFileSize
new2.88 KB
new5.98 KB

@rfmarcelino nope, this is still broken, new Test-only patch to show this still breaks

reroll and update to remove deprecated methods.

lendude’s picture

StatusFileSize
new2.88 KB
new885 bytes
new5.98 KB

Fixed CS

The last submitted patch, 47: 2576927-47-TEST_ONLY.patch, failed testing. View results

matroskeen’s picture

Title: grouped exposed taxonomy filters return invalid when a valid entity is present » Grouped exposed filters fails validation for autocomplete widget
Version: 9.3.x-dev » 9.4.x-dev
Issue summary: View changes
Issue tags: +Bug Smash Initiative
Related issues: +#2636086: Add extra test coverage for operators of views date filters

I reproduced the same issue when was investigating another one: #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID.

It looks like it's applicable to every filter, which is using autocomplete widget for group items. For instance, I have the same issue when trying to add a filter by Author, which is using \Drupal\user\Plugin\views\filter\Name class.

It makes me think that the fix itself should be either in another place, or we should also take care of other plugins in addition to \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid.

I'm just updating the issue summary, because and I don't have any code suggestions for now.

matroskeen’s picture

Status: Needs review » Needs work

I also meant to change the status :)
@lendude, please let me know if you'd like to continue here. Otherwise, we can swap and I'll try to come up with some patch.

matroskeen’s picture

After further investigation, I agree with the approach taken by @lendude in previous patches - value normalization should happen in valueValidate method.

I applied the same changes to \Drupal\user\Plugin\views\filter\Name class, so we should probably need a test coverage for this plugin as well.

I also had to revert some changes in \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid that are already covered by #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID. Unfortunately, tests here won't pass until we land #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID.

I also created another issue that I faced along the way: #3250352: Username views filter should not process default value twice .

Next steps:
1) Add similar test coverage for \Drupal\user\Plugin\views\filter\Name plugin;
2) Transfer issue credits from #2920039: Views' User Name exposed group filter validation and close it as a duplicate;
3) Resume when #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID is in;

matroskeen’s picture

Title: Grouped exposed filters fails validation for autocomplete widget » [PP-1] Grouped exposed taxonomy filter fails validation for autocomplete widget
Issue summary: View changes
Status: Needs work » Postponed

Removing references to \Drupal\user\Plugin\views\filter\Name plugin that will be fixed in #2920039: Views' User Name exposed group filter validation.
We'll resume here when #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID is done.

matroskeen’s picture

Status: Postponed » Needs review
matroskeen’s picture

Title: [PP-1] Grouped exposed taxonomy filter fails validation for autocomplete widget » Grouped exposed taxonomy filter fails validation for autocomplete widget

It looks like a test failure is a random one.

matroskeen’s picture

Version: 9.4.x-dev » 10.0.x-dev
StatusFileSize
new3.05 KB
new6.15 KB

The last submitted patch, 57: 2576927-57-test_only.patch, failed testing. View results

matroskeen’s picture

StatusFileSize
new6.19 KB
new904 bytes
lendude’s picture

Status: Needs review » Needs work

Really nitty nitpicks only, probably only removing stuff I added myself in the first place :D

  1. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -290,13 +290,30 @@ protected function valueValidate($form, FormStateInterface $form_state) {
    +    // Autocomplete puts the values in target_id. Move the values to the
    +    // expected depth.
    

    Might be nice to point to what is expecting this

  2. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -359,11 +376,15 @@ public function validateExposed(&$form, FormStateInterface $form_state) {
    +      // Set the validated_exposed_input to the selected group values.
    

    Not sure we need this comment? Seems pretty obvious what this does :)

  3. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -429,4 +450,21 @@ public function calculateDependencies() {
    +    if ($this->options['type'] == 'textfield') {
    

    We can do === here I think?

  4. +++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyIndexTidUiTest.php
    @@ -283,6 +283,7 @@ public function testExposedGroupedFilter() {
    +    $filter_settings_path = '/admin/structure/views/nojs/handler/test_taxonomy_exposed_grouped_filter/page_1/filter/field_views_testing_tags_target_id';
    
    @@ -292,7 +293,7 @@ public function testExposedGroupedFilter() {
    -    $this->drupalGet('/admin/structure/views/nojs/handler/test_taxonomy_exposed_grouped_filter/page_1/filter/field_views_testing_tags_target_id');
    +    $this->drupalGet($filter_settings_path);
    

    Hmmmm borderline unrelated I think, but lets keep it :) No action required.

matroskeen’s picture

Status: Needs work » Needs review
StatusFileSize
new8.28 KB
new4.15 KB

Done! Following the example on #2920039: Views' User Name exposed group filter validation I also changed the usage of static methods to term storage methods.
Technically, this is out of scope, but why don't we change it here? :)

lendude’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
@@ -290,13 +289,30 @@ protected function valueValidate($form, FormStateInterface $form_state) {
+    // Autocomplete puts the values in target_id. Move the values as expected by
+    // Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::validateExposed() method.

This seems to be longer than 80 chars but the bot doesn't seem to be tripping over this, so ¯\_(ツ)_/¯

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Down to more minor nits here

  1. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -221,7 +220,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
    +        $terms = $this->termStorage->loadMultiple($query->execute());
    

    $query->execute can return NULL, which would cause an error for ::loadMultiple

    But I see that's an existing coddepath so perhaps follow-up?

  2. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -290,13 +289,30 @@ protected function valueValidate($form, FormStateInterface $form_state) {
    +    if ($this->isAGroup()) {
    

    let's return early inside this if and avoid the else while we're touching this code, it makes it much easier to read as the cyclic complexity is reduced

  3. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -290,13 +289,30 @@ protected function valueValidate($form, FormStateInterface $form_state) {
    +            foreach ($item['value'] as $value) {
    +              $tids[] = $value['target_id'];
    +            }
    

    We can write this as $tids = array_column($item['value'], 'target_id');

  4. +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php
    @@ -290,13 +289,30 @@ protected function valueValidate($form, FormStateInterface $form_state) {
    +        foreach ($form_state->getValue(['options', 'value']) as $value) {
    +          $tids[] = $value['target_id'];
    +        }
    

    same here re array_column

matroskeen’s picture

Status: Needs work » Needs review
StatusFileSize
new8 KB
new1.68 KB

1) I'm not sure about this. The interface declares the following:

/**
   * Execute the query.
   *
   * @return int|array
   *   Returns an integer for count queries or an array of ids. The values of
   *   the array are always entity ids. The keys will be revision ids if the
   *   entity supports revision and entity ids if not.
   */
  public function execute();

I also found few more cases matching this pattern: loadMultiple($query->execute()).
Can it really return NULL in some cases?

2) Done
3-4) Good call on using array_column

I also made some minor rearrangements around the lines I was already modifying, hopefully I didn't go too far :)

rubens.arjr’s picture

StatusFileSize
new8.54 KB

In 9.4 there was a bug:
TypeError: Illegal offset type in Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid->validateExposed() (line 364 of /var/www/docroot/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php)
I'm sending a new patch fixing this bug.

matroskeen’s picture

Can we add/edit the test to see the bug and make sure it was caught?

matroskeen’s picture

@rubens.arjr, can you add some steps to reproduce the issue mentioned in your comment?
As you might see, I queued a test of my previous patch for Drupal 9.4.x and it's green. Therefore, your issue probably requires some additional steps. It might be worth moving this into a follow-up, but we need to see what's the root cause.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Tested #64 as #65 steps were not provided.

Can confirm the issue described in the IS and the steps were perfect.
Applied patch
Now am able to save the group filter without issue.

Searching for loadMultiple($query->execute()); only none test file I saw was TermStorage.php

Just to be extra safe could we do something like

        $terms = [];
        $results = $query->execute();
        if (isset($results)) {
          $terms = $this->termStorage->loadMultiple($results);
        }

Version: 10.0.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.