You are here

public function CreditMessaging::render in Commerce PayPal 8

Render the area.

Parameters

bool $empty: (optional) Indicator if view result is empty or not. Defaults to FALSE.

Return value

array In any case we need a valid Drupal render array to return.

Overrides AreaPluginBase::render

File

src/Plugin/views/area/CreditMessaging.php, line 205

Class

CreditMessaging
Defines a PayPal Credit messaging area handler.

Namespace

Drupal\commerce_paypal\Plugin\views\area

Code

public function render($empty = FALSE) {
  if (!$empty || !empty($this->options['empty'])) {
    foreach ($this->view->argument as $name => $argument) {

      // First look for an order_id argument.
      if (!$argument instanceof NumericArgument) {
        continue;
      }
      if (!in_array($argument
        ->getField(), [
        'commerce_order.order_id',
        'commerce_order_item.order_id',
      ])) {
        continue;
      }

      /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
      $order = $this->orderStorage
        ->load($argument
        ->getValue());
      if (!$order) {
        return [];
      }
      $element = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'data-pp-message' => '',
          'data-pp-placement' => $this->options['placement'],
          'data-pp-amount' => Calculator::trim($order
            ->getTotalPrice()
            ->getNumber()),
          'data-pp-style-layout' => $this->options['style'],
        ],
        '#attached' => [
          'library' => [
            'commerce_paypal/credit_messaging',
          ],
        ],
      ];
      if ($this->options['style'] == 'flex') {
        $element['#attributes'] += [
          'data-pp-style-color' => $this->options['color'],
          'data-pp-style-ratio' => $this->options['ratio'],
        ];
      }
      else {
        $element['#attributes'] += [
          'data-pp-style-logo-type' => $this->options['logo_type'],
          'data-pp-style-logo-position' => $this->options['logo_position'],
          'data-pp-style-text-size' => $this->options['text_size'],
          'data-pp-style-text-color' => $this->options['text_color'],
        ];
      }
      return $element;
    }
  }
  return [];
}