You are here

public function EntityBlock::build in Entity Blocks 8

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

src/Plugin/Block/EntityBlock.php, line 159

Class

EntityBlock
Provides the block for similar articles.

Namespace

Drupal\entity_block\Plugin\Block

Code

public function build() {
  if ($entity_id = $this->configuration['entity']) {
    if (($entity = $this->entityStorage
      ->load($entity_id)) && $entity
      ->access('view')) {
      $render_controller = \Drupal::entityTypeManager()
        ->getViewBuilder($entity
        ->getEntityTypeId());
      $view_mode = isset($this->configuration['view_mode']) ? $this->configuration['view_mode'] : 'default';
      return $render_controller
        ->view($entity, $view_mode);
    }
  }
  return [];
}