You are here

public function BundleItemSelectionDefaultFormatter::viewElements in Commerce Product Bundle 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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/BundleItemSelectionDefaultFormatter.php, line 102

Class

BundleItemSelectionDefaultFormatter
Plugin implementation of the 'commerce_product_bundle_item_selection_default' formatter.

Namespace

Drupal\commerce_product_bundle\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $qty = $item->qty;
    $bundle_item = $this->bundleItemStorage
      ->load($item->bundle_item)
      ->label();
    $variation = $this->productVariationStorage
      ->load($item->purchasable_entity)
      ->label();
    $elements[$delta] = [
      '#markup' => "{$bundle_item}: ({$qty}) {$variation}",
      '#cache' => [
        'contexts' => [],
      ],
    ];
  }
  return $elements;
}