You are here

public function BlazyEntity::build in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyEntity.php \Drupal\blazy\BlazyEntity::build()

File

src/BlazyEntity.php, line 29

Class

BlazyEntity
Implements BlazyFormatterInterface.

Namespace

Drupal\blazy

Code

public function build(array $data, $entity, $fallback = '') {
  $build = [];

  // Bail out if empty.
  if (!$entity) {
    return [];
  }

  // Supports Media.
  // @todo if (method_exists($this, 'getMediaItem')) {
  // @todo $this->getMediaItem($data, $entity);
  // @todo }
  $settings =& $data['settings'];
  if (!empty($data['item'])) {
    if (!empty($settings['media_switch'])) {
      $is_lightbox = $this->formatter
        ->getLightboxes() && in_array($settings['media_switch'], $this->formatter
        ->getLightboxes());
      $settings['lightbox'] = $is_lightbox ? $settings['media_switch'] : FALSE;
    }
    if (empty($settings['uri'])) {
      $settings['uri'] = $data['item']->uri;
    }
    $build = $this->formatter
      ->getBlazy($data);

    // 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'])) {
      $build['#attached'] = $this->formatter
        ->attach($settings);
    }
  }
  else {
    $build = $this
      ->entityView($settings['target_type'], $entity, $settings, $fallback);
  }
  return $build;
}