我有一个Drupal 8.7网站和Drupal Commerce 2.14模块
在我商店的模板中显示字段field_professionnel_cgv
,我使用此代码TWIG:
{{ store.field_professionnel_cgv }}
如何从我的产品模板中显示此字段。
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];
}
}
因此,您必须像这里一样,获取存储对象并分配树枝变量。