You are here

public function AjaxAddToCartFormatter::viewElements in Commerce Ajax Add to Cart 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 AddToCartFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/AjaxAddToCartFormatter.php, line 40

Class

AjaxAddToCartFormatter
Plugin implementation of the 'dc_ajax_add_cart' formatter.

Namespace

Drupal\dc_ajax_add_cart\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $elements[0]['dc_ajax_add_cart_form'] = [
    '#lazy_builder' => [
      'dc_ajax_add_cart.lazy_builders:ajaxAddToCartForm',
      [
        $items
          ->getEntity()
          ->id(),
        $this->viewMode,
        $this
          ->getSetting('combine'),
      ],
    ],
    '#create_placeholder' => TRUE,
  ];
  return $elements;
}