如何在Twig模板中呈现字符串<drupal-media>?

问题描述

我的模块中有障碍物。我正在加载文章类型“视频”,一个字段是“长字符串”。内容就是媒体-远程视频。

$body = $articles->get('body')->getValue();
$shortBody = $articles->get('field_video')->value;
$items[] = array(
              'title' => $articles->getTitle(),'text' => $body[0]['value'],'id' => $articles->id(),'path' => $alias,'date' => $articles->get('revision_timestamp')->value,'shorttext' => $shortBody
);

在短文本中,我有字符串<drupal-media data-align="center" data-entity-type="media" data-entity-uuid .... </drupal-media>。模板将其视为字符串,因此不会渲染视频文件。如何从Twig模板中的此字符串渲染视频文件? 谢谢

解决方法

好的,我的解决方法是这样。我将字段类型更改为“媒体-实体”。

然后我将$shortBody = $articles->get('field_video')->value;重写为$shortBody = $articles->get('field_video')->getValue();

并在Twig文件中

{{ drupal_entity('media',item.shorttext) }}

这很好用。