You are here

public function BlazyEntity::getEntityView in Blazy 8.2

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.

Overrides BlazyEntityInterface::getEntityView

1 call to BlazyEntity::getEntityView()
BlazyEntity::build in src/BlazyEntity.php
Build image/video preview either using theme_blazy(), or view builder.

File

src/BlazyEntity.php, line 108

Class

BlazyEntity
Provides common entity utilities to work with field details.

Namespace

Drupal\blazy

Code

public function getEntityView($entity, array $settings = [], $fallback = '') {
  if ($entity instanceof EntityInterface) {
    $entity_type_id = $entity
      ->getEntityTypeId();
    $view_mode = $settings['view_mode'] = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
    $langcode = $entity
      ->language()
      ->getId();
    $fallback = $fallback && is_string($fallback) ? [
      '#markup' => '<div class="is-fallback">' . $fallback . '</div>',
    ] : $fallback;

    // If entity has view_builder handler.
    if ($this->blazyManager
      ->getEntityTypeManager()
      ->hasHandler($entity_type_id, 'view_builder')) {
      $build = $this->blazyManager
        ->getEntityTypeManager()
        ->getViewBuilder($entity_type_id)
        ->view($entity, $view_mode, $langcode);

      // @todo figure out why video_file empty, this is blatant assumption.
      if ($entity_type_id == 'file') {
        try {
          $build = $this
            ->getFileOrMedia($entity, $settings) ?: $build;
        } catch (\Exception $ignore) {

          // Do nothing, no need to be chatty in mischievous deeds.
        }
      }
      return $build ?: $fallback;
    }
    else {

      // If module implements own {entity_type}_view.
      // @todo remove due to being deprecated at D8.7.
      // See https://www.drupal.org/node/3033656
      $view_hook = $entity_type_id . '_view';
      if (is_callable($view_hook)) {
        return $view_hook($entity, $view_mode, $langcode);
      }
    }
  }
  return $fallback;
}