0

This question already has an answer here:

I'm using Drupal 8.5.4 and Bootstrap Paragraphs. I have a custom module foo and a custom paragraph type bar defined within. In the template for bar, I use kint to inspect the content of the variable. It turns out it has 17 properties, and none of them are the value of the field. The first 14 of them are in core/lib/Drupal/Core/Field, L#89-102. The last 3 are 0, #cache, and weight.

Digging into the object, I found the field value in content.bar.0['#context'].value. Obviously using this method to access a simple field value is janky as hell, and I would prefer not to have to do this every time.

Two questions:

  1. Is there a simpler way to access the field value? I don't mind using a twig filter if need be
  2. Is there a way to configure my paragraph types to pass the context to the template, rather than all of the field data?

marked as duplicate by No Sssweat, leymannx, mradcliffe, kiamlaluno Nov 26 '18 at 11:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You could preprocess the paragraph entity for the variable – Kevin Nov 22 '18 at 0:55
0

You are referring to paragraph--bar.html.twig? Then this depends on what you want to do with the value.

Render the field value:

{{ content.field_example }}

Write code with it:

{% if paragraph.field_example.value == 'foo' %}
  <p>The field value is "foo".</p>
{% endif %}

See this blog post for node.html.twig, which applies here as well, only that the entity is paragraph. instead of node.: https://www.md-systems.ch/de/blog/techblog/2017/02/20/twig-and-entity-field-api-or-how-to-take-control-of-nodehtmltwig

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