Token gets replaced with "Array"

Created on 19 July 2024, 5 months ago
Updated 13 August 2024, 4 months ago

Problem/Motivation

I set up a site_setting which shall contain just strings. I needed that because I need my frontend URL to be put somewhere. The Token to request that is this: [site_settings:site_settings--settings--0]. But when I use it, the replacement is the word "Array", because the str_replace function of the Token modul gets a 2 dimensional Array as replace argument.

I have no idea why but the site_settings.token.inc returns a array as a replacement for this token string. (see attachments)

In the end, the Token modul gets the token also as array. No idea if that is intended, but there is no 1 to 1 connection between the given token and the correct replacement. It's a string to array replacement, which might be the cause for the error.

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇩🇪Germany ro-no-lo

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

Merge Requests

Comments & Activities

  • Issue created by @ro-no-lo
  • 🇩🇪Germany ro-no-lo

    I might have found the cause, and "yay" it's a PHP fault.

    In the site_settings.token.inc is this line:

          // Break apart name.
          $name_parts = explode('--', $name);
          $group = $name_parts[0];
          $type = $name_parts[1] ?? NULL;
    
          // We may be deep into a multidimensional array.
          if (isset($name_parts[2]) && !empty($name_parts[2])) {
    

    but if the $name_parts[2] is a string with the number 0 in it like "0", PHP will say that !empty($name_parts[2]) is false. With the number "1" it says true.

    See https://onlinephp.io?s=s7EvyCjg5VJJLCpKrFSwVYjm5eJUKs4sSY0vTi0pycxLL1bSA...

    So I think the condition could be updated to this:

    if (isset($name_parts[2]) && (!empty($name_parts[2]) || $name_parts[2] === "0")) {
    

    after that change it worked for me as expected.

  • First commit to issue fork.
  • Merge request !32Resolve #3462623 "Token gets replaced" → (Merged) created by scott_euser
  • 🇬🇧United Kingdom scott_euser

    Thanks for the details! Yes I can see that being an issue. Can you try out this merge request and see if it solves for you. I did a tiny bit of code cleanup as well in there, but essentially took a slightly different approach checking if its got any string length otherwise also things like 0.0 would result in empty being true.

  • Status changed to Needs review 5 months ago
  • 🇬🇧United Kingdom scott_euser

    If you can confirm it also sorts it then I'll get this into the next release. We don't really use the token functionality on our sites at my agency so I don't have a good way to test.

  • Pipeline finished with Skipped
    5 months ago
    #233126
    • scott_euser committed c9a8cabd on 2.0.x
      Issue #3462623 by scott_euser, ro-no-lo: Token gets replaced with "Array...
  • Status changed to Fixed 5 months ago
  • 🇬🇧United Kingdom scott_euser

    Added better test coverage to confirm. Feel free to raise follow-up if any further issues.

  • 🇩🇪Germany ro-no-lo

    Thank you.

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

Production build 0.71.5 2024