Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ExtraFieldBlock.php

function ExtraFieldBlock::build

Overrides BlockPluginInterface::build

File

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

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal