f285a7a4e018cde4baca858a1b37037bc586cba9
[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 isset($build[0]) ? self::wrap($build) : $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     $iframe     = isset($item['#tag']) && $item['#tag'] == 'iframe';
61     $attributes = [];
62
63     if (isset($item['#attributes'])) {
64       $attributes = &$item['#attributes'];
65     }
66
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;
73     }
74
75     // Wraps the media item to allow consistency for EB/SB.
76     $build = [
77       '#theme'      => 'container',
78       '#children'   => $item,
79       '#attributes' => ['class' => ['media']],
80       '#settings'   => $settings,
81     ];
82
83     if (!empty($settings['bundle'])) {
84       $build['#attributes']['class'][] = 'media--' . str_replace('_', '-', $settings['bundle']);
85     }
86
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']);
90     }
91
92     // See comment above for known media entities using iframe.
93     if ($iframe) {
94       $build['#attributes']['class'][] = 'media--ratio';
95
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 . '%';
99       }
100     }
101     else {
102       $build['#attributes']['class'][] = 'media--rendered';
103     }
104
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"];
109       }
110     }
111
112     return $build;
113   }
114
115 }