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.

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

ro-no-lo created an issue. See original summary.

ro-no-lo’s picture

StatusFileSize
new20.98 KB

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.

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

scott_euser’s picture

Version: 2.0.0-alpha4 » 2.0.x-dev
Status: Active » Needs review

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.

scott_euser’s picture

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.

  • scott_euser committed c9a8cabd on 2.0.x
    Issue #3462623 by scott_euser, ro-no-lo: Token gets replaced with "Array...
scott_euser’s picture

Status: Needs review » Fixed

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

ro-no-lo’s picture

Thank you.

Status: Fixed » Closed (fixed)

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