Right now there is no way to limit the list of tokens using '#theme' => 'token_tree_link'. As far as I can tell.
Based on reading the code it seems to me that one of these two options should work
$form['tokens'] = array(
'#theme' => 'token_tree_link',
'#token_types' => array('node', 'user'),
);
or
$form['tokens'] = array(
'#theme' => 'token_tree_link',
'#options' => array('token_types' => array('node', 'user')),
);
But neither does.
I believe this is because of this line:
$variables['options']['query']['options'] = array_intersect_key($variables, $info['token_tree']['variables']);
When theme_token_tree_link is called with 'token_types' in the $variables array it's removed because token_theme() doesn't declare this as a possible value.
When it's called with $variables['options']['token_types'] this isn't added to the generated querystring because the code above uses $variables when it should use $variables['options'].
There are two ways to fix this problem:
Update token_theme and add the 'token_types' => array() key to the token_tree_link definition and then this will work:
$form['tokens'] = array(
'#theme' => 'token_tree_link',
'#token_types' => array('node', 'user'),
);
Note: If you do this we should also add things like 'global_tokens', and all the other 'theme_token_tree' keys as well I think.
Or you can change this:
$variables['options']['query']['options'] = array_intersect_key($variables, $info['token_tree']['variables']);
to this:
$variables['options']['query']['options'] = array_intersect_key($variables['options'], $info['token_tree']['variables']);
And this will work
$form['tokens'] = array(
'#theme' => 'token_tree_link',
'#options' => array('token_types' => array('node', 'user')),
);
Which one is better? To me the 1st solution, updating the 'token_tree_link' definition seems to be the most user friendly.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | 2289203-theme-token-tree-link-variables.patch | 1.12 KB | dave reid |
Comments
Comment #1
dave reidHrm, my intent was that you should be able to use theme_token_tree_link with the same parameters as theme_token_tree, but I see that isn't the case - D'OH! I think this patch resolves it.
Comment #2
eojthebravePatch works perfectly. Thanks for the quick fix Dave!
Comment #4
dave reidGreat! Thanks for such a great bug report!
Comment #6
sammuell commentedI just ran into this issue too and had already started to work on a patch (same approach as above) when I realized that this has been reported and fixed months ago.
I confirm that it works using the dev branch, this fix deserves to spread throughout the world ;-)
Please release a new version, v1.5 is more than one and a half years old.