Problem/Motivation
When token->scan is called with null as a string, preg_match_all will throw a deprecated warning in PHP8+.
Steps to reproduce
Call token->scan(Null)
Proposed resolution
Deprecate calling token->scan with a non-string parameter. Add a string typehint in Drupal 11.
Remaining tasks
Change record
User interface changes
none
API changes
Deprecated loosely typed parameter in Drupal 10.
Typehint added in Drupal 11.
Data model changes
none
Release notes snippet
none
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 3267785-22.patch | 5.2 KB | alexpott |
| #22 | 20-22-interdiff.txt | 4.29 KB | alexpott |
| #20 | 3267785-20.patch | 931 bytes | darvanen |
Issue fork drupal-3267785
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
Comment #4
Christopher Riley commentedThank you for this patch, this issue was making me crazier than usual.
Comment #5
Christopher Riley commentedSpoke too soon. I applied the patch but when I go the dev page for node tokens it still kicks with an error this time. Time to go back to digging.
Comment #7
ciprian.stavovei commentedI also encountered this issue in multiple scenarios and first I tried creating/applying patches for each module that uses Token and was having this issue but in the end got to the conclusion that the best would be to fix the problem at the root and not in every place that method is used.
I tried the fix from the pull request from this issue and it fixed our problem. Before that I dag a lot to find where the
scan()orreplace()methods were being used and to prevent "NULL" from being passed but couldn't get to the bottom of all. So then I tried to fix it at the root and it worked fine and didn't create any other issue. I Added the fix from the PR also as patch so that people can easily apply it until this gets merged.I believe that it would be much better to get this fixed in core and not in every other module where it is being used.
Comment #8
damienmckennaThe Token::scan() API documentation specifically states that the first argument should be a string, not a NULL, so it should be up to other code to make sure the argument isn't a NULL.
Comment #9
scott_euser commentedSo really this is a symptom of another bug, that token_default_tokens_alter() in the Token module sometimes sends NULL to the token replacement service. In my case:
1. content type A has entity reference field to content type B
2. use token in metatag module to output [field_reference_example:entity:url] from content type A
3. open example of content type A where reference field is empty
4. error shown
\Drupal::service('token_default.manager')->replaceMissingTokensWithDefaults(...) possibly returns NULL replacements. Patch attached, but possibly this is still just solving a symptom rather than the problem.
If the others who experienced this issue can also confirm that you have Token contrib module in place, I think we can safely move this issue over to Token.
Comment #10
scott_euser commentedApologies, its Token Default not Token that was the cause of the problem for me. I created an issue for that here https://www.drupal.org/project/token_default/issues/3320681 - if others can confirm that that also stops the php notice for them, we can close this issue.
Comment #11
darvanenI agree that this issue originates elsewhere but don't close this issue, we can use it to implement strong typing on the scan() function. We'll need:
This might qualify as a task though, rather than a bug, I'll ask in #bugsmash.
Comment #12
darvanenVerdict's in, moving to task.
Comment #13
darvanenIt would seem I was wrong about a patch for Drupal 11, I'm not sure how that is being handled.
Here's a patch adding the deprecation. I don't *think* this needs a test but I could be wrong.
Comment #14
darvanenOops, bad copy/paste in previous patch. Let's try that again.
Comment #15
rahul.shindeLGTM Thanks @darvanen.
Comment #16
acbramley commentedIs this not relevant anymore? Seems like it would be needed?
Comment #17
darvanenThanks @acbramley, I did forget that, I also completely forgot the change record, here's a draft.
Comment #18
darvanenOops, this should work better. I have tested casting NULL to a string and it does work.
Comment #19
smustgrave commentedBelieve the change record needs to be in the trigger_error no?
Comment #20
darvanenGood point, the example I copied from didn't have one but you're right, that should be there.
Comment #21
smustgrave commentedThat was quick! Looks good to me.
Comment #22
alexpottWe should be adding test coverage of this deprecation because it contains some logic. I looked for existing test coverage of Token::scan() and I found core/modules/system/tests/src/Functional/System/TokenScanTest.php which is a pointless functional test - it can be a unit test - and I found a unit test \Drupal\Tests\Core\Utility\TokenTest - which sets up a token service for us to use in testing - so I moved everything there.
Comment #23
rohan-sinha commentedTested the Patch on #22 working fine for me, D10
Comment #24
catchCommitted f2e1dcd and pushed to 10.1.x. Thanks!
Comment #26
catchComment #28
darvanen@catch do we need to adjust 11.x now that it is available? Or is that something that happens later?Oh I see it's just an issue queue placeholder. How *do* we ensure 11 gets updated?