You are here

public function BlazyEntityTrait::buildPreview in Blazy 8

Build image/video preview either using theme_blazy(), or view builder.

This is alternative to Drupal\blazy\BlazyFormatterManager used outside field formatters, such as Views field, or Entity Browser displays, etc.

Parameters

array $data: An array of data containing settings, and image item.

object $entity: The media entity, else file entity to be associated to media if any.

string $fallback: The fallback string to display such as file name or entity label.

Return value

array The renderable array of theme_blazy(), or view builder, else empty.

2 calls to BlazyEntityTrait::buildPreview()
BlazyViewsFieldFile::render in src/Plugin/views/field/BlazyViewsFieldFile.php
Renders the field.
BlazyViewsFieldMedia::render in src/Plugin/views/field/BlazyViewsFieldMedia.php
Renders the field.

File

src/Dejavu/BlazyEntityTrait.php, line 74

Class

BlazyEntityTrait
A Trait common for supported entities.

Namespace

Drupal\blazy\Dejavu

Code

public function buildPreview(array $data, $entity, $fallback = '') {
  $build = [];
  if (!$entity instanceof EntityInterface) {
    return [];
  }

  // Supports VEM/ME if Drupal\blazy\Dejavu\BlazyVideoTrait is imported.
  if (method_exists($this, 'getMediaItem')) {
    $this
      ->getMediaItem($data, $entity);
  }
  $settings =& $data['settings'];
  if (!empty($data['item'])) {
    if (!empty($settings['media_switch'])) {
      $is_lightbox = $this
        ->blazyManager()
        ->getLightboxes() && in_array($settings['media_switch'], $this
        ->blazyManager()
        ->getLightboxes());
      $settings['lightbox'] = $is_lightbox ? $settings['media_switch'] : FALSE;
    }
    if (empty($settings['uri'])) {
      $settings['uri'] = ($file = $data['item']->entity) && empty($data['item']->uri) ? $file
        ->getFileUri() : $data['item']->uri;
    }

    // Provide simple Blazy, if required.
    if (empty($settings['_basic'])) {
      $build = $this
        ->blazyManager()
        ->getImage($data);
    }
    else {
      $build = [
        '#theme' => 'blazy',
        '#item' => $data['item'],
        '#settings' => $settings,
      ];
    }

    // Provides a shortcut to get URI.
    $build['#uri'] = empty($settings['uri']) ? '' : $settings['uri'];

    // Allows top level elements to load Blazy once rather than per field.
    // This is still here for non-supported Views style plugins, etc.
    if (empty($settings['_detached'])) {
      $load = $this
        ->blazyManager()
        ->attach($settings);

      // Enforces loading elements hidden by EB "Show selected" button.
      $load['drupalSettings']['blazy']['loadInvisible'] = TRUE;
      $build['#attached'] = $load;
    }
  }
  else {
    $build = $this
      ->blazyManager()
      ->getEntityView($entity, $settings, $fallback);
  }
  return $build;
}