You are here

public function PanelizerProduct::alterBuild in Commerce Core 8.2

Currently this mimics \Drupal\commerce_product\ProductViewBuilder::alterBuild until we expose injected variation fields to Panels.

@todo Remove once https://www.drupal.org/node/2723691 lands

File

modules/product/src/Plugin/PanelizerEntity/PanelizerProduct.php, line 103

Class

PanelizerProduct
Panelizer entity plugin for integrating with products.

Namespace

Drupal\commerce_product\Plugin\PanelizerEntity

Code

public function alterBuild(array &$build, EntityInterface $entity, PanelsDisplayVariant $panels_display, $view_mode) {

  /** @var \Drupal\commerce_product\Entity\ProductInterface $entity */
  parent::alterBuild($build, $entity, $panels_display, $view_mode);
  $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);
    $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;
      }
    }
  }
}