Numeric content inside square brackets within a node body recognised as a token with a type of int

Created on 29 May 2024, 7 months ago
Updated 3 July 2024, 6 months ago

Problem/Motivation

I have a piece of content that requires a time format to be entered within square brackets. Examples are [16:00] and [12:00].

This is being recognised as a token with a type of int, however Drupal\Core\Utility\Token->generate() requires the type to be a string.

Steps to reproduce

* Install Drupal 10.1.x
* Install and enable the token_filter module.
* Go to Configuration -> Content authoring -> Text formats and editors. Proceed to Basic HTML and tick "Replaces global and entity tokens with their values".
* Create a basic page with some content including [16:00] and [12:00].
* Save the page and view it.
* In your IDE anywhere within Drupal\Core\Utility\Token->generate(), set a breakpoint for your debugger.
* You'll note that the type is set as int. Example: `$type = {int} 16`.
* The expected result should be as a string. Example: `$type = "16"`.

Proposed resolution

In the generate() method of Drupal\Core\Utility\Token, set the type declaration as a string (as expected by the function).

Remaining tasks

* Create a failing test.
* Create patch.
* Ensure test is passing.

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
Token 

Last updated 5 days ago

No maintainer
Created by

🇦🇺Australia hoffismo Brisbane, Queensland

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

Merge Requests

Comments & Activities

  • Issue created by @hoffismo
  • 🇦🇺Australia hoffismo Brisbane, Queensland
  • 🇦🇺Australia hoffismo Brisbane, Queensland

    Adding initial patch but requires tests.

  • Use a pull request instead.

  • First commit to issue fork.
  • Merge request !8229Update file Token.php → (Open) created by immaculatexavier
  • Status changed to Needs review 7 months ago
  • Pipeline finished with Failed
    7 months ago
    Total: 877s
    #185922
  • Status changed to Postponed: needs info 7 months ago
  • 🇺🇸United States smustgrave

    The steps appear to require contrib modules. Is this reproducible in core?

  • Merge request !8281Set type declaration for token generate method → (Open) created by hoffismo
  • Pipeline finished with Failed
    7 months ago
    Total: 889s
    #190660
  • 🇦🇺Australia hoffismo Brisbane, Queensland
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
  • Status changed to Needs review 7 months ago
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    larowlan changed the visibility of the branch 3450635-numeric-content-inside to hidden.

  • Status changed to Needs work 7 months ago
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    @hoffismo can you provide the stack trace of the error you're seeing here?

    I'm guessing you've got a hook_tokens somewhere that is using strict types and that's the issue - core isn't strict-typed yet.

    We can't make the current change in the MR as it has BC implications.

    The correct way to do this would be to check the type of the $type and if it's not a string, then trigger a deprecation error and cast it.

    That would allow callers to be notified and updated before the next major.

    However, I think the fact its getting called with an integer type might be a mistake - I think the correct fix would likely be in `\Drupal\Core\Utility\Token::doReplace` where it is iterating

    foreach ($text_tokens as $type => $tokens) {
          $replacements += $this->generate($type, $tokens, $data, $options, $bubbleable_metadata);
          if (!empty($options['clear'])) {
            $replacements += array_fill_keys($tokens, '');
          }
        }
    

    I think here $type should be checked against valid types.

    E.g something like

    $info = $this->getInfo();
    foreach (array_intersect_key($text_tokens, $info)  as $type => $tokens) {
    

    That should prevent it even being called with `16`

  • 🇦🇺Australia hoffismo Brisbane, Queensland

    Thanks @larowlan. I've attached a stack trace. Yes that's correct, we do have a hook_tokens() in a custom module declaring the $type as string. I can confirm removing that does work to align it with core, since it's not strict typed as yet.

  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    Thanks @hoffismo - I think that probably unblocks you for now too 👍️

  • 🇳🇿New Zealand quietone

    This needs to be committed to 11.x (which is main) first and then applied to branches.

Production build 0.71.5 2024