1

I'm adding fields to array:

{% for item in content.field_notes | field_target_entity %}
   {% set field_component_pages = item.field_component_pages|view|number_format %} 
{% endfor %}

In my scenario the value of item.field_component_pages|view is equal "16", but when I convert it to number with number_format I get value of 1.

I presume the item.field_component_pages|view is not a real value. How can I access field field_component_pages correctly in this case?

0

You can get it form the value. something like item.field_component_pages.0.value properly works for you

{% for item in content.field_notes | field_target_entity %}
   {% set field_component_pages = item.field_component_pages.0.value|number_format %} 
{% endfor %}
  • The output is Warning: strip_tags() expects parameter 1 to be string, array given in and returns 0 – adpo Feb 15 at 13:28
  • @adpo can you please tell me what is the response of {{ dump(item.field_component_pages) }} – Yusef Feb 16 at 12:41
  • @adpo , ok , then try {{ dump(item.field_component_pages|view) }} – Yusef Feb 16 at 17:17
  • @adpo would you please share a screenshot of this fields ( while you are not able to dump it) . I should know the type and field settings somehow – Yusef Feb 16 at 18:50
  • @yosef Thank you for you support. I have decided to dump this value from hook_preprocess_node. and tthe correct value is item.field_component_pages.0.value – adpo Feb 17 at 7:44

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.