You are here

public function ProductVariationFieldRenderer::renderFields in Commerce Core 8.2

Renders all renderable variation fields.

Parameters

\Drupal\commerce_product\Entity\ProductVariationInterface $variation: The product variation.

string $view_mode: The view mode.

Return value

array Array of render arrays, keyed by field name.

Overrides ProductVariationFieldRendererInterface::renderFields

2 calls to ProductVariationFieldRenderer::renderFields()
ProductVariationFieldRenderer::replaceRenderedFields in modules/product/src/ProductVariationFieldRenderer.php
Replaces the rendered variation fields via AJAX.
ProductVariationFieldRendererLayoutBuilder::renderFields in modules/product/src/ProductVariationFieldRendererLayoutBuilder.php
Renders all renderable variation fields.
1 method overrides ProductVariationFieldRenderer::renderFields()
ProductVariationFieldRendererLayoutBuilder::renderFields in modules/product/src/ProductVariationFieldRendererLayoutBuilder.php
Renders all renderable variation fields.

File

modules/product/src/ProductVariationFieldRenderer.php, line 33

Class

ProductVariationFieldRenderer

Namespace

Drupal\commerce_product

Code

public function renderFields(ProductVariationInterface $variation, $view_mode = 'default') {
  $build = $this->variationViewBuilder
    ->view($variation, $view_mode);

  // Formatters aren't called until #pre_render.
  foreach ($build['#pre_render'] as $callable) {
    $build = call_user_func($callable, $build);
  }
  unset($build['#pre_render']);

  // Rendering the product can cause an infinite loop.
  unset($build['product_id']);

  // Fields are rendered individually, top-level properties are not needed.
  foreach (array_keys($build) as $key) {
    if (Element::property($key)) {
      unset($build[$key]);
    }
  }

  // Prepare the fields for AJAX replacement.
  foreach ($build as $field_name => $rendered_field) {
    $build[$field_name] = $this
      ->prepareForAjax($rendered_field, $field_name, $variation);
  }
  return $build;
}