Problem/Motivation

If an empty string ("") is passed in as contextual filter argument, it isn't considered a missing argument but is filtered against.

This was raised before in #2815863: Contextual filter values of "" don't trigger "When the filter value is NOT available" against D7 Views module, and originally came up in #1699378-20: Allow tokens in entity reference views selection arguments and was considered a Views (now: Core) bug in #1699378-94: Allow tokens in entity reference views selection arguments.

Steps to reproduce

Example:

      $arguments = [ 
        0 => '', // is processed in query
        1 => NULL, // is skipped and default argument or default action is used
        2 => 'foo' // is processed in query
      ];

      /** @var \Drupal\views\ViewExecutable $view */
      $view = Views::getView($target_id);
      $view->setArguments($arguments);
      $view->preExecute();
      $view->execute();

An empty string as argument could be an result of a token replacement like this:

$value = '[node:field_with_no_user_input]';
$token_data = [$entity->getEntityTypeId() => $entity];
$token_service = \Drupal::token();
$arguments[] = $token_service->replace($value, $token_data);

Proposed resolution

I think sanitizing the empty string to no argument given is the expected behavior. We should however think twice making sure there really isn't a usecase for the current behavior resp. no sitebuilder's assumption might be broken.

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Release notes snippet

TBD

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Pancho created an issue. See original summary.

Pancho’s picture

Here's the patch against 8.6.x-dev.

Note that this surprisingly doesn't affect the similar case where a contextual filter argument is retrieved from a query parameter (say id) and a fallback value is given (say, all). Even with this patch, this means /admin/testview?id= continues to evaluate to a given argument = "", resulting in a Page not found, while /admin/testview results in the expected view using the fallback parameter. This might be a separate bug, however I didn't find the correct place to fix that one.

Pancho’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: empty_string_contextual_filter-3027640-2.patch, failed testing. View results

Pancho’s picture

Status: Needs work » Needs review
FileSize
623 bytes

!empty breaks a test as it evaluates both "" and "0" as empty. "0" however is a legitimate value.
isset && !== "" does the job just as well, while being less aggressive. The tests should be green now.

Pancho’s picture

Issue tags: +Needs tests
Pancho’s picture

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

Kristen Pol’s picture

Version: 8.9.x-dev » 9.1.x-dev
Issue tags: +Bug Smash Initiative

Patch applies cleanly to 9.1.x.

Kristen Pol’s picture

Thanks for the issue and patch.

1) The changes are similar to the Drupal 7 change: https://git.drupalcode.org/project/views/commit/610f570

2) Although the code below is technically correct, IMO it would be easier to read if the logic was like the Drupal 7 code or, at least, there are parentheses added around isset($this->args[$position]) && $this->args[$position] !== ''.

+++ b/core/modules/views/src/ViewExecutable.php
@@ -1082,7 +1082,7 @@ protected function _buildArguments() {
+      $arg = isset($this->args[$position]) && $this->args[$position] !== '' ? $this->args[$position] : NULL;

3) Kicking of tests for 9.1.x.

Kristen Pol’s picture

Status: Needs review » Needs work

Marking needs work for tests and consideration of feedback in #11.

Adsyy’s picture

Status: Needs work » Needs review
FileSize
625 bytes

Hi, I apply changes as #11 suggested.

I don't know what is best between if structure or ternary inline operator, I think ternary can be a bit harder to read but in this case it's not.
Hope I helped !

Kristen Pol’s picture

Status: Needs review » Needs work

Thanks for the update. The change from #13 looks good and addresses the feedback from #11. Moving back to needs work for tests.

Kristen Pol’s picture

Tagging for steps to reproduce and manual testing too.

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.

marc.groth’s picture

The patch works really well for when you preview the view in the admin page... However results are still being displayed on the front end. In my case I have a contextual filter against taxonomy vocabularies and have verified that the node itself doesn't have any of these values set (i.e. no terms are associated with the node).

I have checked the query (using hook_views_query_alter()) and it looks like this field value is not present on the front end. Whereas in the preview it is (like this):

"field" => "taxonomy_index.tid"
"value" => null
"operator" => "IS NULL"

I'm not too sure why this would be present on the preview but not where it is actually output as a block. Is there something I've missed? For now I have added my own check for this value and if it is not in the query then I add it to ensure that there are no results; however it would be good if this could happen automatically somehow.

allaprishchepa’s picture

Faced the same issue.
Actually, it was related to that one: https://www.drupal.org/project/views_data_export/issues/2950819
But it's because of the empty string in the contextual filter argument.
We have a Contextual filter by the Taxonomy term reference field. And default value is set as "Taxonomy term ID from URL". So if the current node has no corresponding term, we have this error.
Symfony\Component\Routing\Exception\InvalidParameterException: Parameter "arg_0" for route "view.content.export_csv" must match "[^/]++" ("" given) to generate a corresponding URL. in Drupal\Core\Routing\UrlGenerator->doGenerate()

I modified the patch from #13 and added the same checking for argument returned with getDefaultArgument().

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.

stefan.butura’s picture

I've noticed that the query_parameter argument default handler still handles empty strings ("") as valid filters. I've attached a patch that fixes this.

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

marc.groth’s picture

The patch in #20 no longer applies so I have re-rolled it (attached).

danflanagan8’s picture

Here's a fail test along with the fix from #13.

I don't I think I understand what's going on in #18, but it looks related to this: #3239685: View parameter doesn't exist when editing block layout: Parameter "arg_0" must match "[^/]++"

And with #20, I'm concerned that it's getting out of scope. The D7 issue that we're kind of modeling after does not touch any default argument plugins. That might be better handled in a separate issue.

danflanagan8’s picture

Issue summary: View changes

I just closed #2937759: If computed view argument is empty the default value or action is not used as a duplicate of this issue. It had some nice stuff in the IS that I have used to update the IS here.

The last submitted patch, 23: 3027640-23-FAIL.patch, failed testing. View results

danflanagan8’s picture

The patches in #23 ran as expected. They are ready for review. I'm removing the Needs steps to reproduce tag since I update the IS in #24.

quietone credited pminf.

quietone’s picture

Adding credit as recommended by @danflanagan8 in #bugsmash

Kristen Pol’s picture

Thanks for the updated patch. Adding tag due to TBDs in the issue summary.

1. Patch still applies cleanly to 9.4.

2. Test-only patch fails and tests with patch pass.

3. Patch is simple and only addresses the issue in the issue summary.

4. Test has been added.

Remaining tasks:

1. Update issue summary.

2. Manual testing.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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.

For the issue summary update requested in #29

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.