Problem/Motivation
On a Drupal 9 site with smart_date 3.6 and tokens 8.x-1.13 and the auto_entitylabel module enabled, some tokens end up empty.
This is because the Token module passes last.
Remplacement from Smart Date work correctly, but it then get equal to NULL because of the hook_tokens() from Tokens.
Steps to reproduce
- Create node type
- Add a smart date field (e.g. field_smartdates)
- Enable automatic label generation for this bundle with this pattern:
Astreinte [node:field_smartdates:0:format:compact] - Save a node: the token is removed/empty
Proposed resolution
Add a hook_module_implements_alter() to ensure Smart Date tokens are called last.
/**
* Implements hook_module_implements_alter().
*
* Move smart_date hook_tokens() implementation to the end of the list.
*/
function smart_date_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'tokens':
$group = $implementations['smart_date'];
unset($implementations['smart_date']);
$implementations['smart_date'] = $group;
break;
}
}
Remaining tasks
Review MR and merge if possible.
Issue fork smart_date-3433267
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
Comment #3
matthieuscarset commentedComment #4
mandclu commentedI'm a little confused, because this issue is against Smart Date 3.6 (which is no longer supported) but the MR seems to target 4.1, but without many recent commits.
I'm curious, could the change proposed here be a potential fix for #3396209: Updating to Token 1.13 breaks smart_date range tokens?
Comment #5
dieterholvoet commentedI encountered the same issue with a token in the format
[node:field_event_dates:0:value-format:date_only]. This fixes the problem. I'm using Smart Date 4.1.0-rc4.Comment #6
berdirSorry about breaking this, the token.module changed seemed like the only feasible long-term fix for those weird bugs that were reported there, and opens up the ability to move those token implementations into the respective core/contrib modules eventually.
Have you tried to implement an alter hook instead, which would allow you to alter the existing tokens instead of relying on hook order execution?
Comment #7
berdirAh, it's the tokens hook, not token_info. Not sure if a token alter then is feasible or not.
Comment #8
dalemoore commentedI was banging my head against the wall trying to figure out why these weren't working on a new site I'm working on, having the same issue I was having last year in No other token format available or accepted besides the default one, this fixes it for me for now. Trying to use the tokens in the URL for the same as before.
Comment #10
mandclu commentedMerged in. Thanks for finding this solution @matthieuscarset!