You are here

public function PriceTableDefaultFormatter::viewElements in Commerce Price Table 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 PriceDefaultFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/PriceTableDefaultFormatter.php, line 124

Class

PriceTableDefaultFormatter
Plugin implementation of the 'commerce_price_table' formatter.

Namespace

Drupal\commerce_price_table\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $header = [];
  $elements = [];
  $options = $this
    ->getFormattingOptions();
  foreach ($items as $delta => $item) {
    if (isset($item->min_qty) && $item->max_qty && $item->amount) {
      $header[] = $this
        ->getQuantityHeaders($item);
      $row[] = [
        'data' => $this->currencyFormatter
          ->format($item->amount, $item->currency_code, $options),
      ];
    }
  }
  if ($this
    ->getSetting('table_orientation') == PriceTableDefaultFormatter::VERTICAL_MODE) {
    $rows = [];
    $header_old = $header;
    $header = [
      $header_old[0],
      $row[0],
    ];
    for ($index = 1; $index < count($row); $index++) {
      $rows[] = [
        'data' => [
          $header_old[$index],
          $row[$index]['data'],
        ],
      ];
    }
  }
  else {
    $rows = isset($row) ? [
      $row,
    ] : [];
  }
  $elements[] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];
  return $elements;
}