forked from drupalprojects/panopoly_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panopoly_widgets.spotlight.inc
355 lines (312 loc) · 11.1 KB
/
panopoly_widgets.spotlight.inc
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* @file
* A specification for the custom spotlight entity that is part of Panopoly Widgets
*/
/**
* Implements hook_field_info().
*/
function panopoly_widgets_field_info() {
return array(
'panopoly_spotlight' => array(
'label' => t('Spotlight'),
'description' => t('A wonderfully widgetsal spotlight field'),
'default_widget' => 'panopoly_spotlight_form',
'default_formatter' => 'panopoly_spotlight_default',
'settings' => array(
'uri_scheme' => variable_get('file_default_scheme', 'public'),
'default_image' => 1,
),
'instance_settings' => array(
'file_extensions' => 'jpg jpeg gif png',
'file_directory' => 'general',
'max_filesize' => '',
'alt_field' => 0,
'title_field' => 0,
'max_resolution' => '',
'min_resolution' => '',
),
),
);
}
/**
* Implements hook_field_is_empty()
*/
function panopoly_widgets_field_is_empty($item, $field) {
if (empty($item['title']) && empty($item['link']) && empty($item['description']) && empty($item['fid'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_widget_info().
*/
function panopoly_widgets_field_widget_info() {
return array(
'panopoly_spotlight_form' => array(
'label' => t('Spotlight'),
'field types' => array('panopoly_spotlight'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
/**
* Implements hook_field_settings_form()
*/
function panopoly_widgets_field_settings_form($field, $instance, $has_data) {
$form = array();
$defaults = field_info_field_settings($field['type']);
$settings = array_merge($defaults, $field['settings']);
$scheme_options = array();
foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) {
$scheme_options[$scheme] = $stream_wrapper['name'];
}
$form['uri_scheme'] = array(
'#type' => 'radios',
'#title' => t('Upload destination'),
'#options' => $scheme_options,
'#default_value' => $settings['uri_scheme'],
'#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
);
// When the user sets the scheme on the UI, even for the first time, it's
// updating a field because fields are created on the "Manage fields"
// page. So image_field_update_field() can handle this change.
$form['default_image'] = array(
'#title' => t('Default image'),
'#type' => 'managed_file',
'#description' => t('If no image is uploaded, this image will be shown on display.'),
'#default_value' => $field['settings']['default_image'],
'#upload_location' => $settings['uri_scheme'] . '://default_images/',
);
return $form;
}
/**
* Implements hook_field_instance_settings_form()
*/
function panopoly_widgets_field_instance_settings_form($field, $instance) {
$form = image_field_instance_settings_form($field, $instance);
return $form;
}
/**
* Implementation of hook_field_formatter_info().
*/
function panopoly_widgets_field_formatter_info() {
return array(
'panopoly_spotlight_default' => array(
'label' => t('Default'),
'field types' => array('panopoly_spotlight'),
'settings' => array(
'image_style' => 'panopoly_image_spotlight',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function panopoly_widgets_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'panopoly_spotlight_default') {
$element['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#options' => image_style_options(),
'#default_value' => $settings['image_style'],
'#required' => TRUE,
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function panopoly_widgets_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = t('Image Style: @style', array('@style' => $settings['image_style']));
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function panopoly_widgets_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
$formatter = $display['type'];
drupal_add_library('system', 'ui.tabs');
// Assemble the tabs header
$header = '<ul>';
foreach ($items as $delta => $item_data) {
$header .= '<li>' . '<a href="#panopoly-spotlight-' . $delta . '">' . ($delta+1) . '</a></li>';
}
$header .= '</ul>';
// Assemble the tabs content
foreach ($items as $delta => $item_data) {
$element[$delta]['#markup'] = (($delta == 0) ? $header : '') . theme('panopoly_spotlight_view', array('items' => $item_data, 'delta' => $delta, 'settings' => $settings));
}
return $element;
}
/**
* Implements hook_widget_form().
*/
function panopoly_widgets_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'panopoly_spotlight_form') {
// Make it a multipart form
$form['#attributes']['enctype'] = 'multipart/form-data';
// Define the element
$element['title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : NULL,
);
$element['link'] = array(
'#title' => t('Link'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['link']) ? $items[$delta]['link'] : NULL,
);
$element['fid'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#upload_location' => file_field_widget_uri($field, $instance),
'#default_value' => isset($items[$delta]['fid']) ? $items[$delta]['fid'] : NULL,
);
$element['description'] = array(
'#title' => t('Description'),
'#type' => 'textarea',
'#rows' => '2',
'#resizable' => FALSE,
'#default_value' => isset($items[$delta]['description']) ? $items[$delta]['description'] : NULL,
);
}
return $element;
}
/**
* Implements hook_field_validate().
*/
function panopoly_widgets_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!panopoly_widgets_field_is_empty($item, $field) && empty($item['fid'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'panopoly_widgets_spotlight_image_required',
'message' => t('!name field is required.', array('!name' => t('Image'))),
);
}
}
}
/**
* Implements hook_field_presave()
*/
function panopoly_widgets_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
// We need to set presave values if fids are not set. This shouldn't effect save operations
// form API validation will prevent this from ever happening unless previewing.
if (!empty($items)) {
foreach ($items as &$item) {
if (empty($item['fid'])) {
$fid = variable_get('panopoly_widgets_default_' . $entity->bundle . '_fid', NULL);
if (!empty($fid)) {
$item['fid'] = $fid;
}
}
}
image_field_presave($entity_type, $entity, $field, $instance, $langcode, $items);
}
}
/**
* Implements hook_field_load().
*/
function panopoly_widgets_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
$entity_items_copy = $items;
image_field_load($entity_type, $entities, $field, $instances, $langcode, $items, $age);
// image_field_load() will overwrite 'title' with the image title. Here we
// return it to the item title.
foreach ($entity_items_copy as $entity_id => $items_copy) {
foreach ($items_copy as $delta => $item) {
$items[$entity_id][$delta]['title'] = $item['title'];
}
}
}
/**
* Implements hook_field_insert().
*/
function panopoly_widgets_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
image_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
}
/**
* Implements hook_field_update().
*/
function panopoly_widgets_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
// Load the original entity, if $entity->original is not already set.
// This is done as per http://drupal.org/node/985642 and can be removed when that patch is committed.
if (empty($entity->original)) {
$entity->original = entity_load_unchanged($entity_type, $field['id']);
}
image_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
}
/**
* Implements hook_field_delete().
*/
function panopoly_widgets_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
image_field_delete($entity_type, $entity, $field, $instance, $langcode, $items);
}
/**
* Implements hook_field_delete_revision().
*/
function panopoly_widgets_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
image_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, $items);
}
/**
* Implements hook_field_prepare_view().
*/
function panopoly_widgets_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
// If there are no files specified at all, use the default.
// TODO - make sure this works
foreach ($entities as $id => $entity) {
if (empty($items[$id]) && $field['settings']['default_image']) {
if ($file = file_load($field['settings']['default_image'])) {
$items[$id][0] = (array) $file + array(
'is_default' => TRUE,
'alt' => '',
'title' => '',
);
}
}
}
}
/**
* Theme function for table view
*/
function theme_panopoly_spotlight_view($variables) {
$title = $variables['items']['title'];
$description = $variables['items']['description'];
$link = $variables['items']['link'];
$settings = $variables['settings'];
if (module_exists('uuid')) {
$image_entity = entity_uuid_load('file', array($variables['items']['uuid']));
$image = file_load(array_pop($image_entity)->fid);
}
else {
$image = (object) $variables['items'];
}
$output = '<div id="' . 'panopoly-spotlight-' . $variables['delta'] . '" class="' . 'panopoly-spotlight' . '">';
$output .= theme('image_style', array('style_name' => $settings['image_style'], 'path' => $image->uri));
$output .= '<div class="panopoly-spotlight-wrapper">';
if (!empty($title) || !empty($link)) {
if (empty($title)) {
$title = t('Link');
}
$output .= '<h3 class="panopoly-spotlight-label">' . (empty($link) ? check_plain($title) : l($title, $link)) . '</h3>';
}
if (!empty($description)) {
$output .= '<div class="panopoly-spotlight-info">';
$output .= '<p>' . $description . '</p>';
$output .= '</div>';
}
$output .= '</div>';
$output .= '</div>';
return $output;
}