task: #3627991 Replace drupal_static() in token.module, deprecated in Drupal 11.5

Closes #3627991.

  • The menu link and term parent loaders move to two services, TokenMenuLinkHelper and TokenTermHelper, registered under their class name only. They keep the titles in their own properties for the request, so they need neither drupal_static() nor a memory cache bin (cache.memory only exists from Drupal 11.3), and run the same code from Drupal 10.3 to 12. token_menu_link_translated_title() moves to TokenMenuLinkHelper::getTranslatedTitle().
  • The helper methods take typed arguments (int $tid, string $langcode…), so the is_numeric() check on the term ID goes away.
  • TokenEntityHooks and TokenTokensHooks get the helpers through service closures, so the hook classes stay cheap to build for the many hooks that never use them.
  • token_menu_link_load_all_parents(), token_taxonomy_term_load_all_parents() and token_menu_link_translated_title() become deprecated wrappers around the helpers. They cast their arguments, and the term one still returns no parents for a non-numeric term ID.
  • token_clear_cache() is deprecated. Saving a date format or a field only changes which tokens exist, so the hooks, the token cache controller and the Drush cache clear now call Token::resetInfo() directly. The entity type mappings come from the entity type definitions and the parent titles only live for the request, so nothing that called it needed those reset. It also no longer resets token_book_link_load, which no function uses.
  • token_book_load_all_parents() is not converted, only deprecated: book tokens are only defined while the Book module does not provide its own (#3588324). Its two calls in TokenTokensHooks carry an inline PHPStan ignore with that reason.
  • A new taxonomy_term_presave hook clears the term parents, like the existing menu_link_content_presave one does for menu links. TaxonomyTest::testParentsTokenFollowsParentRenameInSameRequest() covers it: it fails on 8.x-1.x ([term:parents] still returns the old name after the parent is renamed) and passes with the change.

The phpunit (next major) failures come from Drupal 12 core changes outside this MR and belong in a separate issue.

Checked locally on Drupal 11.4: all kernel tests pass, and TaxonomyTest fails again when the term reset in TokenEntityHooks::taxonomyTermPresave() is removed; the hooks, the cache controller and the Drush callable each invalidate the token_info cache tag (checked with a throwaway kernel test, which fails when the hook's reset is removed); phpcs and PHPStan report nothing new.

AI-Generated: Yes (Claude Code was used to write the code and the test in this merge request and to draft this description. I reviewed the change before posting it.)

Edited by Frank Mably

Merge request reports

Loading
Loading