NULL value should be empty string

Created on 17 July 2023, over 1 year ago
Updated 19 April 2024, 8 months ago

Problem/Motivation

When the value is empty, getArgument() is NULL.

This return type is an issue when trying to validate the argument since Drupal\views\Plugin\views\argument_validator\Entity->validateArgument() uses the argument in preg_split, resulting in error #1.

This is later used in preg_match in Drupal\views\Plugin\views\HandlerBase::breakString() resulting in error #2.

Finally, it reaches Drupal\views\Plugin\views\argument\NumericArgument->query() and hits a TypeError:

TypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator of type string in Drupal\views\Plugin\views\argument\NumericArgument->query() (line 96 of /home/{USER}/public_html/core/modules/views/src/Plugin/views/argument/NumericArgument.php).

Steps to reproduce

Note: this is untested, but should theoretically work.

  1. Install Drupal 10.1
  2. Create a view with a user contextual filter with a default value set by views_arg_entity_field.
  3. Visit the view using the anonymous user.
  4. Note the error.

Proposed resolution

Return the empty string if the return value is NULL.

Remaining tasks

Figure out if the module ever returns NULL in regularly functionality.

User interface changes

N/A.

API changes

getArgument() returns an empty string rather than NULL

Data model changes

N/A.

Errors

Error #1

Deprecated function: preg_split(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\views\Plugin\views\argument_validator\Entity->validateArgument() (line 183 of /home/{USER}/public_html/core/modules/views/src/Plugin/views/argument_validator/Entity.php)
#0 /home/{USER}/public_html/core/includes/bootstrap.inc(164): _drupal_error_handler_real(8192, 'preg_split(): P...', '/home/{USER}/...', 183)
#1 [internal function]: _drupal_error_handler(8192, 'preg_split(): P...', '/home/{USER}/...', 183)
#2 /home/{USER}/public_html/core/modules/views/src/Plugin/views/argument_validator/Entity.php(183): preg_split('/[,+ ]/', NULL)
#3 /home/{USER}/public_html/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php(1066): Drupal\views\Plugin\views\argument_validator\Entity->validateArgument(NULL)
#4 /home/{USER}/public_html/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php(1101): Drupal\views\Plugin\views\argument\ArgumentPluginBase->validateArgument(NULL)
#5 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1116): Drupal\views\Plugin\views\argument\ArgumentPluginBase->setArgument(NULL)
#6 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1282): Drupal\views\ViewExecutable->_buildArguments()
#7 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1406): Drupal\views\ViewExecutable->build(NULL)
#8 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1469): Drupal\views\ViewExecutable->execute(NULL)

Error #2

Deprecated function: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\views\Plugin\views\HandlerBase::breakString() (line 752 of /home/{USER}/public_html/core/modules/views/src/Plugin/views/HandlerBase.php)
#0 /home/{USER}/public_html/core/includes/bootstrap.inc(164): _drupal_error_handler_real(8192, 'preg_match(): P...', '/home/{USER}/...', 752)
#1 [internal function]: _drupal_error_handler(8192, 'preg_match(): P...', '/home/{USER}/...', 752)
#2 /home/{USER}/public_html/core/modules/views/src/Plugin/views/HandlerBase.php(752): preg_match('/^([\\w0-9-_\\.]+...', NULL)
#3 /home/{USER}/public_html/core/modules/views/src/Plugin/views/argument/NumericArgument.php(94): Drupal\views\Plugin\views\HandlerBase::breakString(NULL, false)
#4 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1126): Drupal\views\Plugin\views\argument\NumericArgument->query(true)
#5 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1282): Drupal\views\ViewExecutable->_buildArguments()
#6 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1406): Drupal\views\ViewExecutable->build(NULL)
#7 /home/{USER}/public_html/core/modules/views/src/ViewExecutable.php(1469): Drupal\views\ViewExecutable->execute(NULL)
🐛 Bug report
Status

Closed: cannot reproduce

Version

2.0

Component

Code

Created by

🇨🇦Canada Nathan Tsai

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

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024