-
Notifications
You must be signed in to change notification settings - Fork 1
/
oembedthumbnail.module
108 lines (88 loc) · 3.48 KB
/
oembedthumbnail.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
// $Id$
/**
* @file
* Includes all the necessary functions for the oEmbed thumbnails module
*/
/**
* Implements hook_field_formatter_info().
* Create a new field formatter for link fields.
*/
function oembedthumbnail_field_formatter_info() {
$formatters = array(
'oembedthumbnail' => array(
'label' => t('oEmbed Thumbnail'),
'field types' => array('link_field'),
'description' => t('Embeds links as thumbnails if possible - otherwise just links them.'),
'settings' => array('imagestyle' => ''),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
* Adds a dropdown to choose image styles on the field settings form.
*/
function oembedthumbnail_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
$element['imagestyle'] = array(
'#title' => t('Image Style'),
'#type' => 'select',
'#options' => image_style_options(),
);
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
* Adds details to the field formatting sumarry on the fields page.
*/
function oembedthumbnail_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
$summary .= t('Image Style') . ': ' . (!empty($settings['imagestyle']) ? $settings['imagestyle'] : 'default');
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function oembedthumbnail_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$attributes = array();
$url = url($item['url'], $item + array('external' => TRUE));
$attributes = array_filter($display['settings']);
$element[$delta] = oembedcore_render_cache('oembedthumbnail', $url, $attributes);
}
return $element;
}
/**
* Implements hook_element_info_alter().
* We need to make sure our pre-render function is called when generating the thumbnail.
*/
function oembedthumbnail_element_info_alter(&$types) {
$types['oembedthumbnail']['#pre_render'][] = 'oembedthumbnail_pre_render_thumbnail';
}
/**
* #pre_render callback for 'oembedthumbnail' elements.
*
* For oEmbed responses, process the thumbnail image according to the chosen image style
*/
function oembedthumbnail_pre_render_thumbnail($element) {
// Generate a remote url http://youtube.com/oembed/images/....
$remote_uri = file_create_url($element['#path']);
// Check if a remote file object exists in the {file_managed} table for this file...
$remote_file = remote_stream_wrapper_file_load_by_uri($remote_uri);
// If there is no file object, create one with the remote url 'http://youtube.com/oembed/images/.....
if (empty($remote_file)) {
$remote_file = remote_stream_wrapper_file_create_by_uri($remote_uri);
file_save($remote_file); // From Dave Reid... Save the file as file_save will be deprecated in remote_stream_wrapper_file_create_by_uri...
}
// Now generate the derivative uri, public://styles/{style_name}/etc........
$derivative_uri = remote_stream_wrapper_image_style_path($element['#display_options']['imagestyle'], $remote_file->uri);
// From that internal uri generate an internal derative url, http://mysite.com/files/styles/{style_name}/etc.....
$element['#path'] = file_create_url($derivative_uri);
return $element;
}