function EntityField::build

Same name and namespace in other branches
  1. 4.0.x modules/ctools_block/src/Plugin/Block/EntityField.php \Drupal\ctools_block\Plugin\Block\EntityField::build()

Overrides BlockPluginInterface::build

File

modules/ctools_block/src/Plugin/Block/EntityField.php, line 136

Class

EntityField
Provides a block to a field on an entity.

Namespace

Drupal\ctools_block\Plugin\Block

Code

public function build() {
    
    /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
    $entity = $this->getContextValue('entity');
    $build = [];
    if ($entity->hasField($this->fieldName)) {
        
        /** @var \Drupal\Core\Field\FieldItemListInterface $field */
        $field = $entity->{$this->fieldName};
        $display_settings = $this->getConfiguration()['formatter'];
        $build['field'] = $field->view($display_settings);
    }
    // Set the cache data appropriately.
    $build['#cache']['contexts'] = $this->getCacheContexts();
    $build['#cache']['tags'] = $this->getCacheTags();
    $build['#cache']['max-age'] = $this->getCacheMaxAge();
    return $build;
}