Problem/Motivation

With the approach in #3034248: Promote use of Token Or module to make tokens more flexible 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

Comments

JeroenT created an issue. See original summary.

jeroent’s picture

Issue summary: View changes
pianomansam’s picture

Category: Task » Feature request

This module doesn't offer that feature currently, but you're welcome to contribute it!

mxwright’s picture

@JeroenT as is pointed out in the module description, custom URLs or strings can be added by using quotes. I would assume something like this would work for you without the base-url token:

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

jeroent’s picture

@mxwright,

That's not exactly what I'm trying.

When you have a multisite or language negotation based on a domain and you want to use a token for e.g. the og:image (since that should be an absolute URL), the solution in #3034248: Promote use of Token Or module to make tokens more flexible supported something like this:

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

so the last part is a concatenation of a token and a string.

Anyway, since in my case it was language negotiation, I could work around this problem by translating the metatag config, but I think this is still a nice feature.

pianomansam’s picture

@JeroenT what's complicated about your feature request is that it involves supporting tokens within tokens. That's not trivial. That can easily run into issues like infinite loops. I haven't seen any other module attempt that ability. If you have seen that somewhere, please let me know so I can evaluate how it can be done.

captain hindsight’s picture

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);
pianomansam’s picture

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

zipme_hkt’s picture

@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.

captain hindsight’s picture

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` ?

pianomansam’s picture

@captain hindsight Always open to pull requests!