OrderShipmentSummary.php in Commerce Shipping 8.2
File
src/OrderShipmentSummary.php
View source
<?php
namespace Drupal\commerce_shipping;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class OrderShipmentSummary implements OrderShipmentSummaryInterface {
protected $entityTypeManager;
protected $shippingOrderManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ShippingOrderManagerInterface $shipping_order_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->shippingOrderManager = $shipping_order_manager;
}
public function build(OrderInterface $order, $view_mode = 'user') {
if (!$this->shippingOrderManager
->hasShipments($order)) {
return [];
}
$shipments = $order
->get('shipments')
->referencedEntities();
if (empty($shipments)) {
return [];
}
$first_shipment = reset($shipments);
$shipping_profile = $first_shipment
->getShippingProfile();
if (!$shipping_profile) {
return [];
}
$single_shipment = count($shipments) === 1;
$profile_view_builder = $this->entityTypeManager
->getViewBuilder('profile');
$shipment_view_builder = $this->entityTypeManager
->getViewBuilder('commerce_shipment');
$summary = [];
$summary['shipping_profile'] = $profile_view_builder
->view($shipping_profile, 'default');
foreach ($shipments as $index => $shipment) {
$summary[$index] = [
'#type' => $single_shipment ? 'container' : 'details',
'#title' => $shipment
->getTitle(),
'#open' => TRUE,
];
$summary[$index]['shipment'] = $shipment_view_builder
->view($shipment, $view_mode);
$summary[$index]['shipment']['shipping_profile']['#access'] = FALSE;
}
return $summary;
}
}