-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblockreference.module
More file actions
633 lines (529 loc) · 17 KB
/
Copy pathblockreference.module
File metadata and controls
633 lines (529 loc) · 17 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?php
/**
* @file
* The main module file for Block Reference.
*/
define('BLOCKREFERENCE_AC_REGEX', '#\[([\w\-]+):([\w\-]+)\]$#');
require_once __DIR__ . '/blockreference.field.inc';
/**
* Implements hook_menu().
*/
function blockreference_menu() {
$items = array();
$items['blockreference/autocomplete/%/%/%/%'] = array(
'title' => 'blockreference autocomplete',
'page callback' => 'blockreference_autocomplete',
'page arguments' => array(2, 3, 4, 5),
'access callback' => 'blockreference_autocomplete_access',
'access arguments' => array(2, 3, 4, 5),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Access callback for the autocomplete path.
*/
function blockreference_autocomplete_access($entity_type, $bundle, $field_name, $eid) {
// The autocomplete access check will fail for paragraph based blockreference
// fields unless the 'paragraphs_bundle_permissions' module is enabled. If
// it isn't enabled, then the user will have access whether creating or
// updating, so we can give access to autocomplete.
if ($entity_type == 'paragraphs_item' && !module_exists('paragraphs_bundle_permissions')) {
return TRUE;
}
else {
// If it isn't a paragraph or if the 'paragraphs_bundle_permissions' is
// enabled, then we proceed with existing checks.
if ($eid == 0) {
$entity_info = entity_get_info($entity_type);
$entity_dummy = entity_create($entity_type, array(
$entity_info['entity keys']['bundle'] => $bundle,
));
global $user;
$entity_access = $entity_dummy->access('create', $user);
}
else {
$entities = entity_load($entity_type, array($eid));
$entity = reset($entities) ?: FALSE;
global $user;
$entity_access = $entity->access('update', $user);
}
return user_access('access content') && $entity_access;
}
}
/**
* Menu callback for the autocomplete results.
*/
function blockreference_autocomplete($entity_type, $bundle, $field_name, $entity_id, $search_string) {
$search_string = trim(implode('/', array_slice(func_get_args(), 4)), '/');
$entity = FALSE;
if ($entity_id) {
$entity_load = entity_load($entity_type, array($entity_id));
if ($entity_load) {
$entity = reset($entity_load);
}
}
$instance = field_info_instance($entity_type, $field_name, $bundle);
$context = array(
'type' => 'autocomplete',
'entity' => $entity,
);
$blocks = _blockreference_find_blocks($instance, $search_string, $context);
$options = _blockreference_options($blocks, TRUE);
backdrop_json_output($options);
}
/**
* Value callback for a blockreference autocomplete element.
*/
function blockreference_autocomplete_value($element, $input = FALSE, $form_state = array()) {
$ac_string = $input !== FALSE ? $input : (isset($element['#value']) ? $element['#value'] : (string) @$element['#default_value']);
$module = $delta = '';
// Find module & delta from an ac string.
if (is_string($ac_string)) {
$block_arr = _blockreference_block_from_ac_string($ac_string);
if ($block_arr) {
list($module, $delta) = array_values($block_arr);
}
}
// Found it? Does is exist?
if ($module && $delta) {
$infos = module_invoke($module, 'block_info');
if (isset($infos[$delta])) {
$block = _blockreference_block($module, $delta, $infos[$delta]);
$value = trim($block->info) . ' [' . $block->module . ':' . $block->delta . ']';
return $value;
}
}
return '';
}
/**
* Validation callback for a blockreference autocomplete element.
*/
function blockreference_autocomplete_validate($element, &$form_state, $form) {
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$value = $element['#value'];
if (!$value) {
form_set_value($element, '', $form_state);
return;
}
$block_arr = _blockreference_block_from_ac_string($value);
// Validate input to be a block.
if (!$block_arr) {
return form_error($element, t('%name: Title mismatch. Please check your selection.', array('%name' => $instance['label'])));
}
// Validate block's usage in this context.
$allowed_blocks = _blockreference_find_blocks($instance, '', array(
'entity' => $element['#entity'],
'type' => 'autocomplete',
));
$moddelta = implode(':', $block_arr);
if (!isset($allowed_blocks[$moddelta])) {
return form_error($element, t('This block is not allowed in this field/context.'));
}
form_set_value($element, $moddelta, $form_state);
}
/**
* Implements hook_theme().
*/
function blockreference_theme() {
return array(
'blockreference_formatter_default' => array(
'variables' => array('element' => NULL),
),
'blockreference_formatter_without_title' => array(
'variables' => array('element' => NULL),
),
'blockreference_formatter_title' => array(
'variables' => array('element' => NULL),
),
'blockreference_formatter_plain' => array(
'variables' => array('element' => NULL),
),
'blockreference_formatter_config_link' => array(
'variables' => array('element' => NULL),
),
);
}
/**
* Theme function for 'default' blockreference field formatter.
*/
function theme_blockreference_formatter_default($variables) {
$element = $variables['element'];
$block = _blockreference_formatter_get_block($element);
if ($block) {
$build = _blockreference_get_renderable_array($block);
return backdrop_render($build);
}
return '';
}
/**
* Theme function for 'without_title' blockreference field formatter.
*/
function theme_blockreference_formatter_without_title($variables) {
$element = $variables['element'];
$block = _blockreference_formatter_get_block($element);
if ($block) {
$build = _blockreference_get_renderable_array($block);
$build['#content']->title = NULL;
return backdrop_render($build);
}
return '';
}
/**
* Theme function for 'plain' blockreference field formatter.
*/
function theme_blockreference_formatter_plain($variables) {
$element = $variables['element'];
$block = _blockreference_formatter_get_block($element);
if ($block) {
$info = module_invoke($block->module, 'block_info');
return $info[$block->delta]['info'];
}
return '';
}
/**
* Theme function for 'title' blockreference field formatter.
*/
function theme_blockreference_formatter_title($variables) {
$element = $variables['element'];
$block = _blockreference_formatter_get_block($element);
if ($block) {
$build = _blockreference_get_renderable_array($block);
$build['#content']->content = NULL;
return backdrop_render($build);
}
return '';
}
/**
* Theme function for 'config_link' blockreference field formatter.
*/
function theme_blockreference_formatter_config_link($variables) {
$element = $variables['element'];
$item = $element['item'];
if (@$item['moddelta']) {
list($module, $delta) = explode(':', $item['moddelta']);
$settings = $element['display']['settings'];
$label = '';
switch ($settings['label']) {
case 'custom':
$label = t($settings['custom_label']);
break;
case 'info':
$label = theme_blockreference_formatter_plain($variables);
break;
case 'config':
$label = db_query('
SELECT title
FROM {block}
WHERE module = ? AND delta = ?
', array($module, $delta))->fetchField();
break;
case 'rendered':
$label = theme_blockreference_formatter_title($variables);
break;
}
if (!$label) {
$label = t('configure block');
}
return l($label, 'admin/structure/block/manage/' . $module . '/' . $delta . '/configure');
}
return '';
}
/**
* The referenceable blocks in field instance settings.
*/
function _blockreference_get_block_modules_from_field($instance, $field = NULL) {
$modules = @$instance['settings']['blockreference_modules'];
if (!$modules) {
$field || $field = field_info_field($instance['field_name']);
$modules = @$field['settings']['referenceable_modules'];
}
$modules = backdrop_map_assoc(array_keys(array_filter($modules)));
return (array) $modules;
}
/**
* The autocomplete match method in field instance settings.
*/
function _blockreference_get_ac_match_method_from_field($instance, $field = NULL) {
$method = @$instance['widget']['settings']['blockreference_ac_match_method'];
return $method ?: 'contains';
}
/**
* Return options list of modules that provide blocks, keyed by module machine
* name.
*/
function _blockreference_get_block_modules() {
$modules = &backdrop_static(__FUNCTION__);
if (!$modules) {
// Get modules that define blocks.
$modules = module_implements('block_info');
$modules = array_flip($modules);
// And get their pretty names.
$all_modules = system_list('module_enabled');
foreach ($modules as $machine_name => $foo) {
$modules[$machine_name] = @$all_modules[$machine_name]->info['name'] ?: $machine_name;
}
natcasesort($modules);
}
return $modules;
}
/**
* Helper to create an autocomplete string from a pretty block.
*/
function _blockreference_block_string($block) {
if (!$block) {
return '';
}
$moddelta = $block->module . ':' . $block->delta;
$label = $block->info;
return $label . ' [' . $moddelta . ']';
}
/**
* Helper to return a [module => ..., delta => ...] array from an AC string.
*/
function _blockreference_block_from_ac_string($ac_string) {
if (preg_match(BLOCKREFERENCE_AC_REGEX, $ac_string, $match)) {
$module = $match[1];
$delta = $match[2];
return compact('module', 'delta');
}
}
/**
* Helper to create a pretty block object from a moddelta.
*/
function _blockreference_block($module, $delta, $info = NULL) {
if (!$info) {
$infos = module_invoke($module, 'block_info');
$info = @$infos[$delta];
}
if ($info) {
$info = compact('module', 'delta') + $info;
$info['info'] = trim($info['info']);
return (object) $info;
}
}
/**
* Helper to find blocks that potentially match a search string, in the right
* order, altered by custom modules.
*/
function _blockreference_find_blocks($instance, $search_string = '', $context = array()) {
$all_block_infos = &backdrop_static(__FUNCTION__, array());
// Generate unique instance id.
$iid = 'field.instance.' . $instance['entity_type'] . '.' . $instance['bundle'] . '.' . $instance['field_name'];
// Cache ALL potential block objects for this instance.
if (!isset($all_block_infos[$iid])) {
$referenceable_modules = _blockreference_get_block_modules_from_field($instance);
$all_block_infos[$iid] = array();
foreach (module_implements('block_info') as $module) {
if (!$referenceable_modules || isset($referenceable_modules[$module])) {
if ($blocks = module_invoke($module, 'block_info')) {
foreach ($blocks as $delta => $info) {
$moddelta = $module . ':' . $delta;
$all_block_infos[$iid][$moddelta] = _blockreference_block($module, $delta, $info);
}
}
}
}
}
$blocks = $all_block_infos[$iid];
$context['instance'] = $instance;
backdrop_alter('blockreference_blocks_pre', $blocks, $context);
// Filter by search string.
if ($search_string) {
$check_string = backdrop_strtolower($search_string);
foreach ($blocks as $moddelta => $block) {
if (strpos(backdrop_strtolower($block->info), $check_string) === FALSE) {
unset($blocks[$moddelta]);
}
}
}
// Sort.
uasort($blocks, function($a, $b) {
return strnatcasecmp($a->info, $b->info);
});
backdrop_alter('blockreference_blocks_post', $blocks, $context);
return $blocks;
}
/**
* Helper to make pretty options from block objects.
*/
function _blockreference_options($blocks, $ac_style = FALSE) {
$options = array();
foreach ($blocks as $block) {
$moddelta = $block->module . ':' . $block->delta;
$label = $block->info;
$value = $ac_style ? _blockreference_block_string($block) : $moddelta;
$options[$value] = $label;
}
return $options;
}
/**
* Implements hook_options_list().
*/
function blockreference_options_list($field, $instance, $entity_type, $entity) {
$context = array(
'type' => 'options_list',
'entity' => $entity,
);
$blocks = _blockreference_find_blocks($instance, '', $context);
return _blockreference_options($blocks);
}
/**
* Helper function for theming normal block views, returns appropriate block.
*/
function _blockreference_formatter_get_block($element) {
$item = $element['item'];
if (@$item['moddelta']) {
list($module, $delta) = explode(':', $item['moddelta']);
return _blockreference_block($module, $delta);
}
return FALSE;
}
/**
* Implements hook_entity_view().
*
* Pre-render blockreference fields attached to entities to ensure forms are
* rendered early enough.
*/
function blockreference_entity_view($entity, $entity_type, $view_mode, $langcode) {
if (empty($entity->content)) {
return;
}
foreach (element_children($entity->content) as $field_name) {
$field = $entity->content[$field_name];
// This is a blockreference field.
if (isset($field['#field_type']) && $field['#field_type'] == 'blockreference') {
// Unset renderable field items.
foreach (element_children($field) as $n) {
unset($entity->content[$field_name][$n]);
}
// Render.
$html = render($field);
// Add back to entity content.
$keep_meta = array_flip(array(
'#weight',
'#title',
'#access',
'#label_display',
'#field_name',
'#entity_type',
'#bundle',
));
$entity->content[$field_name] = array('#markup' => $html) + array_intersect_key($field, $keep_meta);
}
}
}
/**
* Implements hook_migrate_api().
*/
function blockreference_migrate_api() {
return array(
'api' => 2,
'field handlers' => array('MigrateBlockReferenceFieldHandler'),
);
}
/**
* Implements hook_block_usage().
*/
function blockreference_block_usage($module, $delta) {
$moddelta = $module . ':' . $delta;
// Cycle through potential fields/sources to find field items.
$fields = field_info_field_map();
$entity_loads = $loads_by_field_name = array();
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'blockreference') {
$table_name = 'field_data_' . $field_name;
$column_name = $field_name . '_moddelta';
// Query this field table for above bids.
$items = db_query("SELECT entity_type, entity_id FROM $table_name WHERE $column_name = :moddelta", array(
':moddelta' => $moddelta,
));
// Collect entity_type's and entity_id's associatively.
foreach ($items as $item) {
$entity_loads[$item->entity_type][] = $item->entity_id;
$loads_by_field_name[$item->entity_type][$item->entity_id][] = $field_name;
}
}
}
// Load entities and stack 'em up into results.
$matches = array();
foreach ($entity_loads as $entity_type => $ids) {
$entities = entity_load($entity_type, $ids);
foreach ($entities as $id => $entity) {
$uri = entity_uri($entity_type, $entity);
$path = $uri['path'];
$options = @$uri['options'] ?: array();
$fields = $loads_by_field_name[$entity_type][$id];
$matches[] = l($entity_type . ' # ' . $id, $path, $options) . ' (' . implode(', ', $fields) . ')';
}
}
return $matches;
}
/**
* Implements hook_entityreference_link_config().
*/
function blockreference_entityreference_link_config() {
return array(
'blockreference' => array(
'value_column' => 'moddelta',
'links_callback' => '_blockreference_entityreference_link_links',
),
);
}
/**
* Helper to make simple links from blocks for entityreference_link.
*/
function _blockreference_entityreference_link_links($entity_type, $moddeltas, $options, $context, $element) {
$links = array();
foreach ($moddeltas as $moddelta) {
list($module, $delta) = explode(':', $moddelta);
$block = _blockreference_block($module, $delta);
if ($block) {
$links[] = l($block->info, 'admin/structure/block/manage/' . $module . '/' . $delta . '/configure', $options);
}
}
return $links;
}
/**
*
*/
function _blockreference_get_renderable_array($block) {
$plugin_name = $block->module . ':' . $block->delta;
$data = array();
// Load Block entity.
if (!isset($block->class)) {
$block = new BlockLegacy($plugin_name, $data);
}
else {
$block = new $block->class($plugin_name, $data);
}
// Prepare block.
$block->prepare();
// Get block content
// Copied from layout_renderer_standard.inc.
$content = $block->getContent();
if (is_string($content)) {
$content = array(
'#markup' => $content,
);
}
$data = array(
'title' => $block->getTitle(),
'content' => $content,
);
// Allow altering of the title and content by other modules.
backdrop_alter(array(
'block_view',
'block_view_' . $block->module . '_' . str_replace('-', '_', $block->delta),
), $data, $block);
// Assemble a structured content array.
$content = (object) array(
'title' => $data['title'],
'content' => $data['content'],
);
return array(
'#theme' => 'block',
'#content' => $content,
'#block' => $block,
);
}