Deprecated function: preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\token_or\TokenOrTokensPreAlter->tokensPreAlter() (line 35 of modules/contrib/token_or/src/TokenOrTokensPreAlter.php).

Issue fork token_or-3337496

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

allandk created an issue. See original summary.

binoli lalani’s picture

Assigned: Unassigned » binoli lalani

binoli lalani’s picture

Assigned: binoli lalani » Unassigned

Hello,

I have created MR to fix this warning. Please review.

Thank you!

binoli lalani’s picture

Status: Active » Needs review
pianomansam’s picture

Title: From my log » Deprecated function: preg_match_all(): Passing null to parameter #2
Version: 2.1.0 » 2.x-dev
Status: Needs review » Needs work

Let's get some tests around reproducing and solving this.

-  public function tokensPreAlter(&$text, $data, $options) {
+  public function tokensPreAlter(string &$text, $data, $options) {

@binoli-lalani I'm not sure this is the best approach. How can we be sure that the code upstream only passes in a string? In fact, this error report indicates that it's passing in NULL.

fromme’s picture

StatusFileSize
new113.79 KB
new811 bytes

I also faced this problem:
Deprecated function error

And i think the best solution is to check if $text is string and then find matches.
Attached patch here and updated the issue branch.

fromme’s picture

Status: Needs work » Needs review
pianomansam’s picture

Status: Needs review » Needs work

@fromme thank you for your report and patch. I'm having a hard time reproducing this. Before a patch can be accepted, we need a test that can reproduce and test that the patch fixes the issue. Can you at least help me reproduce it?

fromme’s picture

@pianomansam, sure.
In my case it happens while using quiz module - on add/edit forms of quizes and questions.
Enabled modules of quiz - https://prnt.sc/QvqqrdtJnKD_

I tested with:
- drupal 10.0.10;
- quiz 6.x-dev#6d6f65d9 + patch for d10 (https://www.drupal.org/project/quiz/issues/3356316)
- token_or 2.1.0

Let me know if i should attached detailed settings of quiz module.

pianomansam’s picture

@fromme testing with the quiz module sounds pretty involved, so I'm not sure it will work here. Out of curiosity, what's the token you're using that gives the error?

  • pianomansam committed f332b15e on 2.x
    Issue #3337496 by fromme, pianomansam: Deprecated function:...
pianomansam’s picture

Status: Needs work » Fixed

I was able to reproduce this issue. Apparently, some modules send NULL into the token service 😒

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

bryanmanalo’s picture

StatusFileSize
new540 bytes

I was able to replicate this issue.

This is the error that I am getting.

Deprecated function: preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\token_or\Token->scan() (line 35 of modules/contrib/token_or/src/Token.php).

Adding a patch.

pianomansam’s picture

@bryanmanalo this issue was resolved in release 2.1.1. Are you still experiencing it?

bryanmanalo’s picture

Yes, I experienced the issue in another part of the codebase. However, it could just be a combination of using this and menu_token module.

pianomansam’s picture

Status: Closed (fixed) » Needs work

@bryanmanalo that's unfortunate. Does your patch resolve this issue for your site? If so, we'll need to determine what kind of value is being pass in as $text. I see your patch uses empty($text) while the current release uses !$empty so there must be something that empty catches that false does not. So, in order to get this resolved we need to do the following:

  1. Identify the value of $text
  2. Write a test that fails
  3. Fix the logic so that the test passes
liam morland’s picture

Adding a type declaration or cast could detect or fix this.