You are here

public function WishlistPurchaseDefaultFormatter::viewElements in Commerce Wishlist 8.3

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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/WishlistPurchaseDefaultFormatter.php, line 78

Class

WishlistPurchaseDefaultFormatter
Plugin implementation of the 'commerce_wishlist_purchase_default' formatter.

Namespace

Drupal\commerce_wishlist\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [
    '#type' => 'table',
    '#caption' => $this
      ->t('Purchases'),
    '#header' => [
      $this
        ->t('Order ID'),
      $this
        ->t('Quantity'),
      $this
        ->t('Purchased'),
    ],
    '#cache' => [
      'contexts' => [
        'languages:' . LanguageInterface::TYPE_INTERFACE,
      ],
    ],
  ];

  /** @var \Drupal\commerce_wishlist\Plugin\Field\FieldType\WishlistPurchaseItem $item */
  foreach ($items as $item) {
    $purchase = $item
      ->toPurchase();
    $elements['#rows'][] = [
      $purchase
        ->getOrderId(),
      $purchase
        ->getQuantity(),
      $this->dateFormatter
        ->format($purchase
        ->getPurchasedTime()),
    ];
  }
  return $elements;
}