在 Drupal 中应用图像作为背景

Apply image as background in Drupal

我为模块创建了一个模板,想要打印以将图像作为背景。

如果我这样做,

{{content.field_background_image}}

我看到图片路径:/sites/default/files/2019-09/carrots-1082251_1280.jpg

但是如果我在 HTML 中尝试它,当我刷新它时页面会中断:

<div class="slide-content-wrapper" style="background-image: url('/{{ content.field_background_image }}')">

如何将内容图片作为背景?

我一开始也很难弄清楚这一点。您可以执行以下操作:

  1. 转到 'Manage Display' 以了解您的模板适用的内容类型。
  2. 确保图像字段上的格式设置为 'URL to image' 而不是 'Image'
  3. 然后在模板中写入以下代码:
<div style="background-image: url({{ content.field_background_image['0']['#markup'] }})">

当您插入 {{ content.field_background_image }} 时,它上面有一大堆属性和值,因此您必须访问图像 url 所在的特定字段。