Is it possible to combine token replacement and string?

Created on 4 August 2022, over 2 years ago
Updated 22 August 2024, 3 months ago

Problem/Motivation

With the approach in ✨ Promote use of Token Or module to make tokens more flexible Fixed it was possible to create a metatag value like this:

[node:field_hero_image:entity:field_media_image:og_image]||[site:base-url]/themes/custom/my_theme/image.png

Is this still possible with token_or?

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium JeroenT πŸ‡§πŸ‡ͺ

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany captain hindsight

    Thought about the same but in a different way.

    For the example above

    [node:field_hero_image:entity:field_media_image:og_image|"/themes/custom/my_theme/image.png"]
    

    why not extend it like that:

    [node:field_hero_image:entity:field_media_image:og_image|site:base-url"/themes/custom/my_theme/image.png"]
    

    I think this could be achieved by adding another condition in TokenOrTokensPreAlter::tokensPreAlter:

    diff --git a/src/TokenOrTokensPreAlter.php b/src/TokenOrTokensPreAlter.php
    index f57cace..0d34def 100644
    --- a/src/TokenOrTokensPreAlter.php
    +++ b/src/TokenOrTokensPreAlter.php
    @@ -44,10 +44,16 @@ class TokenOrTokensPreAlter {
             $sub_tokens = explode('|', $match_clean);
    
             foreach ($sub_tokens as $sub_token) {
    +          $sub_token_matches = [];
               if (substr($sub_token, 0, 1) === '"' && substr($sub_token, -1, 1) === '"') {
                 // This is a string replacement.
                 $result = substr(substr($sub_token, 1), 0, -1);
               }
    +          elseif (preg_match('/([^"]*)"(.*?)"/', $sub_token, $sub_token_matches)) {
    +            // This is a token replacement with an appended string.
    +            $result = $this->token->replace('[' . $sub_token_matches[1] . ']', $data, $options);
    +            $result .= $result ? $sub_token_matches[2] : '';
    +          }
               else {
                 // This is a token replacement.
                 $result = $this->token->replace('[' . $sub_token . ']', $data, $options);
    
  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    @captain hindsight I think your solution could work. Feel free to create an issue fork to test that out.

  • πŸ‡»πŸ‡³Vietnam zipme_hkt

    @captain hindsight, with your case you can look this module https://www.drupal.org/project/token_eca_alter β†’

    You can easy alter, extend exist token data.

  • πŸ‡©πŸ‡ͺGermany captain hindsight

    Thanks, I discovered there is a hook `hook_tokens_pre_alter` which fullfills all my needs to do that.

    I didn't notice that at the beginning because there is no api.php and I discovered it only by inspecting the code of token_or.

    @pianomansam Maybe you could create a `token_or.api.php` ?

  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    @captain hindsight Always open to pull requests!

Production build 0.71.5 2024