我正在尝试访问嵌套在数组深处的内容类型的值。这是实际场景:
我想使用附加字段信息自定义 Drupal 8 的默认搜索结果页面布局。为了知道我可以访问哪些字段,我使用了
{{ kint(item.value) }}
,它向我展示了以下内容:
我想要访问的字段嵌套在
#result>node>fields
下,例如 body
、field_category
等
有什么方法可以直接在我的树枝模板中访问它们吗?我在猜测类似
{{ item.value['node']['fields']['field_name']} }
之类的东西?但这没有给我带来任何好处!
我的想法很可能不是一个合适的方法,甚至根本不是一个方法!我对 Drupal 还很陌生。
我想要的只是访问嵌套在数组结构内部深处的所需字段。是否有某种经验法则或语法可供遵循?是否有一个过程可以从嵌套的 Drupal 数组内部访问特定元素?
注意:为了自定义布局,我将
item-list.html.twig
文件从 /core/themes/stable/templates/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 %}
如果您想从模板中呈现特定字段,则必须使用例如:
{{ content.field_feature }}
您可以在数组或 d8 的 UI 中看到字段的机器名称。
{{ content.field_<machine-name> }}
通过此,您可以自己访问您的数组和主题您的内容类型。