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.

Comments

dave reid’s picture

Version: 7.x-1.5 » 7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new1.12 KB

Hrm, 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.

eojthebrave’s picture

Status: Needs review » Reviewed & tested by the community

Patch works perfectly. Thanks for the quick fix Dave!

  • Commit 0e505f5 on 7.x-1.x by Dave Reid:
    Issue #2289203 by Dave Reid | eojthebrave: Fixed theme_token_tree_link...
dave reid’s picture

Status: Reviewed & tested by the community » Fixed

Great! Thanks for such a great bug report!

Status: Fixed » Closed (fixed)

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

sammuell’s picture

I 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.