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