You are here

public function PriceDifferenceFormatter::viewElements in Price Difference Formatter 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides PriceCalculatedFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/PriceDifferenceFormatter.php, line 32

Class

PriceDifferenceFormatter
Plugin implementation of the 'price_difference_formatter' formatter.

Namespace

Drupal\price_difference_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  if (!$items
    ->isEmpty()) {
    $context = new Context($this->currentUser, $this->currentStore
      ->getStore(), NULL, []);

    /** @var \Drupal\commerce\PurchasableEntityInterface $purchasable_entity */
    $purchasable_entity = $items
      ->getEntity();
    $quantity = 1;
    if ($purchasable_entity instanceof OrderItemInterface) {
      if ($items
        ->getName() === 'total_price') {
        $quantity = $purchasable_entity
          ->getQuantity();
      }
      $purchasable_entity = $purchasable_entity
        ->getPurchasedEntity();
    }
    $adjustment_types = array_filter($this
      ->getSetting('adjustment_types'));

    // Get the adjusted price.
    $result = $this->priceCalculator
      ->calculate($purchasable_entity, $quantity, $context, $adjustment_types);
    $calculated_price = $result
      ->getCalculatedPrice();
    $number = $calculated_price
      ->getNumber();
    $currency_code = $calculated_price
      ->getCurrencyCode();
    $options = $this
      ->getFormattingOptions();

    // Get the price without the promotion adjustment, but still include
    // any other adjustments that were selected.
    unset($adjustment_types['promotion']);
    $result_without_promotion = $this->priceCalculator
      ->calculate($purchasable_entity, $quantity, $context, $adjustment_types);
    $calculated_price_without_promotion = $result_without_promotion
      ->getCalculatedPrice();
    $number_without_promotion = $calculated_price_without_promotion
      ->getNumber();
    if ($number_without_promotion != $number) {
      $difference = $number_without_promotion - $number;
      $percentage = $difference / $number_without_promotion;
    }

    // Get the display settings for the formatter.
    $display_elements = array_filter($this
      ->getSetting('display_elements'));
    $elements[0] = [
      '#theme' => 'price_difference_formatter',
      '#final_price' => $this->currencyFormatter
        ->format($number, $currency_code, $options),
      '#original_price' => $this->currencyFormatter
        ->format($number_without_promotion, $currency_code, $options),
      '#discount_percentage' => isset($percentage) && isset($display_elements['discount_percentage']) ? round($percentage * 100, 2) . '%' : NULL,
      '#discount_currency' => isset($difference) && isset($display_elements['discount_currency']) ? $this->currencyFormatter
        ->format($difference, $currency_code, $options) : NULL,
      '#adjustments' => $result
        ->getAdjustments(),
      '#cache' => [
        'tags' => $purchasable_entity
          ->getCacheTags(),
        'contexts' => Cache::mergeContexts($purchasable_entity
          ->getCacheContexts(), [
          'languages:' . LanguageInterface::TYPE_INTERFACE,
          'country',
        ]),
      ],
    ];
  }
  return $elements;
}