ProductVariationFieldRendererLayoutBuilder.php in Commerce Core 8.2
File
modules/product/src/ProductVariationFieldRendererLayoutBuilder.php
View source
<?php
namespace Drupal\commerce_product;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_product\Plugin\Block\VariationFieldBlock;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\LayoutEntityHelperTrait;
class ProductVariationFieldRendererLayoutBuilder extends ProductVariationFieldRenderer {
use LayoutEntityHelperTrait;
protected $entityDisplayRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($entity_type_manager);
$this->entityDisplayRepository = $entity_display_repository;
}
public function renderFields(ProductVariationInterface $variation, $view_mode = 'default') {
$product = $variation
->getProduct();
assert($product !== NULL);
$view_mode_display = $this->entityDisplayRepository
->getViewDisplay($product
->getEntityTypeId(), $product
->bundle(), $view_mode);
if ($view_mode_display instanceof LayoutBuilderEntityViewDisplay && $view_mode_display
->isLayoutBuilderEnabled()) {
$sections = $view_mode_display
->getSections();
if ($view_mode_display
->isOverridable() && ($overrides = $this
->getEntitySections($variation
->getProduct()))) {
$sections = $overrides;
}
return $this
->renderLayoutBuilderFields($variation, $sections);
}
return parent::renderFields($variation, $view_mode);
}
protected function renderLayoutBuilderFields(ProductVariationInterface $variation, array $sections) {
$build = [];
foreach ($sections as $section) {
$components = $section
->getComponents();
foreach ($components as $component) {
$plugin = $component
->getPlugin();
if ($plugin instanceof VariationFieldBlock) {
$plugin_id = $plugin
->getPluginId();
list(, , , $field_name) = explode(PluginBase::DERIVATIVE_SEPARATOR, $plugin_id, 4);
$display_options = $plugin
->getConfiguration()['formatter'];
$build[$field_name] = $this
->prepareForAjax($this
->renderField($field_name, $variation, $display_options), $field_name, $variation);
}
}
}
return $build;
}
}