Empty strings ("") passed in as contextual filter argument aren't considered missing

Created on 22 January 2019, over 5 years ago
Updated 27 June 2024, 2 months ago

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

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 12 hours ago

Created by

Pancho UTC+2 πŸ‡ͺπŸ‡Ί EU

Live updates comments and jobs are added and updated live.
  • Needs manual testing

    The change/bugfix cannot be fully demonstrated by automated testing, and thus requires manual testing in a variety of environments.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    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

  • πŸ‡³πŸ‡±Netherlands ricovandevin

    We have a similar issue that is also resolved by this patch. In our case we have a contextual filter on taxonomy term, configured to default to the term set on an entity reference field on a node. When no value is selected on the node, the \Drupal\taxonomy\Plugin\views\argument_default\Tid (taxonomy_id) ViewsArgumentDefault plugin also returns an empty string. This even leads to an error without the patch:

    TypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string in Drupal\views\Plugin\views\argument\ArgumentPluginBase->unpackArgumentValue() (line 1302 of core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php).

Production build 0.71.5 2024