How to attach token in a custom form field I have a custom form in Drupal 8. I need to attach tokens for fields of my form. Basically I want to add "Browse tokens" option for each field of my form.
token.module has various examples, for example this:

// Add the token tree UI.
$form['email']['token_tree'] = array(
  '#theme' =&gt; 'token_tree_link',
  '#token_types' =&gt; array('user'),
  '#show_restricted' =&gt; TRUE,
  '#weight' =&gt; 90,
);


This displays a link for tokens for the type user (and global tokens).

You can also use the #element_validate callback token_element_validate together with also adding #token_types to have it validated.