public function BlazyVideoTrait::getMediaItem in Blazy 8
Same name and namespace in other branches
- 8.2 src/Dejavu/BlazyVideoTrait.php \Drupal\blazy\Dejavu\BlazyVideoTrait::getMediaItem()
Gets the Media item thumbnail, or re-associate the file entity to ME.
Parameters
array $data: An array of data containing settings, and potential video thumbnail item.
object $entity: The media entity, else file entity to be associated to media, if any.
File
- src/
Dejavu/ BlazyVideoTrait.php, line 153
Class
- BlazyVideoTrait
- A Trait common for optional Media Entity and Video Embed Media integration.
Namespace
Drupal\blazy\DejavuCode
public function getMediaItem(array &$data = [], $entity = NULL) {
$settings = $data['settings'];
$media = $entity;
// Core File stores Media thumbnails, re-associate it to Media entity.
// @todo: If any proper method to get video URL from image URI, or FID.
if ($entity
->getEntityTypeId() == 'file' && !empty($settings['uri']) && strpos($settings['uri'], 'video_thumbnails') !== FALSE) {
if ($media_id = \Drupal::entityQuery('media')
->condition('thumbnail.target_id', $entity
->id())
->execute()) {
$media_id = reset($media_id);
/** @var \Drupal\media_entity\Entity\Media $entity */
$media = $this
->blazyManager()
->getEntityTypeManager()
->getStorage('media')
->load($media_id);
}
}
// Only proceed if we do have ME.
if ($media
->getEntityTypeId() != 'media') {
return;
}
$item = NULL;
$bundle = $media
->bundle();
$config = method_exists($media, 'getSource') ? $media
->getSource()
->getConfiguration() : $media
->getType()
->getConfiguration();
$source = isset($config['source_url_field']) ? $config['source_url_field'] : '';
$source_field[$bundle] = isset($config['source_field']) ? $config['source_field'] : $source;
$settings['bundle'] = $bundle;
$settings['source_field'] = $source_field[$bundle];
$settings['media_url'] = $media
->toUrl()
->toString();
$settings['media_id'] = $media
->id();
$settings['view_mode'] = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
// If Media entity has a defined thumbnail, add it to data item.
if ($media
->hasField('thumbnail')) {
$item = $media
->get('thumbnail')
->first();
$settings['file_tags'] = [
'file:' . $item->target_id,
];
// Provides thumbnail URI for EB selection with various Media entities.
if (empty($settings['uri'])) {
$settings['uri'] = ($entity = $item->entity) && empty($item->uri) ? $entity
->getFileUri() : $item->uri;
}
}
$content = [];
if ($settings['source_field'] && $media
->hasField($settings['source_field'])) {
$value = $media->{$settings['source_field']}
->getValue();
$input_url = isset($value[0]['uri']) ? $value[0]['uri'] : (isset($value[0]['value']) ? $value[0]['value'] : '');
$input_url = trim(strip_tags($input_url));
if ($input_url) {
$settings['input_url'] = $input_url;
// Soundcloud has different source_field name: source_url_field.
if (strpos($input_url, 'soundcloud') === FALSE) {
$this
->buildVideo($settings);
}
}
elseif (isset($value[0]['alt']) || is_null($value[0]['alt'])) {
$settings['type'] = 'image';
}
// Do not proceed if it has type, already managed by theme_blazy().
// Supports other Media entities: Facebook, Instagram, Twitter, etc.
if (empty($settings['type']) && ($build = BlazyMedia::build($media, $settings))) {
$content[] = $build;
}
}
// Collect what's needed for clarity.
$data['item'] = $item;
$data['settings'] = $settings;
$data['content'] = $content;
}