View source
<?php
namespace Drupal\commerce_product\Plugin\Block;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_product\ProductVariationFieldRendererInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\layout_builder\Plugin\Block\FieldBlock;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class VariationFieldBlock extends FieldBlock {
protected $productVariationFieldRenderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, FormatterPluginManager $formatter_manager, ModuleHandlerInterface $module_handler, LoggerInterface $logger, ProductVariationFieldRendererInterface $product_variation_field_render) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager, $formatter_manager, $module_handler, $logger);
$this->productVariationFieldRenderer = $product_variation_field_render;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.formatter'), $container
->get('module_handler'), $container
->get('logger.channel.layout_builder'), $container
->get('commerce_product.variation_field_renderer'));
}
public function build() {
$display_settings = $this
->getConfiguration()['formatter'];
$entity = $this
->getEntity();
assert($entity instanceof ProductVariationInterface);
try {
$build = $this->productVariationFieldRenderer
->renderField($this->fieldName, $entity, $display_settings);
} catch (\Exception $e) {
$build = [];
$this->logger
->warning('The field "%field" failed to render with the error of "%error".', [
'%field' => $this->fieldName,
'%error' => $e
->getMessage(),
]);
}
CacheableMetadata::createFromObject($this)
->applyTo($build);
return $build;
}
}