function template_preprocess_commerce_product in Commerce Core 8.2
Prepares variables for product templates.
Default template: commerce-product.html.twig.
Parameters
array $variables: An associative array containing:
- elements: An associative array containing rendered fields.
- attributes: HTML attributes for the containing element.
File
- modules/
product/ commerce_product.module, line 128 - Defines the Product entity and associated features.
Code
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];
}
}