I am trying to access values of a content type nested deep into array. This is the actual scenario:
I want to customise the default search result page layout of Drupal 8 with additional field information. To know what fields are available for me to access I used {{ kint(item.value) }}
, which shows me this:
Fields I want to access are nested under #result>node>fields
like body
, field_category
etc.
Is there any way I can access them directly inside my twig template? I was guessing something like {{ item.value['node']['fields']['field_name']} }
? but this gave me nothing!
There is every possibility that the way I am thinking is not a suitable method or may be even not a method at all! I am still very new to Drupal.
All I want is to access desired fields nested deep below inside an array structure. Is there a kind of thumb rule or syntax to follow? This would be a great help if somebody please explain the process to access a specific element from inside a nested Drupal array.
Note: In order to customise layout I copied item-list.html.twig
file from /core/themes/stable/templaes/dataset
to my theme's templates folder.
Drupal version: 8.2.3
EDIT: Template file for search result (item-list.html.twig
)
<p class="righteous">Makes sure the page is being hit!</p>
{% if context.list_style %}
{%- set attributes = attributes.addClass('item-list__' ~ context.list_style) %}
{% endif %}
{% if items or empty %}
{%- if title is not empty -%}
<h3>{{ title }}</h3>
{%- endif -%}
{%- if items -%}
<{{ list_type }}{{ attributes }}>
{%- for item in items -%}
<li{{ item.attributes }}>{{ content.body }}</li>
{%- endfor -%}
</{{ list_type }}>
{%- else -%}
{{- empty -}}
{%- endif -%}
{%- endif %}