BlazyVideoTrait.php in Blazy 8
File
src/Dejavu/BlazyVideoTrait.php
View source
<?php
namespace Drupal\blazy\Dejavu;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\blazy\BlazyMedia;
trait BlazyVideoTrait {
public function imageFactory() {
return \Drupal::service('image.factory');
}
public static function videoEmbedMediaManager() {
if (function_exists('video_embed_field_theme')) {
return \Drupal::service('video_embed_field.provider_manager');
}
return FALSE;
}
public function buildVideo(array &$settings = [], $external_url = '') {
$settings['input_url'] = empty($settings['input_url']) ? $external_url : $settings['input_url'];
if (!($video = self::videoEmbedMediaManager())) {
return;
}
if (!($provider = $video
->loadProviderFromInput($settings['input_url']))) {
return;
}
$provider
->downloadThumbnail();
$render = $provider
->renderEmbedCode(640, 360, '0');
$old_url = isset($render['#attributes']) && isset($render['#attributes']['src']) ? $render['#attributes']['src'] : '';
$embed_url = isset($render['#url']) ? $render['#url'] : $old_url;
$query = isset($render['#query']) ? $render['#query'] : [];
unset($query['autoplay'], $query['auto_play']);
$settings['video_id'] = $provider::getIdFromInput($settings['input_url']);
$settings['embed_url'] = Url::fromUri($embed_url, [
'query' => $query,
])
->toString();
$settings['scheme'] = $video
->loadDefinitionFromInput($settings['input_url'])['id'];
$settings['uri'] = $provider
->getLocalThumbnailUri();
$settings['type'] = 'video';
$url = $settings['embed_url'];
if (strpos($url, 'autoplay') === FALSE || strpos($url, 'autoplay=0') !== FALSE) {
$settings['autoplay_url'] = strpos($url, '?') === FALSE ? $url . '?autoplay=1' : $url . '&autoplay=1';
}
if (empty($settings['image_style'])) {
if ($data = @getimagesize($settings['uri'])) {
list($settings['width'], $settings['height']) = $data;
}
}
}
public function getImageItem($file) {
$data = [];
$entity = $file;
if (isset($file->entity) && !isset($file->alt)) {
$entity = $file->entity;
}
if (!$entity instanceof File) {
return $data;
}
list($type, ) = explode('/', $entity
->getMimeType(), 2);
$uri = $entity
->getFileUri();
if ($type == 'image' && ($image = $this
->imageFactory()
->get($uri)) && $image
->isValid()) {
$item = new \stdClass();
$item->target_id = $entity
->id();
$item->width = $image
->getWidth();
$item->height = $image
->getHeight();
$item->alt = $entity
->getFilename();
$item->title = $entity
->getFilename();
$item->uri = $uri;
$settings = (array) $item;
$item->entity = $entity;
$settings['type'] = 'image';
$data['item'] = $item;
$data['settings'] = $settings;
unset($item);
}
return $data;
}
public function getMediaItem(array &$data = [], $entity = NULL) {
$settings = $data['settings'];
$media = $entity;
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);
$media = $this
->blazyManager()
->getEntityTypeManager()
->getStorage('media')
->load($media_id);
}
}
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
->hasField('thumbnail')) {
$item = $media
->get('thumbnail')
->first();
$settings['file_tags'] = [
'file:' . $item->target_id,
];
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;
if (strpos($input_url, 'soundcloud') === FALSE) {
$this
->buildVideo($settings);
}
}
elseif (isset($value[0]['alt']) || is_null($value[0]['alt'])) {
$settings['type'] = 'image';
}
if (empty($settings['type']) && ($build = BlazyMedia::build($media, $settings))) {
$content[] = $build;
}
}
$data['item'] = $item;
$data['settings'] = $settings;
$data['content'] = $content;
}
}
Traits
Name |
Description |
BlazyVideoTrait |
A Trait common for optional Media Entity and Video Embed Media integration. |