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

问题描述 投票:0回答:1

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

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

{{ store.field_professionnel_cgv }}

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

templates drupal twig drupal-8 drupal-commerce
1个回答
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]; } }

因此,您必须像这里一样,获取存储对象并分配树枝变量。
© www.soinside.com 2019 - 2024. All rights reserved.