You are here

public function OrderShipmentSummary::build in Commerce Shipping 8.2

Builds a summary of the given order's shipments.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

string $view_mode: The view mode used to render the shipments.

Return value

array The renderable array with the shipment summary.

Overrides OrderShipmentSummaryInterface::build

File

src/OrderShipmentSummary.php, line 46

Class

OrderShipmentSummary
Default implementation of the order shipment summary.

Namespace

Drupal\commerce_shipping

Code

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;
}