You are here

function template_preprocess_commerce_order in Commerce Core 8.2

Prepares variables for order templates.

Default template: commerce-order.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An associative array containing rendered fields.
  • attributes: HTML attributes for the containing element.

File

modules/order/commerce_order.module, line 150
Defines the Order entity and associated features.

Code

function template_preprocess_commerce_order(array &$variables) {
  $view_mode = $variables['elements']['#view_mode'];

  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['elements']['#commerce_order'];
  $variables['order_entity'] = $order;
  $variables['order'] = [];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['order'][$key] = $variables['elements'][$key];
  }

  // Inject order fields not manually printed in a separate variable for easier
  // rendering.
  if (in_array($view_mode, [
    'user',
    'admin',
  ])) {
    $printed_fields = [
      'order_items',
      'total_price',
      'activity',
      'completed',
      'placed',
      'changed',
      'uid',
      'mail',
      'ip_address',
      'billing_information',
      'shipping_information',
      'state',
      'balance',
    ];
    $variables['additional_order_fields'] = array_diff_key($variables['order'], array_combine($printed_fields, $printed_fields));
  }
  if ($billing_profile = $order
    ->getBillingProfile()) {
    $profile_view_bulder = \Drupal::entityTypeManager()
      ->getViewBuilder('profile');
    $variables['order']['billing_information'] = $profile_view_bulder
      ->view($billing_profile, $view_mode);
  }
}