1

我有一个名为“Collection”的内容类型,它显示了 tile 集合的属性。其中一个字段是“氛围”,它是一个段落,包含一个图像字段、一个文本字段和另一个名为“图块分解”的子段落,由图像、名称、格式和不同的显示选项组成。

Ambience
--Image
--Text
--Tile breakdown
----Image
----Name
----Format
----Container width
----[Other display options]

我需要在节点模板的一部分上渲染“环境”图像,在节点模板的另一部分上渲染其“瓦片分解”图像。所有图像都已配置为具有不同图像样式的彩盒。

我想知道,如何以已经渲染的方式显示这些图像,以及所有颜色框的东西。我尝试使用预处理函数将段落添加到 $variables['content'] 并使用 Twig Tweak 的 drupal_entity() 但我无法使其工作。

我也试过这个,但运气一样。

有人知道怎么做吗?

提前致谢!

4

1 回答 1

0

如果要将段落属性“向上”拉到节点级别,则必须在模板中这样做。您可以使用预处理功能更轻松地提取所需的段落字段并将它们添加为变量。下面是一个示例,您将所有图块的图像收集到一个名为 ambiance_images 的变量数组中,然后您可以在模板中使用它。对于您的需要,这可能在结构上过于简单,但应该让您朝着正确的方向前进。请注意,它假定您已经设置了图像样式(您应该)并将其命名为“平铺”。

function mytheme_preprocess_node(&$vars) {
  $node = $vars['node'];
  $type = $node->getType();
  $mode = $vars['view_mode'];

  if ($type == 'collection' && $mode == 'default') {
    $vars['ambiance_images'] = [];
    $style = \Drupal::entityTypeManager()->getStorage('image_style')->load('tile');
    $tiles = $node->get('field_ambience')->referencedEntities()->first()->get('field_tiles')->referencedEntities();
    foreach ($tiles as $tile) {
      $vars['ambiance_images'][] = $style->buildUrl($tile->get('field_image')->entity->getFileUri());
    }
  }
}
于 2018-04-12T16:09:24.953 回答