You are here

public function CheckoutProgressBlock::build in Commerce Core 8.2

Builds the checkout progress block.

Return value

array A render array.

Overrides BlockPluginInterface::build

File

modules/checkout/src/Plugin/Block/CheckoutProgressBlock.php, line 77

Class

CheckoutProgressBlock
Provides a checkout progress block.

Namespace

Drupal\commerce_checkout\Plugin\Block

Code

public function build() {
  $order = $this->routeMatch
    ->getParameter('commerce_order');
  if (!$order) {

    // The block is being rendered outside of the checkout page.
    return [];
  }
  $checkout_flow = $this->checkoutOrderManager
    ->getCheckoutFlow($order);
  $checkout_flow_plugin = $checkout_flow
    ->getPlugin();
  $configuration = $checkout_flow_plugin
    ->getConfiguration();
  if (empty($configuration['display_checkout_progress'])) {
    return [];
  }

  // Prepare the steps as expected by the template.
  $steps = [];
  $visible_steps = $checkout_flow_plugin
    ->getVisibleSteps();
  $requested_step_id = $this->routeMatch
    ->getParameter('step');
  $current_step_id = $this->checkoutOrderManager
    ->getCheckoutStepId($order, $requested_step_id);
  $current_step_index = array_search($current_step_id, array_keys($visible_steps));
  $index = 0;
  foreach ($visible_steps as $step_id => $step_definition) {
    if ($index < $current_step_index) {
      $position = 'previous';
    }
    elseif ($index == $current_step_index) {
      $position = 'current';
    }
    else {
      $position = 'next';
    }
    $index++;

    // Hide hidden steps until they are reached.
    if (!empty($step_definition['hidden']) && $position != 'current') {
      continue;
    }

    // Create breadcrumb style links for active checkout steps.
    if ($current_step_id !== 'complete' && $configuration['display_checkout_progress_breadcrumb_links'] && $index <= $current_step_index) {
      $label = Link::createFromRoute($step_definition['label'], 'commerce_checkout.form', [
        'commerce_order' => $order
          ->id(),
        'step' => $step_id,
      ])
        ->toString();
    }
    else {
      $label = $step_definition['label'];
    }
    $steps[] = [
      'id' => $step_id,
      'label' => $label,
      'position' => $position,
    ];
  }
  return [
    '#attached' => [
      'library' => [
        'commerce_checkout/checkout_progress',
      ],
    ],
    '#theme' => 'commerce_checkout_progress',
    '#steps' => $steps,
  ];
}