Modernize token system

Created on 16 July 2024, about 2 months ago
Updated 30 July 2024, about 1 month ago

What is the problem to solve?

I want to create this feature Allow to pass variables to aiprompt programatically Active and find tokens system old and difficult to work with (and documentation also very confusing scattered all over the place).
It could be modernized: tokens could be defined as plugins OOP based.

Providing tokens could be as simple as:

$children = [];

$node_token1 = new NodeToken();
$token->setName('node'); 
$token->setData($node);      // [node:...]
$children[] = $node_token1;

$node_token2 = new NodeToken();
$token->setName('another_node');
$token->setData($node2);    // [another_node:...]
$children[] = $node_token2;

$token_service = \Drupal::service('token');
$tokens = $token_service->getAllAvailableTokens();

$custom_token = new CustomToken();
$custom_token->setName('custom');
$custom_token->setDescription('This is custom wrapper token');
$custom_token->setData($children);    // tokens available as [custom:node:...] and [custom:another_node:...]
$tokens[] = $custom_token;

$build = [];
$build['tokens'] = [
  '#theme' => 'token_tree_link',
  '#tokens' => $tokens
];

$text = "This is [custom:node:title] and [custom:another_node:title]";

$build['tokenized_text']['#markup'] = $token_service->replace($text, $tokens, $options);

Note: Because I want to generate tokens from user defined arguments, I don't know in advance what those tokens will be and want to generate tokens in place in a simple way like example above. And in my particular case:
[arguments:some_custom_named_entity_token:...]

Question: What it takes to achieve the same with current (which is still Drupal 7 remnant) system?

Who is this for?

New, non-expert level and also expert level Drupal developers who would like to develop with more modern simpler API.

Result: what will be the outcome?

Example above looks easy, while at the same time, I don't know how do it with the old system. Old is probably very complex.
So outcome will be new API, easy to document, understand and work with.

It could probably be done as a module, and then later transferred to Drupal core.

How can we know the desired result is achieved

// TBD

Feature request
Status

Active

Component

Idea

Created by

🇱🇹Lithuania mindaugasd

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024