f1fd6b215bb9245d1f71e3b59475c146ed54e9bc
[yaffs-website] / web / modules / contrib / blazy / src / BlazyMedia.php
1 <?php
2
3 namespace Drupal\blazy;
4
5 use Drupal\image\Entity\ImageStyle;
6
7 /**
8  * Provides extra media utilities without dependencies on Media Entity, etc.
9  */
10 class BlazyMedia {
11
12   /**
13    * Builds the media field which cannot be displayed using theme_blazy().
14    *
15    * Some use URLs from inputs, some local files.
16    *
17    * @param object $media
18    *   The media being rendered.
19    *
20    * @return array|bool
21    *   The renderable array of the media field, or false if not applicable.
22    */
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.
28       try {
29         $response = \Drupal::httpClient()->get($settings['input_url']);
30       }
31       catch (\Exception $e) {
32         return FALSE;
33       }
34     }
35
36     $build = $media->get($settings['source_field'])->view($settings['view_mode']);
37     $build['#settings'] = $settings;
38
39     return self::wrap($build);
40   }
41
42   /**
43    * Returns a field to be wrapped by theme_container().
44    *
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.
49    *
50    * @param array $field
51    *   The source renderable array $field.
52    *
53    * @return array
54    *   The new renderable array of the media item wrapped by theme_container().
55    */
56   public static function wrap(array $field = []) {
57     // Media entity is a single being, reasonable to work with multi-value?
58     $item       = $field[0];
59     $settings   = isset($field['#settings']) ? $field['#settings'] : [];
60     $attributes = &$item['#attributes'];
61     $iframe     = isset($item['#tag']) && $item['#tag'] == 'iframe';
62
63     // Converts iframes into lazyloaded ones.
64     if ($iframe && !empty($attributes['src'])) {
65       $attributes['data-src'] = $attributes['src'];
66       $attributes['class'][] = 'b-lazy media__iframe media__element';
67       $attributes['src'] = 'about:blank';
68       $attributes['allowfullscreen'] = TRUE;
69     }
70
71     // Wraps the media item to allow consistency for EB/SB.
72     $build = [
73       '#theme'      => 'container',
74       '#children'   => $item,
75       '#attributes' => ['class' => ['media']],
76       '#settings'   => $settings,
77     ];
78
79     if (!empty($settings['bundle'])) {
80       $build['#attributes']['class'][] = 'media--' . str_replace('_', '-', $settings['bundle']);
81     }
82
83     // Adds helper for Entity Browser small thumbnail selection.
84     if (!empty($settings['thumbnail_style']) && !empty($settings['uri'])) {
85       $build['#attributes']['data-thumb'] = ImageStyle::load($settings['thumbnail_style'])->buildUrl($settings['uri']);
86     }
87
88     // Currently known media entities using iframe: Instagram.
89     if ($iframe) {
90       $build['#attributes']['class'][] = 'media--ratio';
91
92       if (!empty($attributes['width']) && !empty($attributes['height'])) {
93         $padding_bottom = round((($attributes['height'] / $attributes['width']) * 100), 2);
94         $build['#attributes']['style'] = 'padding-bottom: ' . $padding_bottom . '%';
95       }
96     }
97     else {
98       $build['#attributes']['class'][] = 'media--rendered';
99     }
100
101     // Clone relevant keys as field wrapper is no longer in use.
102     foreach (['attached', 'cache'] as $key) {
103       if (isset($field["#$key"])) {
104         $build["#$key"] = $field["#$key"];
105       }
106     }
107
108     return $build;
109   }
110
111 }