You are here

public function BlazyEntity::entityView in Blazy 7

1 call to BlazyEntity::entityView()
BlazyEntity::build in src/BlazyEntity.php

File

src/BlazyEntity.php, line 72

Class

BlazyEntity
Implements BlazyFormatterInterface.

Namespace

Drupal\blazy

Code

public function entityView($entity_type, $entity, array $settings, $fallback = '') {

  // Get the correct language.
  global $language;
  $view_hook = $entity_type . '_view';
  $view_mode = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
  $langcode = empty($settings['langcode']) ? $language->language : $settings['langcode'];

  // Untranslatable fields are rendered with no language code, fall back
  // to the content language in that case.
  $langcode = $langcode !== LANGUAGE_NONE ? $langcode : NULL;

  // If module implements own {entity_type}_view.
  if (function_exists($view_hook)) {
    if ($entity_type == 'file') {

      // Add some references to the referencing entity.
      // @see https://www.drupal.org/node/2333107
      $entity->referencing_entity_type = $settings['entity_type_id'];
      $entity->referencing_field = $settings['field_name'];
    }
    return $view_hook($entity, $view_mode, $langcode);
  }
  elseif (function_exists('entity_view')) {
    return entity_view($entity_type, [
      $entity,
    ], $view_mode, $langcode);
  }
  return $fallback ? [
    '#markup' => $fallback,
  ] : [];
}