Score:1

How do I make a subgroup of custom tokens under the Nodes top level token group?

us flag

I have a few custom tokens to create which are based off of node context. At first I thought I could have them as their own top level group with code like this:

  $info['types']['mpep_policy'] = [
    'name' =>  t('MPEP Policy'),
    'description' => t('mpep policy tokens'),
    'needs-data' => 'node',
  ];
  $mpep_customs = ['Liability:Each Occurence', 'Liability:General Aggregate', 'Business Description', 'IM Premium', 'Policy Documents'];
  foreach ($mpep_customs as $mpep_custom) {
    $info['tokens']['mpep_policy'][$mpep_custom] = [
      'name' => $mpep_custom,
    ];
  }

This gives me the ideal situation of having "MPEP Policy" show as a top level token group; but, when creating token values there is no node context available. I read a tutorial suggesting that setting 'needs-data' to node would do this but it does not. I suspect this is from the module which is using token replacements (FillPDF). It verifies that I am passing in entities that match the type of token being used.

So I think the best UI for the token browser that I can expect to work is to have a subgroup called MPEP Policy listed under Nodes:

Nodes
- MPEP Policy
-- Liability:Each Occurence
-- Liability:General Aggregate

This code seemed like it should be sufficient for that:

  $mpep_customs = ['Liability:Each Occurence', 'Liability:General Aggregate', 'Business Description', 'IM Premium', 'Policy Documents'];
  foreach ($mpep_customs as $mpep_custom) {
    $info['tokens']['node']['mpep_policy'][$mpep_custom] = [
      'name' => $mpep_custom,
    ];
  }

but this only gives me a blank label under Nodes (with no description) and the token [node:mpep_policy]. But it is not expandable to show my custom tokens.

What am I missing here?

cn flag
You're probably missing the magic in `hook_tokens` which extends tokens - see [`hook_tokens`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%21token.api.php/function/hook_tokens/9.2.x), the examples down the bottom of using the `findWithPrefix` method on the token service
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.