commerce_product_bundles-8.x-1.0/src/BundleVariationFieldRendererLayoutBuilder.php
src/BundleVariationFieldRendererLayoutBuilder.php
<?php
namespace Drupal\commerce_product_bundles;
use Drupal\commerce_product_bundles\Entity\ProductBundleVariationInterface;
use Drupal\commerce_product_bundles\Plugin\Block\BundleVariationFieldBlock;
use Drupal\commerce_product_bundles\Service\ProductBundleVariationFieldRenderer;
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 BundleVariationFieldRendererLayoutBuilder
*
* Code was taken from and modified:
* @see \Drupal\commerce_product\ProductVariationFieldRendererLayoutBuilder
*
* @package Drupal\commerce_product_bundles
*/
class BundleVariationFieldRendererLayoutBuilder extends ProductBundleVariationFieldRenderer {
use LayoutEntityHelperTrait;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* BundleVariationFieldRendererLayoutBuilder constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($entity_type_manager);
$this->entityDisplayRepository = $entity_display_repository;
}
/**
* {@inheritdoc}
*/
public function renderFields(ProductBundleVariationInterface $bundle_variation, $view_mode = 'default') {
$bundle_product = $bundle_variation->getBundleProduct();
assert($bundle_product !== NULL);
$view_mode_display = $this->entityDisplayRepository->getViewDisplay($bundle_product->getEntityTypeId(), $bundle_product->bundle());
// Check if layouts are enabled for that product.
if ($view_mode_display instanceof LayoutBuilderEntityViewDisplay && $view_mode_display->isLayoutBuilderEnabled()) {
$sections = $view_mode_display->getSections();
if ($view_mode_display->isOverridable() && $overrides = $this->getEntitySections($bundle_variation->getBundleProduct())) {
$sections = $overrides;
}
return $this->renderLayoutBuilderFields($bundle_variation, $sections);
}
return parent::renderFields($bundle_variation, $view_mode);
}
/**
* Render fields.
*
* @param \Drupal\commerce_product_bundles\Entity\ProductBundleVariationInterface $bundle_variation
* @param array $sections
*
* @return array
*/
protected function renderLayoutBuilderFields(ProductBundleVariationInterface $bundle_variation, array $sections) {
$build = [];
// Loop trough sections, grab their components.
foreach ($sections as $section) {
$components = $section->getComponents();
foreach ($components as $component) {
$plugin = $component->getPlugin();
if ($plugin instanceof BundleVariationFieldBlock) {
$plugin_id = $plugin->getPluginId();
[,,, $field_name] = explode(PluginBase::DERIVATIVE_SEPARATOR, $plugin_id, 4);
$display_options = $plugin->getConfiguration()['formatter'];
// Render field with display options provided from plugin formatter.
$build[$field_name] = $this->prepareForAjax($this->renderField($field_name, $bundle_variation, $display_options), $field_name, $bundle_variation);
}
}
}
return $build;
}
}
