Problem/Motivation

After deleting a custom token, it still appears when retrieving the list of available tokens. This happens because the token cache is not invalidated upon deletion, unlike when a token is created or updated. As a result, stale tokens remain accessible until the cache is manually cleared or expires, which can cause confusion and unintended behavior.

Steps to reproduce

1. Create a custom token using the custom tokens module form.
2. Verify that the token appears in the list of available tokens.
3. Delete the custom token.
4. Retrieve the list of tokens again (e.g., via `hook_token_info()`, `Token Browser` or similar methods).
5. Observe that the deleted token still appears due to the cache not being invalidated.

Proposed resolution

Invalidate or clear the token cache when a custom token is deleted, similar to the cache invalidation behavior performed when creating or updating a token. This will ensure that deleted tokens are immediately removed from any token listings and prevent stale data from persisting.

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

iheb.attia created an issue. See original summary.

iheb.attia’s picture

Status: Active » Needs review
anybody’s picture

Status: Needs review » Needs work

@iheb.attia I think instead of the function it should use the TokenService and ->clearCache() using dependency injection:

$this->tokenService->clearCache();
grevil’s picture

@anybody, yea I think that's better. Especially since "token_clear_cache()" is defined in the token contrib module.

But "token_clear_cache()" is also used in other places inside token custom... makes me wonder, whether this module is even compatible with tokens core. Probably not.

grevil changed the visibility of the branch 3531812-deleting-custom-tokens to hidden.

grevil changed the visibility of the branch 3531812-deleting-custom-tokens to active.

grevil’s picture

Status: Needs work » Reviewed & tested by the community

Hm there is no "clearCache" on the token service. only "resetInfo" and I am not 100% sure it does the same thing.

Furthermore, there is no replacement defined in this CR: https://www.drupal.org/node/1973488

Since we are already using "token_clear_cache()" inside the normal "TokenCustomForm", I'd say we merge this as is. If it causes any trouble in the future, we can look further into it.

  • grevil committed 023ef03a on 8.x-1.x authored by iheb.attia
    #3531812: Deleting custom tokens does not invalidate token cache
    
grevil’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

anybody’s picture

Since we are already using "token_clear_cache()" inside the normal "TokenCustomForm", I'd say we merge this as is. If it causes any trouble in the future, we can look further into it.

Fine with that!

Status: Fixed » Closed (fixed)

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