You are here

public function BlazyEntity::build in Blazy 8.2

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

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

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 array.

Overrides BlazyEntityInterface::build

File

src/BlazyEntity.php, line 64

Class

BlazyEntity
Provides common entity utilities to work with field details.

Namespace

Drupal\blazy

Code

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

  // Supports core Media via Drupal\blazy\BlazyOEmbed::getMediaItem().
  $data['settings'] = empty($data['settings']) ? [] : $data['settings'];
  $this->blazyManager
    ->getCommonSettings($data['settings']);
  $this->oembed
    ->getMediaItem($data, $entity);
  $settings =& $data['settings'];

  // Made Responsive image also available outside formatters here.
  if (!empty($settings['resimage']) && $settings['ratio'] == 'fluid') {
    $this->blazyManager
      ->setResponsiveImageDimensions($settings, FALSE);
  }

  // Only pass to Blazy for known entities related to File or Media.
  if (in_array($entity
    ->getEntityTypeId(), [
    'file',
    'media',
  ])) {

    /** @var Drupal\image\Plugin\Field\FieldType\ImageItem $item */
    if (empty($data['item'])) {
      $data['content'][] = $this
        ->getEntityView($entity, $settings, $fallback);
    }

    // Pass it to Blazy for consistent markups.
    $build = $this->blazyManager
      ->getBlazy($data);

    // 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);
      $build['#attached'] = empty($build['#attached']) ? $load : NestedArray::mergeDeep($build['#attached'], $load);
    }
  }
  else {
    $build = $this
      ->getEntityView($entity, $settings, $fallback);
  }
  $this->blazyManager
    ->getModuleHandler()
    ->alter('blazy_build_entity', $build, $entity, $settings);
  return $build;
}