class OrderShipmentSummary in Commerce Shipping 8.2
Default implementation of the order shipment summary.
Renders the shipping profile, then the information for each shipment. Assumes that all shipments share the same shipping profile.
Hierarchy
- class \Drupal\commerce_shipping\OrderShipmentSummary implements OrderShipmentSummaryInterface
Expanded class hierarchy of OrderShipmentSummary
1 string reference to 'OrderShipmentSummary'
1 service uses OrderShipmentSummary
File
- src/
OrderShipmentSummary.php, line 14
Namespace
Drupal\commerce_shippingView source
class OrderShipmentSummary implements OrderShipmentSummaryInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The shipping order manager.
*
* @var \Drupal\commerce_shipping\ShippingOrderManagerInterface
*/
protected $shippingOrderManager;
/**
* Constructs a new OrderShipmentSummary object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\commerce_shipping\ShippingOrderManagerInterface $shipping_order_manager
* The shipping order manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ShippingOrderManagerInterface $shipping_order_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->shippingOrderManager = $shipping_order_manager;
}
/**
* {@inheritdoc}
*/
public function build(OrderInterface $order, $view_mode = 'user') {
if (!$this->shippingOrderManager
->hasShipments($order)) {
return [];
}
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
$shipments = $order
->get('shipments')
->referencedEntities();
if (empty($shipments)) {
return [];
}
$first_shipment = reset($shipments);
$shipping_profile = $first_shipment
->getShippingProfile();
if (!$shipping_profile) {
// Trying to generate a summary of incomplete shipments.
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);
// The shipping profile is already shown above, so avoid duplication.
$summary[$index]['shipment']['shipping_profile']['#access'] = FALSE;
}
return $summary;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderShipmentSummary:: |
protected | property | The entity type manager. | |
OrderShipmentSummary:: |
protected | property | The shipping order manager. | |
OrderShipmentSummary:: |
public | function |
Builds a summary of the given order's shipments. Overrides OrderShipmentSummaryInterface:: |
|
OrderShipmentSummary:: |
public | function | Constructs a new OrderShipmentSummary object. |