0

I have a paragraph field that is a reference entity to the Customer Testimonial content type. A content editor can add the customer-testimonial paragraph and type the name of the testimonial. This will then display the text on the website.

I have made a paragraph--c15a-customer-testimonial.html.twig template file with the following code.

{%
  set c15a = {

    title: content.field_testimonial.entity.field_testimonial_titel,

  }
%}

<div class="component c15a">
    <div>

        <div>Testimonial title</div>

        <h2>{{ c15a.title }}</h2>

    </div>
</div>

Of course, this prints nothing. How can I get the fields from the content type I'm referencing in my paragraph?

1 Answer 1

2

The referenced entity is not built yet, so you can't access the formatted field content.

You can get the field value from the database by using the paragraph object:

{{ paragraph.field_testimonial.entity.field_testimonial_titel.value }}

This is unformatted, which might work for a title field.

If you want a formatted output better use a custom view mode for the referenced entity to configure the fields, or in this case one field, if this is all you want to display.

3
  • Your proposed path to get the value is not working, but this is: content.field_testimonial[0]['#node'].field_testimonial_titel.value ... why is that?
    – kevinius
    Apr 3, 2017 at 11:31
  • yes, this is the same node: paragraph.field_ref.entity == content.field_ref[0]['#node']. I would use the first one, because this also works if the field is not configured.
    – 4uk4
    Apr 3, 2017 at 11:39
  • Ok, it does work, i just forgot to replace content. with paragraph. ... thx...
    – kevinius
    Apr 3, 2017 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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