3

我正在尝试访问嵌套在数组深处的内容类型的值。这是实际情况:

我想使用附加字段信息自定义 Drupal 8 的默认搜索结果页面布局。要知道哪些字段可供我访问,我使用了{{ kint(item.value) }},这向我展示了这一点:

在此处输入图像描述

我要访问的字段嵌套在#result>node>fieldslikebody等下field_category

有什么方法可以直接在我的树枝模板中访问它们?我猜是什么{{ item.value['node']['fields']['field_name']} }?但这什么也没给我!

我的思维方式很可能不是一个合适的方法,甚至可能根本不是一个方法!我对 Drupal 还是很陌生。

我想要的只是访问嵌套在数组结构下方深处的所需字段。是否有一种经验法则或语法可以遵循?如果有人请解释从嵌套的 Drupal 数组中访问特定元素的过程,这将是一个很大的帮助。

注意:为了自定义布局,我将item-list.html.twig文件从/core/themes/stable/templaes/dataset主题的模板文件夹中复制了出来。

Drupal 版本:8.2.3

编辑:搜索结果的模板文件(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 %}
4

1 回答 1

0

如果要从模板中呈现特定字段,则必须使用例如:

{{ content.field_feature }}

您可以在数组或 d8 的 UI 中看到字段的机器名称。

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

有了这个,您可以自己访问您的数组和主题您的内容类型。

于 2016-12-09T06:56:05.170 回答