You are here

public function BlazyManager::getEntityView in Blazy 8

Returns the entity view, if available.

Parameters

object $entity: The entity being rendered.

array $settings: The settings containing view_mode.

string $fallback: The fallback content when all fails, probably just entity label.

Return value

array|bool The renderable array of the view builder, or false if not applicable.

File

src/BlazyManager.php, line 345

Class

BlazyManager
Implements a public facing blazy manager.

Namespace

Drupal\blazy

Code

public function getEntityView($entity = NULL, array $settings = [], $fallback = '') {
  if ($entity instanceof EntityInterface) {
    $entity_type_id = $entity
      ->getEntityTypeId();
    $view_hook = $entity_type_id . '_view';
    $view_mode = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
    $langcode = $entity
      ->language()
      ->getId();

    // If module implements own {entity_type}_view.
    if (function_exists($view_hook)) {
      return $view_hook($entity, $view_mode, $langcode);
    }
    elseif ($this
      ->getEntityTypeManager()
      ->hasHandler($entity_type_id, 'view_builder')) {
      return $this
        ->getEntityTypeManager()
        ->getViewBuilder($entity_type_id)
        ->view($entity, $view_mode, $langcode);
    }
    elseif ($fallback) {
      return [
        '#markup' => $fallback,
      ];
    }
  }
  return FALSE;
}