1
votes

In paragraph template I'm writing out paragraph field value like this:

{{ content.field_text['0']['#context'].value }}

and that works well. Now I want to support tokens while printing that text. I.e. if inside text I find "%current%" string I want to replace it with path to current page.

I'm also able to get path to current page like this:

{{ url('<current>') }}

And I can do search replace with static text like this:

{{ content.field_text['0']['#context'].value|replace({'%url%': "Dummy text"}) }}

and this also works.

But when I try to put it all together like this:

{{ content.field_text['0']['#context'].value|replace({'%url%': url('<current>') }) }}

it's not working. In that case I get error message:

Notice: Array to string conversion in twig_replace_filter() (line 486 of vendor/twig/twig/lib/Twig/Extension/Core.php).

And instead of real current page path token is replaced with "Array" string.

Also tried to get this to variable first and then put variable as second parameter for replace function, but that didn't work neither.

Any advice how to achieve this?

1
I cannot reproduce this with a basic example like twigfiddle.com/gidxa3. Are you sure that content.field_text['0']['#context'].value always is a string? - xabbuh
When I print it with {{ }} I get a string. I'm not sure if twig is doing some transformation/casting while printing. {{ dump(var) }} is also not working for me. :( - MilanG

1 Answers

0
votes

You are on the article template node details page

{{ entity.field_text.value }} to get the true raw value, includes tags and encoding. Example {{ node.body.value }}

Or

{{ content.field_text.0 }} to get the raw value minus tags and encoding. Example: {{ content.body.0 }}

if not working, you can dump(content) ?