0

I'm attempting to append two fields with some text in the middle. Using the changed date and a plain, core, text field, I can do something like this, and it works:

{{ node.changed.value|date('m.d.y') ~ ' | By, '}}{{ content.field_author_name.0 }}

However, if I attempt to do that in a single tag, the changed date, and the 'By' text is output, but I don't get the author name field.

{{ node.changed.value|date('m.d.y') ~ ' | By, ' ~ content.field_author_name.0 }}

Is that not valid twig syntax, or is there something else needed to get that to work in a single statement?

1

When using {{ content.field_author_name.0 }} it works because field is rendered with the twig.
The ~ operator converts all operands into strings and concatenates them. Given that content.field_author_name.0 is a render array you need point to the string value of this field. To do this you can inspect it with the devel or twig_vardumper module using dump(content.field_author_name.0). More about debugging twig templates: https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates#s-viewing-variables

Assuming that field_author_name is simple text field with the Plain text formatter, this should work:

{{ node.changed.value|date('m.d.y') ~ ' | By, ' ~ content.field_author_name.0['#context'].value }}

To display node creator name you can use this:

{{ node.changed.value|date('m.d.y') ~ ' | By, ' ~ node.uid.entity.name.value }}
| improve this answer | |
  • That clears it up for me. I had dumped that variable out and seen the context array and value field and assumed it should be pulling the value behind the scenes. Didn't realize the ~ operator was converting the value directly to a string before Drupal got ahold of it. – gcalex5 Nov 21 '19 at 14:18
0

To get the author name you can use {{ node.Owner.name.value }} or {{ node.uid.entity.name.0.value }}. Try with:

{{ node.changed.value|date('m.d.y') ~ ' | By, ' ~ node.Owner.name.value }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.