You are here

public function BlazyEntity::getFileOrMedia in Blazy 8.2

Returns file view or media due to being empty returned by view builder.

@todo make it usable for other file-related entities.

1 call to BlazyEntity::getFileOrMedia()
BlazyEntity::getEntityView in src/BlazyEntity.php
Returns the entity view, if available.

File

src/BlazyEntity.php, line 148

Class

BlazyEntity
Provides common entity utilities to work with field details.

Namespace

Drupal\blazy

Code

public function getFileOrMedia($file, array $settings, $use_file = TRUE) {
  list($type, ) = explode('/', $file
    ->getMimeType(), 2);
  if ($type == 'video') {

    // As long as you are not being too creative by renaming or changing
    // fields provided by core, this should be your good friend.
    $settings['media_source'] = 'video_file';
    $settings['source_field'] = 'field_media_video_file';
  }
  if (!empty($settings['source_field']) && isset($settings['media_source']) && ($media = $this->blazyManager
    ->getEntityTypeManager()
    ->getStorage('media')
    ->loadByProperties([
    $settings['source_field'] => [
      'fid' => $file
        ->id(),
    ],
  ]))) {
    if ($media = reset($media)) {
      return $use_file ? BlazyMedia::build($media, $settings) : $media;
    }
  }
  return [];
}