You are here

function template_preprocess_commerce_invoice in Commerce Invoice 8.2

Prepares variables for invoice templates.

Default template: commerce-invoice.html.twig.

Parameters

array $variables: An associative array containing:

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

File

./commerce_invoice.module, line 271
Defines the Invoice entity and associated features.

Code

function template_preprocess_commerce_invoice(array &$variables) {

  /** @var Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
  $invoice = $variables['elements']['#commerce_invoice'];
  $token = \Drupal::token();
  $invoice_type_data = $invoice
    ->getData('invoice_type', []);
  $invoice_type = InvoiceType::create($invoice_type_data);

  // Replace the tokens in the footer text + the payment terms.
  $data = [
    'commerce_invoice' => $invoice,
  ];
  $footer_text = $invoice_type
    ->getFooterText();
  if ($footer_text) {
    $variables['footer_text'] = $token
      ->replace($footer_text, $data);
  }
  $payment_terms = $invoice_type
    ->getPaymentTerms();
  if ($payment_terms) {
    $variables['payment_terms'] = $token
      ->replace($payment_terms, $data);
  }
  if ($invoice_type
    ->getLogoUrl()) {
    $variables['logo_url'] = $invoice_type
      ->getLogoUrl();
  }
  $variables['invoice_entity'] = $invoice;
  $variables['invoice'] = [];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['invoice'][$key] = $variables['elements'][$key];
  }
  if ($invoice
    ->getBillingProfile()) {
    $profile_view_bulder = \Drupal::entityTypeManager()
      ->getViewBuilder('profile');
    $variables['invoice']['billing_information'] = $profile_view_bulder
      ->view($invoice
      ->getBillingProfile());
  }

  /** @var \Drupal\commerce_invoice\InvoiceTotalSummaryInterface $invoice_total_summary */
  $invoice_total_summary = \Drupal::service('commerce_invoice.invoice_total_summary');
  $variables['totals'] = $invoice_total_summary
    ->buildTotals($invoice);
  if (\Drupal::moduleHandler()
    ->moduleExists('commerce_log')) {
    $variables['invoice']['activity'] = [
      '#type' => 'view',
      '#name' => 'commerce_activity',
      '#display_id' => 'default',
      '#arguments' => [
        $invoice
          ->id(),
        'commerce_invoice',
      ],
      '#embed' => TRUE,
      '#title' => t('Invoice activity'),
    ];
  }
}