You are here

public function VariationFieldBlock::build in Commerce Core 8.2

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides FieldBlock::build

See also

\Drupal\block\BlockViewBuilder

File

modules/product/src/Plugin/Block/VariationFieldBlock.php, line 74

Class

VariationFieldBlock
Variation field block.

Namespace

Drupal\commerce_product\Plugin\Block

Code

public function build() {
  $display_settings = $this
    ->getConfiguration()['formatter'];
  $entity = $this
    ->getEntity();
  assert($entity instanceof ProductVariationInterface);
  try {
    $build = $this->productVariationFieldRenderer
      ->renderField($this->fieldName, $entity, $display_settings);
  } catch (\Exception $e) {
    $build = [];
    $this->logger
      ->warning('The field "%field" failed to render with the error of "%error".', [
      '%field' => $this->fieldName,
      '%error' => $e
        ->getMessage(),
    ]);
  }
  CacheableMetadata::createFromObject($this)
    ->applyTo($build);
  return $build;
}