commerce_product_bundles-8.x-1.0/src/Plugin/Block/BundleVariationFieldBlock.php
src/Plugin/Block/BundleVariationFieldBlock.php
<?php
namespace Drupal\commerce_product_bundles\Plugin\Block;
use Drupal\commerce_product_bundles\Entity\ProductBundleVariationInterface;
use Drupal\commerce_product_bundles\service\ProductBundleVariationFieldRendererInterface;
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;
/**
* Bundle Variation field block.
*
* Code was taken from and modified:
* @see \Drupal\commerce_product\Plugin\Block\VariationFieldBlock
*/
class BundleVariationFieldBlock extends FieldBlock {
/**
* @var \Drupal\commerce_product_bundles\service\ProductBundleVariationFieldRendererInterface
*/
protected $productBundleVariationFieldRenderer;
/**
* BundleVariationFieldBlock constructor.
*
* @param array $configuration
* @param $plugin_id
* @param $plugin_definition
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_manager
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* @param \Psr\Log\LoggerInterface $logger
* @param \Drupal\commerce_product_bundles\service\ProductBundleVariationFieldRendererInterface $bundle_variation_field_render
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, FormatterPluginManager $formatter_manager, ModuleHandlerInterface $module_handler, LoggerInterface $logger, ProductBundleVariationFieldRendererInterface $bundle_variation_field_render) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager, $formatter_manager, $module_handler, $logger);
$this->productBundleVariationFieldRenderer = $bundle_variation_field_render;
}
/**
* {@inheritdoc}
*/
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_bundles.bundle_variation_field_renderer')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$display_settings = $this->getConfiguration()['formatter'];
$entity = $this->getEntity();
assert($entity instanceof ProductBundleVariationInterface);
try {
$build = $this->productBundleVariationFieldRenderer->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;
}
}
