/
multisite_block_carousel.module
372 lines (323 loc) · 11.9 KB
/
multisite_block_carousel.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* @file
* Module file for multisite_block_carousel.
*/
/**
* Implements hook_menu().
*/
function multisite_block_carousel_menu() {
if (module_exists('block')) {
// Add the link in block admin, for default theme.
$items['admin/structure/block/add-block-carousel-block'] = array(
'title' => 'Add carousel block',
'page callback' => 'drupal_get_form',
'page arguments' => array('multisite_block_carousel_add_block_form'),
'access arguments' => array('administer blocks'),
'type' => MENU_LOCAL_ACTION,
);
// Add the link in block admin, for every other theme.
// This has to be done in two steps or it won't work for default theme.
$default_theme = variable_get('theme_default', 'bartik');
foreach (list_themes() as $key => $theme) {
if ($key != $default_theme) {
$items['admin/structure/block/list/' . $key . '/add-block-arousel-block'] = array(
'title' => 'Add carousel block',
'page callback' => 'drupal_get_form',
'page arguments' => array('multisite_block_carousel_add_block_form'),
'access arguments' => array('administer blocks'),
'type' => MENU_LOCAL_ACTION,
);
}
}
// Manage deletion link.
$items['admin/structure/block/delete-block-carousel'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('multisite_block_carousel_delete'),
'access arguments' => array('administer blocks'),
'type' => MENU_CALLBACK,
);
}
return $items;
}
/**
* Menu callback: display the carousel block addition form.
*
* @see multisite_block_carousel_add_block_form_submit()
*/
function multisite_block_carousel_add_block_form($form, &$form_state) {
module_load_include('inc', 'block', 'block.admin');
$form = block_admin_configure($form, $form_state, 'multisite_block_carousel', NULL);
// Prevent block_add_block_form_validate/submit() from being automatically
// added because of the base form ID by providing these handlers manually.
$form['#validate'] = array();
$form['#submit'] = array('multisite_block_carousel_add_block_form_submit');
return $form;
}
/**
* Submit handler for multisite_block_carousel_add_block_form().
*
* Saves the new carousel block.
*
* @see multisite_block_carousel_add_block_form()
*/
function multisite_block_carousel_add_block_form_submit($form, &$form_state) {
// Determine the delta (ID) of the new block.
$block_ids = variable_get('block_carousel_ids', array());
$delta = empty($block_ids) ? 1 : max($block_ids) + 1;
$form_state['values']['delta'] = $delta;
// Save the new array of blocks IDs.
$block_ids[] = $delta;
variable_set('block_carousel_ids', $block_ids);
// Save the block configuration.
multisite_block_carousel_block_save($delta, $form_state['values']);
// Run the new block submission (borrowed from block_add_block_form_submit).
$query = db_insert('block')->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'region',
'status',
'weight',
'delta',
'cache',
));
foreach (list_themes() as $theme) {
if ($theme->status) {
$region = !empty($form_state['values']['regions'][$theme->name]) ? $form_state['values']['regions'][$theme->name] : BLOCK_REGION_NONE;
$query->values(array(
'visibility' => (int) $form_state['values']['visibility'],
'pages' => trim($form_state['values']['pages']),
'custom' => (int) $form_state['values']['custom'],
'title' => $form_state['values']['title'],
'module' => $form_state['values']['module'],
'theme' => $theme->name,
'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
'status' => 0,
'status' => (int) ($region != BLOCK_REGION_NONE),
'weight' => 0,
'delta' => $delta,
'cache' => DRUPAL_NO_CACHE,
));
}
}
$query->execute();
$query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
foreach (array_filter($form_state['values']['roles']) as $rid) {
$query->values(array(
'rid' => $rid,
'module' => $form_state['values']['module'],
'delta' => $delta,
));
}
$query->execute();
drupal_set_message(t('The block has been created.'));
cache_clear_all();
$form_state['redirect'] = 'admin/structure/block';
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters the block admin form to add delete links next to menu blocks.
*/
function multisite_block_carousel_form_block_admin_display_form_alter(&$form, $form_state) {
$blocks = module_invoke_all('block_carousel_blocks');
foreach (variable_get('block_carousel_ids', array()) as $delta) {
if (empty($blocks[$delta])) {
$form['blocks']['block_carousel_' . $delta]['delete'] = array(
'#type' => 'link',
'#title' => t('delete'),
'#href' => 'admin/structure/block/delete-block-carousel/' . $delta,
);
}
}
}
/**
* Form constructor for the carousel blocks delete confirmation form.
*/
function multisite_block_carousel_delete($form, &$form_state, $delta = 0) {
$form['delta'] = array('#type' => 'hidden', '#value' => $delta);
return confirm_form($form, t('Are you sure you want to delete the block?'), 'admin/structure/block', NULL, t('Delete'), t('Cancel'));
}
/**
* Form submission handler for multisite_block_carousel_delete().
*
* Deletes the carousel block.
*
* @see multisite_block_carousel_delete()
*/
function multisite_block_carousel_delete_submit($form, &$form_state) {
// Remove the menu block configuration variables.
$delta = $form_state['values']['delta'];
$block_ids = variable_get('block_carousel_ids', array());
unset($block_ids[array_search($delta, $block_ids)]);
sort($block_ids);
variable_set('block_carousel_ids', $block_ids);
variable_del("block_carousel_{$delta}_gid");
db_delete('block')
->condition('module', 'multisite_block_carousel')
->condition('delta', $delta)
->execute();
db_delete('block_role')
->condition('module', 'multisite_block_carousel')
->condition('delta', $delta)
->execute();
drupal_set_message(t('The block has been removed.'));
cache_clear_all();
$form_state['redirect'] = 'admin/structure/block';
}
/**
* Implements hook_block_info().
*/
function multisite_block_carousel_block_info() {
$blocks = array();
$deltas = variable_get('block_carousel_ids', array());
// Load every carousel block.
foreach (array_keys(module_invoke_all('block_carousel_blocks')) as $delta) {
$deltas[] = $delta;
}
foreach ($deltas as $delta) {
$blocks[$delta]['info'] = 'Mediagallery carousel block ' . strval($delta);
$blocks[$delta]['cache'] = DRUPAL_CACHE_PER_ROLE;
}
return $blocks;
}
/**
* Implements hook_block_configure().
*
* This hook declares configuration options for blocks provided by this module.
*/
function multisite_block_carousel_block_configure($delta = '') {
$form = array();
$deltas = variable_get('block_carousel_ids', array());
if (in_array($delta, $deltas) || !$delta) {
if (!$delta) {
$delta = ($deltas ? max($deltas) + 1 : 1);
}
// Get all available galleries.
$nids = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.type', 'gallerymedia')
->execute()
->fetchAllKeyed();
// Create field 'Gallery' with all available galleries.
$form["block_carousel_{$delta}_gid"] = array(
'#type' => 'select',
'#title' => t('Gallery'),
'#options' => $nids,
'#description' => t('This gallery content will be displayed in the carousel.'),
'#default_value' => variable_get("block_carousel_{$delta}_gid", t('empty')),
'#required' => TRUE,
);
}
return $form;
}
/**
* Implements hook_block_save().
*
* This hook declares how the configured options for a block provided by this
* module are saved.
*/
function multisite_block_carousel_block_save($delta = '', $edit = array()) {
$deltas = variable_get('block_carousel_ids', array());
if (in_array($delta, $deltas)) {
variable_set("block_carousel_{$delta}_gid", $edit["block_carousel_{$delta}_gid"]);
}
}
/**
* Implements hook_block_view().
*
* This hook generates the contents of the blocks themselves.
*/
function multisite_block_carousel_block_view($delta = '') {
$block = array();
$deltas = variable_get('block_carousel_ids', array());
if (in_array($delta, $deltas)) {
$block['subject'] = t('Title of first block (carousel_configurable)');
$block['content'] = multisite_block_carousel_contents($delta);
}
return $block;
}
/**
* Returns block content.
*
* @param string $delta
* The block machine name.
*
* @return string
* The content of the block.
*
* @see multisite_block_carousel_block_view()
*/
function multisite_block_carousel_contents($delta) {
global $base_url;
$deltas = variable_get('block_carousel_ids', array());
if (in_array($delta, $deltas)) {
if (variable_get("block_carousel_{$delta}_gid") == 'empty') {
return '';
}
$gall = node_load(variable_get("block_carousel_{$delta}_gid"));
$path = $base_url . '/' . (isset($gall->path['alias']) ? $gall->path['alias'] : $gall->path['source']);
$media_items = array();
$return = '';
$first = TRUE;
// Merge photos and videos.
if (isset($gall->field_picture_upload[LANGUAGE_NONE]) && isset($gall->field_video_upload[LANGUAGE_NONE])) {
$media_items = array_merge($gall->field_picture_upload[LANGUAGE_NONE], $gall->field_video_upload[LANGUAGE_NONE]);
}
elseif (isset($gall->field_picture_upload[LANGUAGE_NONE])) {
$media_items = $gall->field_picture_upload[LANGUAGE_NONE];
}
elseif (isset($gall->field_video_upload[LANGUAGE_NONE])) {
$media_items = $gall->field_video_upload[LANGUAGE_NONE];
}
// @todo This is unneeded, $media_items is already instantiated to an empty
// array.
else {
$media_items = array();
}
// Sort table.
$return .= '<div class=" carousel slide" id="block-carousel-' . $gall->nid . '">';
// Carousel slider.
$return .= '<ol class="carousel-indicators">';
foreach ($media_items as $id => $item) {
$return .= '<li data-target="#block-carousel-' . $gall->nid . '" data-slide-to="' . $id . '" class="' . ($first ? "active" : "") . '"></li>';
$first = FALSE;
}
$return .= '</ol>';
$first = TRUE;
// Carousel content.
$return .= '<div class="carousel-inner">';
foreach ($media_items as $id => $item) {
$return .= '<div class="item ' . ($first ? "active" : "") . '">';
switch ($item['type']) {
case 'image':
$thumb = image_style_url('landscape', $item['uri']);
$return .= '<a href="' . check_plain($path) . '">';
$return .= '<img src="' . $thumb . '" alt="' . check_plain($item['filename']) . '" />';
$return .= '<p class="carousel-caption">' . (isset($item['field_picture_description'][LANGUAGE_NONE][0]['safe_value']) ? filter_xss($item['field_picture_description'][LANGUAGE_NONE][0]['safe_value']) : check_plain($item['filename'])) . '</p>';
$return .= '</a>';
break;
case 'video':
$thumb = image_style_url('landscape', file_load($item['thumbnail'])->uri);
$return .= '<a href="' . $path . '">';
$return .= '<img src="' . $thumb . '" alt="' . check_plain($item['filename']) . '" />';
$return .= '<p class="carousel-caption">' . check_plain($item['filename']) . '</p>';
$return .= '</a>';
break;
default:
break;
}
$return .= '</div>';
$first = FALSE;
}
$return .= '</div>';
$return .= '<a class="carousel-control left" href="#block-carousel-' . $gall->nid . '" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>';
$return .= '<a class="carousel-control right" href="#block-carousel-' . $gall->nid . '" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>';
$return .= '</div>';
return array('#markup' => $return);
}
}