Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
token.inc contains procedural code that cannot be lazy-loaded. It should be converted to a utility or a service. It's small and simple enough to be a utility, and I have not yet seen a need to override the functionality. However, it depends on ModuleHandler, which is a service, and which makes the token API harder to test if it's not a service (with dependency injection) itself.
The conversion to either a utility or a service would be fairly straightforward, as it's mostly a case of find and replace and there are no behavioral changes needed.
Comment | File | Size | Author |
---|---|---|---|
#26 | token-inc-to-token-php.diff | 4.2 KB | alexpott |
#24 | drupal_1969540_07.patch | 76.58 KB | Xano |
#24 | interdiff.txt | 1.03 KB | Xano |
#22 | interdiff.txt | 839 bytes | Xano |
#22 | drupal_1969540_06.patch | 76.64 KB | Xano |
Comments
Comment #1
Dave ReidService probably makes sense since we'd want to inject the ModuleHandler and eventually caching as well.
Comment #2
XanoYep, that's what I realized: they're not mutually exclusive.
Comment #3
ParisLiakos CreditAttribution: ParisLiakos commentedtagging
Comment #4
XanoComment #6
Dave Reid- If we're using a service, we can't use static methods anymore. Needs to be all regular function calls.
- Needs to be registered at /Drupal::Token for shorthand and DX
Comment #7
XanoFixed all of the above, and the installation failure, and re-tagging.
Comment #9
XanoComment #11
Dave ReidUsing __FUNCTION__ here means that this static will be stored as 'info' which is not desired. We should manually use 'token_info' as the static key instead of __FUNCTION__ until we replace this with injected caching.
Comment #12
Dave ReidCrazy thought, what if instead of having to declare \Drupal::token() in every instance of hook_tokens and hook_tokens_alter(), what if we passed $this into the hook as well?
Comment #13
XanoInteresting. I'm not sure whether it will actually improve DX, as most implementations will need other services as well, but it should be worth checking out. We'll need to do this in a follow-up issue, though.
The patch fixes the drupal_static(__FUNCTION__) issue, and a problem in a Views test which was caused by naming collisions. I opted to refer to the token service using $token_service rather than $token.
Comment #14
XanoComment #15
XanoToken::info() should be explicitly made public.
Comment #16
XanoToken::info() is now public. No other changes.
Comment #18
Xano#16: drupal_1969540_04.patch queued for re-testing.
Re-testing, as the failed test passed locally and it seems like an unrelated issue.
Comment #19
ParisLiakos CreditAttribution: ParisLiakos commentedlooks very good to me:)
just one biggie and two minors
why not turn this into a class property? Then we could unit test it
should be public function
No need to prefix Drupal in procedural, non namespaced code
Comment #20
XanoThis patch addresses those issues. It adds Token:setInfo() and Token::resetInfo() methods, and renames Token::info() to Token::getInfo().
Comment #22
XanoComment #23
ParisLiakos CreditAttribution: ParisLiakos commentedThanks! looks really good now:)
we dont do this elsewhere in core.
protected $moduleHandler;
should be enough
same here..you can completely remove the @return
besides those this seems ready to fly, good job!
Comment #24
XanoComment #25
ParisLiakos CreditAttribution: ParisLiakos commentedComment #26
alexpottAttaching the diff with all whitespace changes removed of the new Token class and token.inc... all changes look good to me. And this patch completely converts all usages. Nice!
Committed c014519 and pushed to 8.x.Thanks!
Comment #27
XanoThe token API is a service.
Comment #29
XanoComment #30
xjmUntagging. Please remove the tag when the change notification task is completed.