This issue came up as I was testing the PR for #1439. Everything was working beautifully in the PR sandbox, but only if you are an experienced Drupal/Backdrop user, and you can thus anticipate certain quirks we have all gotten used to over the years...

After having clicked the "Browse available tokens" link and browsed through the tokens, when you click on a token to be used, you get this error/prompt:

Screen Shot 2020-02-20 at 4 09 49 am

I realize that in theory, the token browser can be used in any field in the same page (although it does not necessarily mean that the token set specified in a "Browse available tokens" link for one field can be used in other fields), but it is annoying to have clicked a link under a specific field, and then have this error message telling you to select a field.

If the field had focus prior to the token browser dialog being opened, then any token clicked on the dialog gets properly inserted into that field, without any error/prompt; but if the focus was elsewhere before clicking the link (such as the "enable" checkboxes in this case, which show/hide the image alt/title fields using #states), then you get this UX WTF.

Can we have it so that theme_token_tree_link() accepts a field ID as a parameter, so that once the "Browse available tokens" link is clicked, there's some JS triggered, which sets the focus to that field before opening the token browser?

GitHub Issue #: 
4316