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

Breadcrumb

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

function FieldBlock::build

Overrides BlockPluginInterface::build

File

core/modules/layout_builder/src/Plugin/Block/FieldBlock.php, line 161

Class

FieldBlock
Provides a block that renders a field from an entity.

Namespace

Drupal\layout_builder\Plugin\Block

Code

public function build() {
    $display_settings = $this->getConfiguration()['formatter'];
    $display_settings['third_party_settings']['layout_builder']['view_mode'] = $this->getContextValue('view_mode');
    $entity = $this->getEntity();
    try {
        $build = [];
        $view = $entity->get($this->fieldName)
            ->view($display_settings);
        if ($view) {
            $build = [
                $view,
            ];
        }
    } catch (EnforcedResponseException $e) {
        throw $e;
    } 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::createFromRenderArray($build)->addCacheableDependency($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