View source
<?php
namespace Drupal\uc_order;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\uc_order\Plugin\OrderPaneManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderViewBuilder extends EntityViewBuilder {
protected $orderPaneManager;
public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, OrderPaneManager $order_pane_manager) {
parent::__construct($entity_type, $entity_manager, $language_manager);
$this->orderPaneManager = $order_pane_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager'), $container
->get('language_manager'), $container
->get('plugin.manager.uc_order.order_pane'));
}
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
unset($build['#theme']);
return $build;
}
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
parent::buildComponents($build, $entities, $displays, $view_mode);
$panes = $this->orderPaneManager
->getPanes();
$components = $displays['uc_order']
->getComponents();
foreach ($entities as $id => $order) {
foreach ($panes as $pane_id => $pane) {
if (!isset($components[$pane_id])) {
continue;
}
if ($contents = $pane
->view($order, $view_mode)) {
$build[$id][$pane_id] = [
'#type' => 'container',
'#attributes' => [
'id' => 'order-pane-' . $pane_id,
'class' => array_merge([
'order-pane',
], $pane
->getClasses()),
],
];
if ($title = $pane
->getTitle()) {
$build[$id][$pane_id]['title'] = [
'#type' => 'container',
'#markup' => $title . ':',
'#attributes' => [
'class' => [
'order-pane-title',
],
],
];
}
$build[$id][$pane_id]['pane'] = $contents;
}
}
}
}
}