Wrong token type used for taxonomy terms

Created on 27 October 2023, about 1 year ago
Updated 25 April 2024, 7 months ago

Problem/Motivation

In scheduler.token.inc, the tokens are generated using the entity type id. The entity type id is not always the same as the token type id, e.g. for taxonomy terms the token type id is "term", but the entity type id is "taxonomy_term". So the tokens provided by scheduler can't be used with taxonomy terms (and other entity types where this is the case).

Steps to reproduce

Try to use the tokens provided by scheduler with taxonomy terms.

Proposed resolution

If the contributed token module is installed, use the token.entity_mapper service provided by that module to try and get the correct token type id with the entity type id used as a fallback.

Remaining tasks

  • Make the test fail for existing code
  • Fix tokens.inc
πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany FeyP

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

Merge Requests

Comments & Activities

  • Issue created by @FeyP
  • Issue was unassigned.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    227 pass
  • πŸ‡©πŸ‡ͺGermany FeyP

    Attached is a patch against 2.x-dev.

  • Status changed to RTBC 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States Scott Weston

    Tested patch #2 on clean, Standard installation of Core version 10.2.0, Token 8.x-1.13, and Scheduler 2.0.1. The patch resolves the issue and clears an error message in Status Report: "Token types are not defined but have tokens - $info['types']['taxonomy_term']"

  • Status changed to Needs work 11 months ago
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Thanks FeyP for reporting this. I will replicate and check, but it does sound like a problem. I think the test coverage for tokens needs to be expanded to cover taxonomy terms, as this should have been tested for. I guess I skipped that when writing the taxonomy plugin.

    Another way to solve this would be to add a new property to the plugin, and let it default to the entity type, but define it specifically for taxonomy terms.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    227 pass
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    FeyP,
    Did you actually try using the scheduler tokens for a taxonomy term in a taxonomy description? Currently neither [taxonomy_term:scheduler-publish] or [term:scheduler-publish] get resolved and rendered as a date, when testing in the UI. But the testSchedulerTokenReplacement does cover taxonomy terms, amd the test passes with and without your change. I think it may be because we also need to make the equivalent change in that you did in

    Scott Weston,
    Thanks for pointing out

    an error message in Status Report: Token types are not defined but have tokens - $info['types']['taxonomy_term']

    I missed that entirely because it is displayed in the ordinary info section. It would be helpful if is was in the 'warnings' section like other mis-configurations and problems.

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    I have updated the test to use the token type not the entity type. This next commit should fail for the taxonomy tokens test.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    224 pass, 1 fail
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    As intended, we now get the single test failure

        Drupal\Tests\scheduler\Functional\SchedulerTokenReplaceTest::testSchedulerTokenReplacement
        with data set "#taxonomy_term" ('taxonomy_term', 'test_vocab')
        Scheduler tokens replaced correctly for  format.
        Failed asserting that two strings are equal.
        --- Expected
        +++ Actual
        @@ @@
        -'Publish on: Wed, 01/03/2024 - 04:42. Unpublish on: Wed, 01/03/2024 -
        05:42.'
        +'Publish on: [term:scheduler-publish]. Unpublish on:
        [term:scheduler-unpublish].'
    

    The above shows that when correctly using the token type in the test, the token is not rendered. Will be a simple fix.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    3 pass, 29 fail
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    3 pass, 29 fail
  • Status changed to Fixed 11 months ago
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Tests pass on gitlab pipeline. The fails on drupal.org are deprecations on 3rd-party dependency modules, see πŸ“Œ Tests fail with "Using a translatable string as a category for field type is deprecated" Active
    Therefore merging this.
    Thank you @FeyP for reporting, and for the first patch.

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

  • heddn Nicaragua

    This puts a hidden dependency on token.module. Or at least, my kernel tests (which didn't have token required previously) now require me to add token. Because otherwise,

    Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "token.entity_mapper".

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Oh. There is a comment

    // This function should only get executed if the Token module is enabled,
    // therefore we can assume the token.entity_mapper service exists without
    // having to check for it.
    

    but that is clearly wrong if your tests break. Can you open a new issue please?

Production build 0.71.5 2024