简体   繁体   English

Drupal 8 views-view-field.html.twig

[英]Drupal 8 views-view-field.html.twig

this is going to be quick and direct. 这将是快速而直接的。 I'm trying to access different content results from a view related with a content type. 我正在尝试从与内容类型相关的视图访问不同的内容结果。 I'm pretty much a newbie with twig, but I did make it work to access fields. 我几乎是树枝的新手,但确实可以访问字段。

Example: 例:

 Content Type: Books
 Fields: Name, Portrait(Image). Machine Name: field_name - field_image

Let's say I upload 2 contents called Book 1 and Book 2 based on the content type. 假设我根据内容类型上载了名为Book 1和Book 2的2个内容。

What I want is to display all fields from Book 1 for the moment or Book 2 but not all of the content at the same time. 我要显示的是当前书籍1或书籍2的所有字段,但不是同时显示所有内容。

My effort: 我的努力:

  • First attempt: tried this line of code 第一次尝试:尝试了这一行代码

    <h2>{{view.field.field_name.getvalue(view.result[0])}}</h2>

    Works perfectly, it prints index 0 but not working printing the image, because instead it gives me the id. 效果很好,它可以打印索引0,但不能打印图像,因为它给了我id。

  • Second attempt: tried this line of code: 第二次尝试:尝试了以下代码行:

    {{fields.field_image.content}}

    This one seems to be the most practical, it works showing the actual picture from the field image, but I dont know how to access only the image for one index(Book 1). 这似乎是最实用的一种,它可以显示现场图像中的实际图像,但我不知道如何仅访问一个索引的图像(书1)。

  • Third attempt: tried this line of code 第三次尝试:尝试了这行代码

    {{content.field_image.0}}

    This one doesnt work for me at all. 这个根本不适合我。

Hopefully somebody would help me with this. 希望有人可以帮助我。 I've been struggling one week with this. 我一直为此苦苦挣扎。

You can turn on Twig debugging, then do a dump to see how things are structured. 您可以打开Twig调试,然后进行转储以查看事物的结构。

https://www.drupal.org/docs/8/theming/twig/discovering-and-inspecting-variables-in-twig-templates https://www.drupal.org/docs/8/theming/twig/discovering-and-inspecting-variables-in-twig-templates

Or perhaps look at the Twig Tweak module that gives a lot of great functions that might suite your needs: 或者看一下Twig Tweak模块,该模块提供了许多可能满足您需求的出色功能:

https://www.drupal.org/docs/8/modules/twig-tweak/cheat-sheet-8x-2x https://www.drupal.org/docs/8/modules/twig-tweak/cheat-sheet-8x-2x

This one in particular might be good since you have the ID in your first example: 由于您在第一个示例中具有ID,因此特别有用:

{# The argument can be FID, UUID or URI. #}
  <dt>Image:</dt>
  <dd>{{ drupal_image('public://ocean.jpg') }}</dd>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM