forked from phase2/oa_media
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oa_media.module
189 lines (167 loc) · 5.39 KB
/
oa_media.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* @file
* Code for the Open Atrium Media feature.
*/
include_once 'oa_media.features.inc';
/**
* @file
*/
/**
* Override theme function for Media Colorbox
* to fix image resizing
* copied from http://drupal.org/node/1514406
*/
function oa_media_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['media_colorbox']['function'])) {
$theme_registry['media_colorbox']['function'] = 'oa_media_media_colorbox';
}
}
/**
* Implements hook_field_formatter_info().
*/
function oa_media_field_formatter_info() {
return array(
'oa_media_formatter' => array(
'label' => t('First Image'),
'field types' => array('file'),
'settings' => array(
'image_style' => 'thumbnail',
),
)
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function oa_media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
// Grab the instance settings for this view mode.
$display = $instance['display'][$view_mode];
// Grab configuration options for this formatter.
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'oa_media_formatter') {
// Build options array.
$styles = image_styles();
$style_options = array();
foreach ($styles as $style) {
$style_options[$style['name']] = $style['name'];
}
$element['image_style'] = array(
'#type' => 'select',
'#title' => t('Image Style'),
'#description' => t('Select the image style to use for this formatter'),
'#default_value' => $settings['image_style'],
'#options' => $style_options,
);
}
return $element;
}
/**
* Implements hook_field_formatter_view().
*/
function oa_media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($display['type'] == 'oa_media_formatter') {
foreach ($items as $delta => $item) {
if ($item['type'] == 'image') {
$settings = $display['settings'];
$image = theme('image_style', array('style_name' => $settings['image_style'], 'path' => $item['uri']));
$element[0] = array(
'#markup' => '<div class="oa-media-first-thumbnail">' . $image . "</div>",
);
break;
}
}
}
return $element;
}
/**
* Override for theme_media_colorbox.
* We need to override the paths to support private file system access
*/
function oa_media_media_colorbox($variables) {
$entity_id = $variables['entity_id'];
$file_id = $variables['file_id'];
$field = $variables['field'];
$field_name = isset($field['field_name']) ? $field['field_name'] : '';
$settings = $variables['display_settings'];
//switch to figure out where caption should come from
switch ($settings['colorbox_caption']) {
case 'title':
$caption = $variables['title'];
break;
case 'mediafield':
$caption = $variables['media_colorbox_caption'];
break;
default:
$caption = '';
}
// Shorten the caption for the example styles or when caption shortening is active.
$colorbox_style = variable_get('colorbox_style', 'default');
$trim_length = variable_get('colorbox_caption_trim_length', 75);
if ((variable_get('colorbox_caption_trim', 0)) && (drupal_strlen($caption) > $trim_length)) {
$caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
}
// Build the gallery id.
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
break;
case 'field_page':
$gallery_id = 'gallery-' . $field_name;
break;
case 'custom':
$gallery_id = $settings['colorbox_gallery_custom'];
break;
default:
$gallery_id = '';
}
$class = 'media-colorbox ' . $variables['item_class'];
//load file and render for select view mode
if ($file_id != NULL) {
$file = file_load($file_id);
$fview = file_view($file, $settings['file_view_mode'], $variables['langcode']);
// override the path with the proper private file access
if ($file->type == 'image') {
$variables['path'] = file_create_url($file->uri);
}
elseif (($file->type == 'document') &&
isset($fview['file']['#theme']) && ($fview['file']['#theme'] == 'file_entity_download_link')) {
// don't use colorbox popup for document download links
$variables['path'] = file_create_url($file->uri);
$class = '';
}
$text = drupal_render($fview);
}
elseif (isset($variables['item'])) {
$text = drupal_render($variables['item']);
}
//strip anchor tags as rendered output will be wrapped by another anchor tag
//fix for issue #1477662
$stripped_text = media_colorbox_strip_only($text, 'a');
$output = theme('link', array(
//'text' => drupal_render($variables['item']),
'text' => $stripped_text,
'path' => $variables['path'],
'options' => array(
'html' => TRUE,
'attributes' => array(
'title' => $caption,
'class' => $class,
'style' => $variables['item_style'],
'rel' => $gallery_id,
'data-mediaColorboxFixedWidth' => $settings['fixed_width'],
'data-mediaColorboxFixedHeight' => $settings['fixed_height'],
'data-mediaColorboxAudioPlaylist' => $settings['audio_playlist'],
),
),
));
return $output;
}