protected function ProductViewBuilder::alterBuild in Commerce Core 8.2
Specific per-entity building.
Parameters
array $build: The render array that is being created.
\Drupal\Core\Entity\EntityInterface $entity: The entity to be prepared.
\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity view display holding the display options configured for the entity components.
string $view_mode: The view mode that should be used to prepare the entity.
Overrides EntityViewBuilder::alterBuild
File
- modules/
product/ src/ ProductViewBuilder.php, line 79
Class
- ProductViewBuilder
- Defines the entity view builder for products.
Namespace
Drupal\commerce_productCode
protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// If we're in a Layout Builder controlled display, we do not need to run
// variation field injection, as any of these fields will be placed as
// blocks within the display.
$is_layout_builder = $display instanceof LayoutEntityDisplayInterface && $display
->isLayoutBuilderEnabled();
if ($is_layout_builder) {
return;
}
$product_type_storage = $this->entityTypeManager
->getStorage('commerce_product_type');
/** @var \Drupal\commerce_product\ProductVariationStorageInterface $variation_storage */
$variation_storage = $this->entityTypeManager
->getStorage('commerce_product_variation');
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
$product_type = $product_type_storage
->load($entity
->bundle());
if ($product_type
->shouldInjectVariationFields() && $entity
->getDefaultVariation()) {
$variation = $variation_storage
->loadFromContext($entity);
$variation = $this->entityRepository
->getTranslationFromContext($variation, $entity
->language()
->getId());
$attribute_field_names = $variation
->getAttributeFieldNames();
$rendered_fields = $this->variationFieldRenderer
->renderFields($variation, $view_mode);
foreach ($rendered_fields as $field_name => $rendered_field) {
// Group attribute fields to allow them to be excluded together.
if (in_array($field_name, $attribute_field_names)) {
$build['variation_attributes']['variation_' . $field_name] = $rendered_field;
}
else {
$build['variation_' . $field_name] = $rendered_field;
}
}
}
}