Problem/Motivation

When calling a view with invalid numeric argument, e.g. "Content: ID" it produces the following error.

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 C:\xampp\htdocs\cmacgm\drupal-10\docroot\core\modules\views\src\Plugin\views\argument\NumericArgument.php).

Steps to reproduce

  1. Create a test view for Content
  2. Add a contextual filter for "Content: ID"
  3. Enable "Allow multiple values" in "More" when adding the filter
  4. Use the Preview to enter "%15" as an argument
  5. Check watchdog or browser console for the error above

Proposed resolution

In NumericArgument.php, L63 add a check for $break->operator. If that value is NULL, it means the argument inserted is wrong. Show/log an error that says the argument X in Y view is invalid instead of the original error.

Remaining tasks

TBD

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-3353786

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

shrikant.dhotre created an issue. See original summary.

cilefen’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: -NumericArgument, -$operator +Needs steps to reproduce
shrikant.dhotre’s picture

Status: Postponed (maintainer needs more info) » Needs review
Issue tags: -Needs steps to reproduce +Needs review
StatusFileSize
new711 bytes

Found the solution in null operator

cilefen’s picture

Status: Needs review » Needs work
Issue tags: -Needs review +Needs steps to reproduce
shrikant.dhotre’s picture

StatusFileSize
new711 bytes
shrikant.dhotre’s picture

StatusFileSize
new719 bytes
shrikant.dhotre’s picture

StatusFileSize
new729 bytes
shrikant.dhotre’s picture

shrikant.dhotre’s picture

Status: Needs work » Needs review
Issue tags: -Needs steps to reproduce
cilefen’s picture

Status: Needs review » Needs work
Issue tags: +Needs steps to reproduce

Thank you for the patches. Bugs in Drupal Core require steps to reproduce. Please do not remove that tag until providing the steps to reproduce.

dpi’s picture

The MR/solution is trivial enough to not warrant credit.

We need steps to reproduce, so we may consider adding tests.

Aside: empty is typically too naive, we need to be using narrower type checking.

_pratik_ made their first commit to this issue’s fork.

lendude’s picture

Status: Needs work » Postponed (maintainer needs more info)

Marking postpone until we have some steps to reproduce this on a clean Drupal install, usually this is just hiding a problem upstream.

The question to answer is why this would ever be NULL, is it NULL in a valid scenario or due to another problem.

shrikant.dhotre’s picture

Thanks for highlighting, I will deep analyses whether NULL is valid scenario or not

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.

shrikant.dhotre’s picture

Yes, this is valid Scenario, when we are creating a field with machine name having suffix numeric view will not work,
e.g.

field_1
field_2

And when we are using views field as view this fields arguments are giving an error.
TypeError: Cannot assign null to property

quietone’s picture

Issue summary: View changes

@shrikant.dhotre, thanks. Can you add more detail? Does the error happen when creating the view or when viewing the page. There is an example of the detail needed in Document steps to reproduce an issue.

I did try to test, creating a view with a integer numeric field with a name 'field_2'. I did not get an error when creating the page or viewing it.

I have also restored the standard template to make this issue easier to work on.

shrikant.dhotre’s picture

Hi @Quietone,

I created fields machine names with numeric, with implementation of workflow module.
created below fields and nested paragraphs with content type using content moderation state, paragraph and view field as view contributed
Modedration stage are defiend like : Draft, Pending, Approved, Archived.
field_name_1,
field_name_2 >> field_name_22 >> field_name_222,
field_name_3 >> field_name_333 >> field_name_444

Now requirement is like, we have impleted drupal with headless arch.
Created a Json API with using content revision, so each field is comming with historical data with revision_id
field_name_1-revision_id

For Json API, created a views in rest export nested.

view output is giving an error with
TypeError: Cannot assign null to property

Lysenko made their first commit to this issue’s fork.

lysenko’s picture

In my opinion, this question arose because of a change in this place:
public string $operator;

The value NULL for this variable is assigned here if the passed string does not match the regular expression

The simplest solution seems to be to allow the variable $operator to be NULL

I my case this error was occurred when in custom code are passing the substitution placeholder to the numeric filter.

      $selection_handler_options = [
        'view' => [
          'view_name' => 'view_name_id',
          'display_name' => 'display_name_id,
          'arguments' => ['substitution', $entity->id()],
        ],
      ];
zserno’s picture

I ran into this error after upgrading a Drupal 9 site to 10.

I agree with #16: originally this site started out as an early Drupal 8 site. Somehow, one of the views had its pager's offset configuration unset. Setting the offset value to '0' fixed the issue for me.

eugene bocharov’s picture

I would suggest just to allow NULL for \Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator

@@ -108,7 +108,7 @@ abstract class ArgumentPluginBase extends HandlerBase implements CacheableDepend
   /**
    * The operator used for the query: or|and.
    */
-  public string $operator;
+  public ?string $operator;
joey-santiago’s picture

Agree with #26...

In my case, the error is thrown in a view that is embedded in a webform. The view arguments are passed by ajax and come from an element that is on the same page of the view. So in some conditions the view gets NULL being passed as an argument. In this case, somehow NULL gets to the operator. Implemented the suggestion suggested in #26 and it seems to work fine.

andikanio’s picture

StatusFileSize
new605 bytes

This is the patch based on #26 which works for my case.

hieuntnguyen’s picture

StatusFileSize
new1.09 KB
ovanes’s picture

Hello all,

Just to inform you, I've locally tested the patch from comment #28 and can verify that it is effective.

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

The setup is as follows:
- Drupal version: 10.1.4
- PHP Version: 8.1.18

Thank you.

didebru’s picture

We are also facing this issue patch #29 and #28 solved it.

I know this needs steps to reproduce but this property can be null and needs to be treated like that. Either by this patch or by allowing it to be null.

campbellt_’s picture

Patch #28 also solved the issue on our site.

fernly’s picture

+1 on patch #28, fixes the problem.

jonraedeke’s picture

Patch #28 and #29 both fix my issue. I'm not sure which is the best approach.

In my case, default contextual filters that happen to be null because the field was not required were causing this fatal error.

isa.bel’s picture

Patch #29 worked for me.

jcontreras’s picture

#29 worked for me. Thanx!

kreatil’s picture

#29 fixed the issue for me too.

kreatil’s picture

Status: Postponed (maintainer needs more info) » Reviewed & tested by the community

After a thorough review of the available information and attempts to understand this issue, I have concerns regarding the feasibility of reproducing the error, particularly since it seems to predominantly affect sites that have been upgraded from Drupal 8 or Drupal 9.

From what I've gathered, this error does not readily manifest in fresh installations of Drupal 10, which suggests that it might be tied to specific configurations or legacy data from older versions. This makes it challenging to replicate the conditions necessary to observe the error in a controlled environment.

Patch #29 does not add any new functionality, but merely checks whether $break->operator is set. Which appears to have successfully resolved the issue for many who have applied it, indicating its effectiveness in addressing this specific error.

Given these circumstances, I would like to propose a consideration for the integration of Patch #29 into core.

rob230’s picture

I have this error in a view which is using search_api.

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 1310 of modules/views/src/Plugin/views/argument/ArgumentPluginBase.php).

Drupal\search_api\Plugin\views\argument\SearchApiStandard->fillValue() (Line: 160)
Drupal\search_api\Plugin\views\argument\SearchApiStandard->query() (Line: 1126)
Drupal\views\ViewExecutable->_buildArguments() (Line: 1282)
Drupal\views\ViewExecutable->build() (Line: 392)

It's a very simple view which just does a fulltext search and shows the search excerpt. It broke when upgrading to Drupal 10. Whilst patch #29 would fix it, I agree with #38 that the fix only treats the symptom but not the root cause. So I investigated some more and in my case I found it was due to a contextual filter which had a default value of a fixed value of an empty string, but which had a validator for "Content" that looked for the ID of a node and handles multiples with "One or more IDs separated by , or +".

So people should check the contextual filters and make sure an impossible situation hasn't been created where a blank value is validated. One could argue that the validator shouldn't cause an exception on the site, however Drupal had been instructed to do something that didn't make sense, and simply ignoring it would mean the mistake is never found.

peter pulsifer’s picture

I had the same error as @ovanes, #30.

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 1303 of core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php).

This came from a view that takes a node id as a contextual argument, and it was being passed a random argument. When that argument included a non-numeric character, the error was triggered.

The cause of all this is the function breakString($str) in HandlerBase.php, which returns $operator as a string ('or' or 'and') when $str is alphanumeric and returns it as null when $str contains a non-alphanumeric character. I don't see the null return behavior documented anywhere; is it important to retain it? If not, the fix would be to change breakString() so it always returned a string for $operator - either 'and', which seems to be the default, or simply a null string, which would at least not cause TyeError issues.

I solved my problem by adding numeric validation to the contextual argument, but it seems like this should be cleaned up.

giuseppe87’s picture

I confirm the description of #39, with a similar situation:

I have a site started with Drupal 8, upgraded at some point to 9 and recently to 10.

On that site, there are some views of nodes with a contextual filter "content:ID", whose validator is "content type". It accepts one or multiple ids.

Those views are sometimes rendered programmatically, with the following code:

    $view = [
      '#type' => 'view',
      '#name' => $view_id,
      '#display_id' => $view_display,
      '#arguments' => $nids,
    ];
    $render = $this->renderer->render($view);

Where $nids sometimes may be an empty string - if no value is provided.
Until Drupal 9 this worked, but since Drupal 10 I had to put a check in case $nids is an empty string, because that would cause the error.

I also agree with #39 considerations: it was poor custom code that made Drupal doing something that should not be possible.
However, hiding that error would also hide eventual code\configuration errors without having a way to realize it.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

The patch here should be converted to an MR - and the last run on this issue had over a thousand test failures (probably a DrupalCI issue but we need a recent run on gitlab CI to commit it).

Additionally, this approach is just hiding the error without informing admins. I think we could log an error with the view name and display to tell the site admin something is wrong - maybe they can resave or rebuild the view or something.

dimitriskr’s picture

Assigned: shrikant.dhotre » Unassigned
Issue tags: -Needs steps to reproduce
StatusFileSize
new2.25 KB

I came up with this error today. In my project, we're calling views programmatically and some contextual filters started with a comma ",". In 9.5.x this was no problem, but we upgraded to 10.2 few days ago and got this. I first tried to re-save the view but didn't have any changes in the config.

I think we need to add a check that if is_null($break->operator), it means the filter values used to call the view are invalid and return that error to the user

Also, for the actual problem, if the property 'break_phrase' of the 'nid' argument is set to 'true', it will produce this error when using invalid arguments. The 'break_phrase' property in the UI is "Allow multiple values" in "More" when editing a contextual filter

I'm uploading a test patch that reproduces this error

dimitriskr changed the visibility of the branch 11.x to hidden.

dimitriskr changed the visibility of the branch 10.1.x to hidden.

dimitriskr’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

I'm moving the work to https://git.drupalcode.org/project/drupal/-/merge_requests/6774. The other MRs can be closed

pcambra’s picture

Status: Needs work » Needs review
dimitriskr’s picture

@pcambra did you maybe forget to push to the issue branch? Otherwise there's nothing to review

pcambra’s picture

Status: Needs review » Needs work

sorry, I was testing patch in #29 (which solves my issue) and changed this by mistake

dimitriskr’s picture

OK. but this patch just hides the error, not solving the actual issue. The problem originates from the fact that an argument in your view is invalid

shrikant.dhotre changed the visibility of the branch 3353786-typeerror-cannot-assign to hidden.

pameeela’s picture

Title: TypeError: Cannot assign null to property Drupal\views\Plugin\views\argument\ArgumentPluginBase::$operator » Error calling a view with invalid numeric argument, e.g. "Content: ID"
Issue summary: View changes
Issue tags: +Bug Smash Initiative

Updated title to be more descriptive.

pameeela’s picture

Title: Error calling a view with invalid numeric argument, e.g. "Content: ID" » Error calling a view with non-numeric argument, e.g. "Content: ID"
bfuzze9898’s picture

Patch #28 alone works for me. Thanks.

guiu.rocafort.ferrer made their first commit to this issue’s fork.

guiu.rocafort.ferrer’s picture

Status: Needs work » Needs review

In my case, i had a string value with the character "/" on it, so the regular expression was not picking it up and causing the operator to be NULL.

I think the main issue comes from HandlerBase::breakString, which should allways return an operator, even if none of the regular expressions apply. In that case, the value returns as an empty array, so i believe it should be safe to return any of the operators for that matter.

This issue does not only happen with numeric arguments, but also with string arguments that contain characters not present in the regular expression, so the fix should not focus on the numericArgument plugin.

Please review the new branch '3353786-breakstring-add-default-operator'. Setting the issue as needs review.

PD: Another different issue, which will require a different issue to address, is broadening the regular expression to allow for more special characters to be present, like the one that was causing the issue for me "/".

smustgrave’s picture

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

Issue summary does not match the current solution.

Quick glance though not sure that's the approach. The doc does not mention it could be null. Seems like if it becomes null that's the bug.

dimitriskr’s picture

Yes exactly
It becomes null because the argument starts with an invalid character and since the "Use multiple values" option is enabled, it tries to filter it for "+"(OR) and ","(AND) characters, anything else is invalid.

I think we need to re-evaluate the scope of the issue, per #57 comment, to filter out invalid characters in arguments (as an option in UI maybe, to cover all use-cases), and keep this feature disabled, in order not to break anything during updates. If this option is disabled, we show an error to the user, as is the current proposed resolution, and if it's enabled, no harm done

guiu.rocafort.ferrer’s picture

I believe that the fix should be merged, so the breakString does not return a NULL operator, which causes a fatal php error, and then we can create a follow-up issue to discuss the acceptable characters in the provided values, as #59 suggests.

das-peter’s picture

Here my 2 cents:
The operator class variable is part of ArgumentPluginBase - which as the name says is a base class.
As it behaves now, it is completely up to whoever inherits ArgumentPluginBase what to do if the operator is not set.
And I think this should be the case - introducing a default value would change the base to something opinionated which would be quite a change.
The same basic principle seems to apply to HandlerBase::breakString() - if it can't decide what to do based on the input given - it essentially declares that by returning NULL for the operator.
As such I'd advocate to allow NULL as value for the operator class variable.
However, I'd change the current MR code slightly to :

public ?string $operator = NULL;

Simply because this would fly with phps declare(strict_types=1); already and will never produce an exception like Typed property ArgumentPluginBase::$operator must not be accessed before initialization and NULL will still pass all potential isset() tests.


Now the mentioned issues with HandlerBase::breakString() not properly handling some strings is kind of unrelated to this issue - there are other issues related to this e.g. #672606


My use case is a combination of Search API & Views Reference where tokens are used to build views arguments. Which is not related to any broken D9 D10 upgrade path but simply how one can configure a views setup.

robert-arias made their first commit to this issue’s fork.

robert-arias changed the visibility of the branch 3353786-views-breakstring-regex-issue to hidden.

robert-arias’s picture

I also happened to face this issue. In my case, I was passing in some arguments to the view through code, but I got that error. After reading this thread, I started to make some tests. In my case, the value and operator were not being processed correctly, that meant the argument value I was passing into the view was not entering either if in HandlerBase::breakString. I was passing in 3500 IDs, which, as a concatenated string, became a 21.763-length string. If I cut back in the length, from 3500 ids to 3200, the string became around <20.000 length. For the first long string, there was no match, for the second shorter string, however, there was a match. So, everything pointed out to the length of the string. I updated the regex to be a bit more performant, and now my use case does work.

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.