You are here

public function TestDisplayVariant::build in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php \Drupal\display_variant_test\Plugin\DisplayVariant\TestDisplayVariant::build()

Builds and returns the renderable array for the display variant.

The variant can contain cacheability metadata for the configuration that was passed in setConfiguration(). In the build() method, this should be added to the render array that is returned.

Return value

array A render array for the display variant.

Overrides VariantInterface::build

File

core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php, line 85

Class

TestDisplayVariant
Provides a display variant that requires configuration.

Namespace

Drupal\display_variant_test\Plugin\DisplayVariant

Code

public function build() {
  $config = $this
    ->getConfiguration();
  if (empty($config['required_configuration'])) {
    throw new \Exception('Required configuration is missing!');
  }
  $contexts = $this
    ->getContexts();
  if (!isset($contexts['context'])) {
    throw new \Exception('Required context is missing!');
  }
  $build = [];
  $build['content']['default'] = [
    '#markup' => $config['required_configuration'] . ' ' . $contexts['context']
      ->getContextValue(),
  ];
  CacheableMetadata::createFromObject($this)
    ->applyTo($build);
  return $build;
}