3 namespace Drupal\blazy;
5 use Drupal\image\Entity\ImageStyle;
8 * Provides extra media utilities without dependencies on Media Entity, etc.
13 * Builds the media field which cannot be displayed using theme_blazy().
15 * Some use URLs from inputs, some local files.
17 * @param object $media
18 * The media being rendered.
21 * The renderable array of the media field, or false if not applicable.
23 public static function build($media, $settings = []) {
24 // Prevents fatal error with disconnected internet when having ME Facebook,
25 // ME SlideShare, resorted to static thumbnails to avoid broken displays.
26 if (!empty($settings['input_url'])) {
27 // @todo: Remove when ME Facebook alike handles this.
29 $response = \Drupal::httpClient()->get($settings['input_url']);
31 catch (\Exception $e) {
36 $build = $media->get($settings['source_field'])->view($settings['view_mode']);
37 $build['#settings'] = $settings;
39 return isset($build[0]) ? self::wrap($build) : $build;
43 * Returns a field to be wrapped by theme_container().
45 * Currently Instagram, and SlideShare are known to use iframe, and thus can
46 * be converted into a responsive Blazy with fluid ratio. The rest are
47 * returned as is, only wrapped by .media wrapper for consistency with complex
48 * interaction like EB.
51 * The source renderable array $field.
54 * The new renderable array of the media item wrapped by theme_container().
56 public static function wrap(array $field = []) {
57 // Media entity is a single being, reasonable to work with multi-value?
59 $settings = isset($field['#settings']) ? $field['#settings'] : [];
60 $iframe = isset($item['#tag']) && $item['#tag'] == 'iframe';
63 if (isset($item['#attributes'])) {
64 $attributes = &$item['#attributes'];
67 // Converts iframes into lazyloaded ones.
68 if ($iframe && !empty($attributes['src'])) {
69 $attributes['data-src'] = $attributes['src'];
70 $attributes['class'][] = 'b-lazy media__iframe media__element';
71 $attributes['src'] = 'about:blank';
72 $attributes['allowfullscreen'] = TRUE;
75 // Wraps the media item to allow consistency for EB/SB.
77 '#theme' => 'container',
79 '#attributes' => ['class' => ['media']],
80 '#settings' => $settings,
83 if (!empty($settings['bundle'])) {
84 $build['#attributes']['class'][] = 'media--' . str_replace('_', '-', $settings['bundle']);
87 // Adds helper for Entity Browser small thumbnail selection.
88 if (!empty($settings['thumbnail_style']) && !empty($settings['uri'])) {
89 $build['#attributes']['data-thumb'] = ImageStyle::load($settings['thumbnail_style'])->buildUrl($settings['uri']);
92 // See comment above for known media entities using iframe.
94 $build['#attributes']['class'][] = 'media--ratio';
96 if (!empty($attributes['width']) && !empty($attributes['height'])) {
97 $padding_bottom = round((($attributes['height'] / $attributes['width']) * 100), 2);
98 $build['#attributes']['style'] = 'padding-bottom: ' . $padding_bottom . '%';
102 $build['#attributes']['class'][] = 'media--rendered';
105 // Clone relevant keys as field wrapper is no longer in use.
106 foreach (['attached', 'cache'] as $key) {
107 if (isset($field["#$key"])) {
108 $build["#$key"] = $field["#$key"];