Version 1
[yaffs-website] / web / modules / contrib / media_entity_twitter / media_entity_twitter.module
1 <?php
2
3 /**
4  * @file
5  * Hook implementations for media_entity_twitter module.
6  */
7
8 /**
9  * Implements hook_theme().
10  */
11 function media_entity_twitter_theme() {
12   return [
13     'media_entity_twitter_tweet' => [
14       'variables' => ['path' => NULL, 'attributes' => []],
15     ],
16     'media_entity_twitter_tweet_thumbnail' => [
17       'variables' => [
18         'content' => '',
19         'author' => '',
20         'avatar' => NULL,
21       ],
22     ],
23   ];
24 }
25
26 /**
27  * Preprocess function for media_entity_twitter_tweet_thumbnail theme hook.
28  *
29  * @param array $variables
30  *   Variables to be injected into the template.
31  */
32 function media_entity_twitter_preprocess_media_entity_twitter_tweet_thumbnail(array &$variables) {
33   // If the avatar exists, load it directly into memory and base64 encode it.
34   // For security reasons, browsers don't always allow external images xlinked
35   // in an SVG to be displayed when the SVG is being embedded via an <img> tag.
36   // The workaround is to embed the image directly into the SVG as a base64
37   // encoded string.
38   if ($variables['avatar']) {
39     $extension = pathinfo($variables['avatar'], PATHINFO_EXTENSION);
40     $extension = strtolower($extension);
41
42     // Don't fetch the avatar if it has an unrecognized extension.
43     if (in_array($extension, ['gif', 'jpg', 'jpeg', 'png', 'webp'])) {
44       $data = file_get_contents($variables['avatar']);
45
46       if ($data) {
47         // image/jpg is not a thing.
48         if ($extension == 'jpg') {
49           $extension = 'jpeg';
50         }
51         $variables['avatar'] = 'data:image/' . $extension . ';base64,' . base64_encode($data);
52       }
53       else {
54         unset($variables['avatar']);
55       }
56     }
57     else {
58       unset($variables['avatar']);
59     }
60   }
61 }