如何显示产品模板中的商店字段?

时间:2019-09-16 14:20:56

标签: templates drupal twig drupal-8 drupal-commerce

我有一个Drupal 8.7网站和Drupal Commerce 2.14模块

在商店的模板中显示字段field_professionnel_cgv,我使用以下代码TWIG:

{{ store.field_professionnel_cgv }}

如何从我的产品模板中显示此字段。

1 个答案:

答案 0 :(得分:0)

您可以将mymodule_preprocess_commerce_product()钩子函数添加到模块中,并在该函数内部获取存储实体并使用其数据设置twig变量,以便在模板内可用。

检查原始功能的外观: /modules/contrib/commerce/modules/product/commerce_product.module

function template_preprocess_commerce_product(array &$variables) {
  /** @var Drupal\commerce_product\Entity\ProductInterface $product */
  $product = $variables['elements']['#commerce_product'];

  $variables['product_entity'] = $product;
  $variables['product_url'] = $product->isNew() ? '' : $product->toUrl();
  $variables['product'] = [];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['product'][$key] = $variables['elements'][$key];
  }
}

因此,您必须像在此那样获取存储对象并分配twig变量。

相关问题