我正在尝试从格式为
views-view-unformatted....html.twig
的 Twig 模板中的视图“内容:链接到内容”输出 url 别名。 url 别名在视图预览中成功输出,类似于 /path1/article
。论坛建议 {{ fields.view_node.content }}
应该可以,但在我的情况下不行。
views-view-unformatted....html.twig
如下
{#
/**
* @file
* Theme override to display a view of unformatted rows.
*
* Available variables:
* - title: The title of this group of rows. May be empty.
* - rows: A list of the view's row items.
* - attributes: The row's HTML attributes.
* - content: The row's content.
* - view: The view object.
* - default_row_class: A flag indicating whether default classes should be
* used on rows.
*
* @see template_preprocess_views_view_unformatted()
*/
#}
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
]
%}
{{row.content['#row']._entity.title[0].value}} ***returns value for title
/node/{{row.content['#row']._entity.nid[0].value}}" ***returns nid
{{ fields.view_node.content }} ***doesn't work
{{ path('entity.node.canonical', {'node': node.id}) }} ***crashes page
{% endfor %}
我尝试过类似的变体:
{{row.content['#row']._entity.url[0].value}}
{{row.content['#row']._entity.view_node[0].value}}
{{row.content['#row']._entity.alias[0].value}}
没有成功。
看起来我的模板没有这个thread中提到的字段变量,那么我将如何启用一个字段变量呢?我使用 Drupal Barrio 主题
希望得到一些帮助。谢谢
你说 {{row.content['#row']._entity.nid[0].value}} 返回你的节点的 id。
所以而不是
{{ path('entity.node.canonical', {'node': node.id}) }}
您应该将node.id替换为行内容中节点的id:
{{ path('entity.node.canonical', {'node': row.content['#row']._entity.nid[0].value}) }}
这就是您之前的尝试失败的原因,因为在这种情况下 {{ node.id }} 不存在。