You are here

public function ExtraFieldBlock::build in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/Plugin/Block/ExtraFieldBlock.php \Drupal\layout_builder\Plugin\Block\ExtraFieldBlock::build()

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 BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

core/modules/layout_builder/src/Plugin/Block/ExtraFieldBlock.php, line 121

Class

ExtraFieldBlock
Provides a block that renders an extra field from an entity.

Namespace

Drupal\layout_builder\Plugin\Block

Code

public function build() {
  $entity = $this
    ->getEntity();

  // Add a placeholder to replace after the entity view is built.
  // @see layout_builder_entity_view_alter().
  $extra_fields = $this->entityFieldManager
    ->getExtraFields($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  if (!isset($extra_fields['display'][$this->fieldName])) {
    $build = [];
  }
  else {
    $build = [
      '#extra_field_placeholder_field_name' => $this->fieldName,
      // Always provide a placeholder. The Layout Builder will NOT invoke
      // hook_entity_view_alter() so extra fields will not be added to the
      // render array. If the hook is invoked the placeholder will be
      // replaced.
      // @see ::replaceFieldPlaceholder()
      '#markup' => $this
        ->t('Placeholder for the @preview_fallback', [
        '@preview_fallback' => $this
          ->getPreviewFallbackString(),
      ]),
    ];
  }
  CacheableMetadata::createFromObject($this)
    ->applyTo($build);
  return $build;
}