Error calling a view with non-numeric argument, e.g. "Content: ID"

Created on 12 April 2023, over 1 year ago
Updated 4 September 2024, 2 months ago

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

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Views 

Last updated about 18 hours ago

Created by

🇮🇳India shrikant.dhotre

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • Issue created by @shrikant.dhotre
  • Status changed to Postponed: needs info over 1 year ago
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India shrikant.dhotre

    Found the solution in null operator

  • Status changed to Needs work over 1 year ago
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 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.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,202 pass
  • @shrikantdhotre opened merge request.
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,202 pass
  • 🇦🇺Australia dpi Perth, Australia

    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 to naive, we need to be using narrower type checking.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,202 pass
  • Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Not currently mergeable.
  • @shrikantdhotre opened merge request.
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,202 pass
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,202 pass
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,204 pass
  • Status changed to Postponed: needs info over 1 year ago
  • 🇳🇱Netherlands Lendude Amsterdam

    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.

  • 🇮🇳India shrikant.dhotre

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

  • 🇮🇳India shrikant.dhotre

    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

  • 🇳🇿New Zealand quietone

    @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.

  • 🇮🇳India shrikant.dhotre

    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

  • First commit to issue fork.
  • last update over 1 year ago
    29,884 pass
  • @lysenko opened merge request.
  • last update over 1 year ago
    29,885 pass
  • 🇺🇦Ukraine Lysenko Ukraine, Lutsk

    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

  • 🇭🇺Hungary zserno

    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.

  • 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;
    
  • 🇫🇮Finland joey-santiago

    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.

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

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7 updated deps
    last update about 1 year ago
    Custom Commands Failed
  • last update about 1 year ago
    30,415 pass
  • 🇧🇬Bulgaria ovanes Sofia

    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.

  • 🇩🇪Germany DiDebru

    We are also facing this issue patch #29 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.

  • 🇦🇺Australia campbellt_

    Patch #28 also solved the issue on our site.

  • 🇧🇪Belgium fernly

    +1 on patch #28, fixes the problem.

  • 🇺🇸United States jonraedeke

    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.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    Patch Failed to Apply
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,686 pass
  • 🇧🇷Brazil isa.bel Balneário Camboriú

    Patch #29 worked for me.

  • 🇺🇸United States jcontreras

    #29 worked for me. Thanx!

  • 🇩🇪Germany kreatIL

    #29 fixed the issue for me too.

  • Status changed to RTBC 11 months ago
  • 🇩🇪Germany kreatIL

    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.

  • last update 11 months ago
    25,911 pass, 1,827 fail
  • last update 11 months ago
    25,922 pass, 1,809 fail
  • last update 11 months ago
    25,917 pass, 1,817 fail
  • last update 11 months ago
    25,927 pass, 1,821 fail
  • last update 11 months ago
    25,946 pass, 1,803 fail
  • 🇬🇧United Kingdom Rob230

    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.

  • last update 11 months ago
    Build Successful
  • last update 11 months ago
    25,983 pass, 1,822 fail
  • last update 10 months ago
    25,980 pass, 1,818 fail
  • last update 10 months ago
    25,981 pass, 1,839 fail
  • 🇺🇸United States peter pulsifer

    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.

  • 🇮🇹Italy Giuseppe87

    I confirm the description of #39:

    I have on a site started with Drupal 8, upgraded to 9 and recently to 10 a similar situation.

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

    That view is sometimes render 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 that was poor code custom that made Drupal doing something that should not be possible, but hiding that error would hide the mistake without having a way to realize it.

  • Status changed to Needs work 9 months ago
  • 🇬🇧United Kingdom catch

    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.

  • Issue was unassigned.
  • 🇬🇷Greece dimitriskr

    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' argument in the UI is "Allow multiple values" in "More" when editing a contextual filter

    I'm uploading a test patch that reproduces this error

  • 🇬🇷Greece dimitriskr

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

  • 🇬🇷Greece dimitriskr

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

  • Pipeline finished with Failed
    9 months ago
    Total: 170s
    #104483
  • 🇬🇷Greece dimitriskr

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

  • Pipeline finished with Failed
    9 months ago
    Total: 334s
    #104490
  • Pipeline finished with Failed
    9 months ago
    Total: 481s
    #104501
  • Pipeline finished with Failed
    9 months ago
    Total: 458s
    #106014
  • Status changed to Needs review 8 months ago
  • 🇪🇸Spain pcambra Asturies
  • 🇬🇷Greece dimitriskr

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

  • Status changed to Needs work 8 months ago
  • 🇪🇸Spain pcambra Asturies

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

  • 🇬🇷Greece dimitriskr

    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

  • 🇮🇳India shrikant.dhotre

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

  • 🇦🇺Australia pameeela

    Updated title to be more descriptive.

  • 🇺🇸United States bfuzze9898

    Patch #28 alone works for me. Thanks.

  • 🇪🇸Spain guiu.rocafort.ferrer Barcelona

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

  • Status changed to Needs review 3 months ago
  • 🇪🇸Spain guiu.rocafort.ferrer Barcelona

    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 "/".

  • Status changed to Needs work 3 months ago
  • 🇺🇸United States smustgrave

    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.

  • 🇬🇷Greece dimitriskr

    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

  • 🇪🇸Spain guiu.rocafort.ferrer Barcelona

    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.

  • 🇨🇭Switzerland das-peter

    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 🐛 Hyphens and forward slashes (-/) break Views contextual filters Needs work

    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.

Production build 0.71.5 2024