【问题标题】:Drupal 8, Twig - how to output fields from a custom view template, and check for taxonomy termsDrupal 8,Twig - 如何从自定义视图模板输出字段,并检查分类术语
【发布时间】:2020-09-29 14:12:00
【问题描述】:

我想输出从视图呈现的分类字段的值:

<h4>{{ label }}</h4><!-- Does show -->

<p>{{ content.field_event_type }}</p><!-- Doesn't show -->

<p>
  {{ content.field_date }}<!-- Doesn't show -->
  <br>{{ content.field_location }}<!-- Doesn't show -->
</p>

<a href="{{ url }}" aria-label="" class="btn btn-primary my-2 p-3">Read event details</a>

此外,field_event_type 是一个分类字段。我希望能够检查它是否具有给定的分类,以及输出上述值。例如:

{% if content.field_event_type.has_term("webinar") %}
    <p>Is webinar</p>
{% endif %}

但我找不到任何东西来做这件事。

【问题讨论】:

    标签: twig drupal-8


    【解决方案1】:

    试试这个

    {% set tid = node.yourTaxonomyField.target_id %}
    {% if tid %}
      {{ drupal_field('field_desired_field', 'taxonomy_term', tid) }}
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多