You are here

public function EntityBrowserBlock::build in Entity Browser Block 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/EntityBrowserBlock.php, line 280

Class

EntityBrowserBlock
Defines a generic entity browser block type.

Namespace

Drupal\entity_browser_block\Plugin\Block

Code

public function build() {
  $build = [];
  $view_builders = [];
  $entities = self::loadEntitiesByIDs($this->configuration['entity_ids']);
  foreach ($entities as $id => $entity) {
    if (empty($entity)) {
      continue;
    }
    $entity_type_id = $entity
      ->getEntityTypeId();
    if (!isset($view_builders[$id])) {
      $view_builders[$id] = $this->entityTypeManager
        ->getViewBuilder($entity_type_id);
    }
    if ($entity && $entity
      ->access('view')) {
      if (isset(static::$recursiveRenderDepth[$id])) {
        static::$recursiveRenderDepth[$id]++;
      }
      else {
        static::$recursiveRenderDepth[$id] = 1;
      }
      if (static::$recursiveRenderDepth[$id] > static::RECURSIVE_RENDER_LIMIT) {
        return $build;
      }
      $build[] = $view_builders[$id]
        ->view($entity, $this->configuration['view_modes'][$id]);
    }
  }
  return $build;
}