class ProductVariationFieldRendererLayoutBuilder in Commerce Core 8.2
Hierarchy
- class \Drupal\commerce_product\ProductVariationFieldRenderer implements ProductVariationFieldRendererInterface
- class \Drupal\commerce_product\ProductVariationFieldRendererLayoutBuilder uses LayoutEntityHelperTrait
Expanded class hierarchy of ProductVariationFieldRendererLayoutBuilder
File
- modules/
product/ src/ ProductVariationFieldRendererLayoutBuilder.php, line 13
Namespace
Drupal\commerce_productView source
class ProductVariationFieldRendererLayoutBuilder extends ProductVariationFieldRenderer {
use LayoutEntityHelperTrait;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Constructs a new ProductVariationFieldRendererLayoutBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The 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(ProductVariationInterface $variation, $view_mode = 'default') {
// Get parent product and load its view mode display.
$product = $variation
->getProduct();
assert($product !== NULL);
$view_mode_display = $this->entityDisplayRepository
->getViewDisplay($product
->getEntityTypeId(), $product
->bundle(), $view_mode);
// Check if layouts are enabled for that product.
if ($view_mode_display instanceof LayoutBuilderEntityViewDisplay && $view_mode_display
->isLayoutBuilderEnabled()) {
// Grab sections from bundle layout view mode.
$sections = $view_mode_display
->getSections();
// If overrides are allowed, fetch them if they exists.
if ($view_mode_display
->isOverridable() && ($overrides = $this
->getEntitySections($variation
->getProduct()))) {
$sections = $overrides;
}
// Render fields for output.
return $this
->renderLayoutBuilderFields($variation, $sections);
}
// If no layouts are enabled proceed to regular rendering.
return parent::renderFields($variation, $view_mode);
}
/**
* Render fields from LayoutBuilder sections.
*
* @param \Drupal\commerce_product\Entity\ProductVariationInterface $variation
* The product variation.
* @param \Drupal\layout_builder\Section[] $sections
* The layout sections.
*
* @return array
* Return array of rendered fields.
*/
protected function renderLayoutBuilderFields(ProductVariationInterface $variation, array $sections) {
$build = [];
// Loop trough sections, grab their components.
foreach ($sections as $section) {
// Grab section components, then loop trough them
// to find fields from variations on each component.
$components = $section
->getComponents();
foreach ($components as $component) {
$plugin = $component
->getPlugin();
// We are only interested in field blocks from commerce product module.
if ($plugin instanceof VariationFieldBlock) {
$plugin_id = $plugin
->getPluginId();
list(, , , $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, $variation, $display_options), $field_name, $variation);
}
}
}
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LayoutEntityHelperTrait:: |
protected | property | The section storage manager. | 1 |
LayoutEntityHelperTrait:: |
protected | function | Gets the sections for an entity if any. | |
LayoutEntityHelperTrait:: |
protected | function | Gets components that have Inline Block plugins. | |
LayoutEntityHelperTrait:: |
protected | function | Gets revision IDs for layout sections. | |
LayoutEntityHelperTrait:: |
protected | function | Gets the section storage for an entity. | |
LayoutEntityHelperTrait:: |
protected | function | Determines if an entity is using a field for the layout override. | |
LayoutEntityHelperTrait:: |
protected | function | Determines if an entity can have a layout. | |
LayoutEntityHelperTrait:: |
protected | function | Determines if the original entity used the default section storage. | |
LayoutEntityHelperTrait:: |
private | function | Gets the section storage manager. | 1 |
ProductVariationFieldRenderer:: |
protected | property | The product variation view builder. | |
ProductVariationFieldRenderer:: |
protected | function | Builds the AJAX replacement CSS class for a variation's field. | |
ProductVariationFieldRenderer:: |
protected | function | Prepares the rendered field for AJAX replacement. | |
ProductVariationFieldRenderer:: |
public | function |
Renders a single variation field. Overrides ProductVariationFieldRendererInterface:: |
|
ProductVariationFieldRenderer:: |
public | function |
Replaces the rendered variation fields via AJAX. Overrides ProductVariationFieldRendererInterface:: |
|
ProductVariationFieldRendererLayoutBuilder:: |
protected | property | The entity display repository. | |
ProductVariationFieldRendererLayoutBuilder:: |
public | function |
Renders all renderable variation fields. Overrides ProductVariationFieldRenderer:: |
|
ProductVariationFieldRendererLayoutBuilder:: |
protected | function | Render fields from LayoutBuilder sections. | |
ProductVariationFieldRendererLayoutBuilder:: |
public | function |
Constructs a new ProductVariationFieldRendererLayoutBuilder object. Overrides ProductVariationFieldRenderer:: |