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 self::wrap($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 $attributes = &$item['#attributes'];
61 $iframe = isset($item['#tag']) && $item['#tag'] == 'iframe';
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;
71 // Wraps the media item to allow consistency for EB/SB.
73 '#theme' => 'container',
75 '#attributes' => ['class' => ['media']],
76 '#settings' => $settings,
79 if (!empty($settings['bundle'])) {
80 $build['#attributes']['class'][] = 'media--' . str_replace('_', '-', $settings['bundle']);
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']);
88 // Currently known media entities using iframe: Instagram.
90 $build['#attributes']['class'][] = 'media--ratio';
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 . '%';
98 $build['#attributes']['class'][] = 'media--rendered';
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"];