【问题标题】:Accessing fields of an entity reference in a view (views-view-fields.html.twig)访问视图中实体引用的字段 (views-view-fields.html.twig)
【发布时间】:2021-01-19 14:05:04
【问题描述】:

所以我有 2 种内容类型

  • 网络研讨会

网络研讨会有一个实体参考字段(label:speaker, id:field_person_speaker)

  • 人(多个值)

我在路径 /webinar 上有一个包含这些字段的视图(内容类型网络研讨会的未格式化字段列表)

  • 标题
  • youtubeID
  • 日期
  • 扬声器(格式化程序:渲染实体)

我有一个呈现一行的模板(views-view-fields--vcon-webinar.html.twig):

{% set title = fields.title.content %}
{% set date = fields.field_webinar_date.content %}
{% set url = path('entity.node.canonical', { 'node' : row.nid}) %}
{% set speakers = fields.field_person_speaker.content %}

<div class="card h-100 shadow dl-webinar">
  <div class="card-body">
    <a class="dl-webinar-link" href="{{url}}"><span class="btn btn-light btn-sm dl-webinar-signup">Sign up</span></a>
    <div>
      <div class="d-flex">
        {{speakers}}
      </div>
    </div>
    <div>
      <div class="d-flex">
        <div class="p-3">
          <h4>{{title}}</h4>
          <p class="font-weight-light mb-0">{{date}}</p>
        </div>
      </div>
    </div>

  </div>
</div>

问题是我无法获取引用实体(扬声器)的各个字段值。我需要这些字段值(例如:field_person_first_name),因为我需要完全控制输出模板。

<div class="dl-webinar-bio d-flex align-items-end flex-row-reverse  flex-fill">
    <div><p class="dl-webinar-bio-name"><strong>First name, Last name</strong> <small class="font-weight-light">Project manager</small></p></div>
</div>

请有人帮我解决这个问题...谢谢....

【问题讨论】:

  • 也许对 Persons 实体使用 Rendered Entity 格式化程序会更好?您可以为此设置特定的查看模式并准备单独的模板。如果我很了解您想要达到的目标。
  • stackoverflow.com/users/156892/kevinius 。据我了解,当您需要使用 Person 内容类型添加 RELATIONSHIP 以加载其字段时,就是这种情况 - joxi.net/BA0VWwDUP0OMzm

标签: drupal-8 drupal-views


【解决方案1】:

最简单的方法是在特定的视图模式下呈现 Person 内容类型。 然后使用您期望的确切 HTML 结构覆盖模板 node--person-yourviewmode.html.twig

所以你没有改变views-view-fields--vcon-webinar.html.twig模板。

node--person-yourviewmode.html.twig

<div class="dl-webinar-bio d-flex align-items-end flex-row-reverse  flex-fill">
    <div><p class="dl-webinar-bio-name"><strong>{{content.field_firstname}} {{ content.field_lastname}}</strong> <small class="font-weight-light">Project manager</small></p></div>
</div>

上面的模板会在views-view-fields--vcon-webinar.html.twig中调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多