3

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:

enter image description here

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 %}
Subrata Sarkar
  • 2,975
  • 6
  • 45
  • 85
  • cannot find any errors. my editor syntax highlight just the {{ list_type }}> red. you checked if the IF statment is true? and ive never seen those minuses in {%- -%}. – Gabbax0r Dec 09 '16 at 10:38
  • I don't have any idea what those "-" signs are doing. I copied the template from stable theme and it was written there like this. – Subrata Sarkar Dec 09 '16 at 10:49

1 Answers1

0

If you want to render specific fields from a template you have to use for example:

{{ content.field_feature }}

you can see in your array or in the UI of d8 the machine names of your fields.

{{ content.field_<machine-name> }}

with this you can access your array and theme your content type yourself.

Gabbax0r
  • 1,696
  • 3
  • 18
  • 31