-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubox.module
988 lines (809 loc) · 32.5 KB
/
ubox.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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
<?php
/**
* @file
* Code for the Userfriendly box feature.
*/
include_once('ubox.features.inc');
/**
* Table of content
*
* Defines & init
* Menu router & permissions
* Ubox node API
* Forms
* Theme
* Blocks / Ubox Area
* Variable
* Boost
* Helpers
*
*/
/* *********************************************************************
* DEFINES & INIT
* ****************************************************************** */
define('UBOX_WEIGHT_MAX', 20);
/* *********************************************************************
* MENU ROUTER & PERMISSIONS
* ****************************************************************** */
/**
* Implementation of hook_menu().
*/
function ubox_menu() {
$items = array();
// Ubox configuration page
$items['admin/config/content/ubox'] = array(
'title' => 'Userfriendly Box',
'page callback' => 'drupal_get_form',
'page arguments' => array('ubox_admin_form'),
'access arguments' => array('administer ubox settings'),
'type' => MENU_NORMAL_ITEM,
'file' => "ubox.admin.inc",
);
// Block admin pages.
$items['admin/structure/block/add-ubox-area'] = array(
'title' => 'Add Ubox area',
'description' => 'Add a new Ubox area.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ubox_add_ubox_area_form'),
'access arguments' => array('administer ubox area'),
'type' => MENU_LOCAL_ACTION,
'file' => 'ubox.admin.inc',
);
$default_theme = variable_get('theme_default', 'bartik');
foreach (list_themes() as $key => $theme) {
if ($key != $default_theme) {
$items['admin/structure/block/list/' . $key . '/add-ubox-area'] = array(
'title' => 'Add Ubox area',
'description' => 'Add a new Ubox area.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ubox_add_ubox_area_form'),
'access arguments' => array('administer ubox area'),
'type' => MENU_LOCAL_ACTION,
'file' => 'ubox.admin.inc',
);
}
}
$items['admin/structure/block/delete-ubox-area'] = array(
'title' => 'Delete Ubox area',
'page callback' => 'drupal_get_form',
'page arguments' => array('ubox_delete_ubox_area_form'),
'access arguments' => array('administer ubox area'),
'type' => MENU_CALLBACK,
'file' => 'ubox.admin.inc',
);
return $items;
} // ubox_menu
/**
* Implements hook_permission().
*/
function ubox_permission() {
return array(
'administer ubox area' => array(
'title' => t('Administer ubox area'),
),
'administer ubox settings' => array(
'title' => t('Administer ubox area'),
),
'alter ubox search query' => array(
'title' => t("Alter ubox search query"),
'description' => t("Have acces to the query alter field in ubox node form.")
),
);
}
/* *********************************************************************
* UBOX NODE API
* ****************************************************************** */
/**
* Implements hook_field_extra_fields().
*/
function ubox_field_extra_fields() {
$extra['node']['ubox']['display'] = array(
'ubox' => array(
'label' => t('Ubox'),
'weight' => 0,
),
);
return $extra;
} // ubox_field_extra_fields
/**
* Implements hook_node_view().
*/
function ubox_node_view($node, $view_mode, $langcode) {
if($node->type == 'ubox' && in_array($view_mode, array('teaser', 'full', 'ubox_embed'))){
if (!empty($node->field_ubox_content)) {
module_load_include('inc', 'ubox', 'ubox.node_view');
// Prepare node
ubox_node_view_prepare($node, $view_mode, $langcode);
switch($node->field_ubox_content[LANGUAGE_NONE][0]['value']) {
case 'custom':
ubox_node_view_custom($node, $node->ubox_template, $langcode);
break;
case 'existing':
ubox_node_view_existing($node, $node->ubox_template, $langcode);
break;
case 'search':
ubox_node_view_search($node, $node->ubox_template, $langcode);
break;
}
// Apply custom theme function to our node
$node->content['#theme'] = 'ubox__'.$node->ubox_template;
$node->content['#attached']['css'][] = drupal_get_path('module', 'ubox') . '/css/ubox.css';
$node->content['ubox'] = array();
if (!empty($node->field_ubox_title) && $view_mode != 'full') {
$node->content['ubox']['ubox_title'] = array(
'#theme' => 'html_tag',
'#tag' => 'h2',
'#value' => check_plain($node->field_ubox_title[LANGUAGE_NONE][0]['value']),
'#attributes' => array('class' => array('ubox-title')),
'#weight' => 0,
);
}
$node->content['ubox']['ubox_header'] = array(
'#theme' => 'ubox_link__header',
'#zone' => 'header',
'#link' => (!empty($node->field_ubox_header_link)) ? $node->field_ubox_header_link[LANGUAGE_NONE][0] : array(),
'#weight' => 1,
);
$node->content['ubox']['ubox_footer'] = array(
'#theme' => 'ubox_link__footer',
'#zone' => 'footer',
'#link' => (!empty($node->field_ubox_footer_link)) ? $node->field_ubox_footer_link[LANGUAGE_NONE][0] : array(),
'#weight' => 3,
);
if (!empty($node->items)) {
$node->content['ubox']['ubox_content'] = array(
'#prefix' => '<div class="ubox-content">', '#suffix' => '</div>',
'items' => $node->items,
'#weight' => 2,
);
$node->content['ubox']['ubox_content']['items']['#weight'] = 10;
if (isset($node->exposed_form) && $node->exposed_form !== FALSE) {
$node->content['ubox']['ubox_content']['exposed_form'] = array(
'#weight' => 5,
'form' => drupal_get_form('ubox_exposed_filter_form', $node)
);
}
}
} // empty($node->field_ubox_content)
}
elseif (strpos($view_mode, 'ubox_') === 0) {
module_load_include('inc', 'ubox', 'ubox.node_view');
ubox_node_view_uboxitem($node, $view_mode, $langcode);
}
} // ubox_node_view
/**
* Implements hook_node_presave().
*/
function ubox_node_presave($node) {
if($node->type == "ubox"){
// Create image_style presets if not exist
if (!empty($node->field_ubox_display_region[LANGUAGE_NONE])) {
$preset = $node->field_ubox_display_region[LANGUAGE_NONE][0]['value'] . "-" . str_replace(',','_',$node->field_ubox_template_size[LANGUAGE_NONE][0]['value']) . "-" . $node->field_ubox_template_count[LANGUAGE_NONE][0]['value'] . "-ubox_" . $node->field_ubox_template[LANGUAGE_NONE][0]['value'];
$image_style = image_style_load($preset);
// If no presets, create it
if(empty($image_style)){
$image_style = image_style_load('thumbnail');
$image_style['name'] = $preset;
image_style_save($image_style);
}
}
}
} // ubox_node_presave
/**
* Implements hook_node_delete()
* Remove 'content_existing' field referencing deleted node
*/
function ubox_node_delete($node) {
// Get ubox referencing the deleted node
$result = db_query("SELECT entity_id FROM {field_revision_field_ubox_content_existing} WHERE entity_type = 'node' AND bundle='ubox' AND field_ubox_content_existing_target_id = :nid", array(':nid'=>$node->nid));
$results = db_select('field_revision_field_ubox_content_existing', 'ce')
->fields('ce', array('entity_id'))
->condition('entity_type', "node", '=')
->condition('bundle', "ubox", '=')
->condition('field_ubox_content_existing_target_id', $node->nid, '=')
->execute()
->fetchAll();
foreach($results as $result) {
$ubox = node_load($result->entity_id);
if ($ubox->type == 'ubox') {
$items = field_get_items('node', $ubox, 'field_ubox_content_existing');
if (!empty($items)) {
foreach($items as $k => $item) {
if ($item['target_id'] == $node->nid) {
unset($ubox->field_ubox_content_existing[LANGUAGE_NONE][$k]);
break;
}
}
}
node_save($ubox);
}
}
} // ubox_node_delete
/* *********************************************************************
* FORMS
* ****************************************************************** */
function ubox_exposed_filter_form($form, &$form_state, $ubox) {
$form = array();
$form['#tree'] = TRUE;
// Load field_collection items
$field_ubox_search_filters = field_get_items('node', $ubox, 'field_ubox_search_filters');
$field_collection_items = array();
foreach($field_ubox_search_filters as $item) {
$field_collection_items[] = $item['value'];
}
$filters = entity_load('field_collection_item', $field_collection_items);
foreach($filters as $filter) {
if (!empty($filter->field_ubox_filter_expose) && $filter->field_ubox_filter_expose[LANGUAGE_NONE][0]['value'] != '_none') {
if (!empty($filter->field_ubox_filter_type)) {
_ubox_add_exposed_filter($form, $form_state, $filter, 'field_ubox_filter_type');
}
if (!empty($filter->field_ubox_filter_terms)) {
_ubox_add_exposed_filter($form, $form_state, $filter, 'field_ubox_filter_terms');
}
}
}
$form['ubox'] = array(
'#type' => 'value', '#value' => $ubox->nid
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Search")
);
return $form;
} // ubox_exposed_filter_form
function ubox_exposed_filter_form_submit(&$form, &$form_state) {
$values['ubox'] = $form['ubox']['#value'];
$values['filters'] = $form_state['values']['filters'];
$form_state['rebuild'] = TRUE;
drupal_goto(current_path(), array('query'=>$values));
}
function _ubox_add_exposed_filter(&$form, &$form_state, $filter, $field_name) {
// Get field allowed values
$field = field_info_field($field_name);
$allowed_values = list_allowed_values($field, NULL, NULL, $filter);
// Get exposed field widget
$expose = $filter->field_ubox_filter_expose[LANGUAGE_NONE][0]['value'];
// Limit allowed values to exposed values
$exposed_values = array();
if ($expose == 'select') {
$exposed_values[0] = t("- All -");
}
foreach($filter->{$field_name}[LANGUAGE_NONE] as $value) {
$exposed_values[$value['value']] = $allowed_values[$value['value']];
}
$form['filters'][$filter->item_id][$field_name] = array(
'#type' => ($expose != 'select_multiple') ? $expose : 'select',
'#multiple' => ($expose != 'select_multiple') ? FALSE : TRUE,
'#options' => $exposed_values,
'#default_value' => (isset($_GET['filters'][$filter->item_id][$field_name])) ? $_GET['filters'][$filter->item_id][$field_name] : array()
);
$form['filters'] += array('#tree' => TRUE);
} // _ubox_add_exposed_filter
/**
* Implements hook_form_alter().
*/
function ubox_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'ubox_node_form'){
if (isset($form['#node']) && isset($form['type']['#value']) && $form_id == $form['type']['#value'] .'_node_form'){
$node = $form['#node'];
// Attach JS file for ubox-node-form
$form['#attached']['css'][] = drupal_get_path('module', 'ubox') . '/css/ubox.node-form.css';
$form['#attached']['js'][] = drupal_get_path('module', 'ubox') . '/js/ubox.node-form.js';
// Set field_ubox_display_region & field_ubox_display_weight default_value (Allowed values are set in ubox_field_widget_form_alter)
if (!empty($node->field_ubox_display_region)) {
$form['field_ubox_display_region'][$form['field_ubox_display_region']['#language']]['#default_value'][] = $node->field_ubox_display_region[$form['field_ubox_display_region']['#language']][0]['value'];
}
if (!empty($node->field_ubox_display_weight)) {
$form['field_ubox_display_weight'][$form['field_ubox_display_weight']['#language']]['#default_value'][] = $node->field_ubox_display_weight[$form['field_ubox_display_weight']['#language']][0]['value'];
}
if (!empty($node->field_ubox_template)) {
$form['field_ubox_template'][$form['field_ubox_template']['#language']]['#default_value'][] = $node->field_ubox_template[$form['field_ubox_template']['#language']][0]['value'];
}
// La taxonomy est rechargée à la sélection d'un type de contenu.
// La taxonomy rechargée ne contient que les vocabulaires liés au type de contenu sélectionné
foreach($form['field_ubox_search_filters']['und'] as $itemNumber => $field_collection) {
if(is_array($form['field_ubox_search_filters']['und'][$itemNumber]) && isset($form['field_ubox_search_filters']['und'][$itemNumber]['field_ubox_filter_type'])) {
// Get default terms only if we print display for first time or if we reload field after selecting type
if(!isset($form_state['triggering_element']) || ( isset($form_state['triggering_element'])
&& (
$form_state['triggering_element']['#name'] != 'field_ubox_search_filters_add_more'
&& $form_state['triggering_element']['#array_parents'][3] != 'remove_button'
&& $form_state['triggering_element']['#array_parents'][2] == $itemNumber
))
) {
// Get default terms
$form['field_ubox_search_filters']['und'][$itemNumber]['field_ubox_filter_terms'] = ubox_select_taxonomy_from_contenttype($form, $form_state, $itemNumber);
}
// Add ajax refresh to terms when edit types
$form['field_ubox_search_filters']['und'][$itemNumber]['field_ubox_filter_terms']['#prefix'] = '<div id="ajax-field-ubox-search-filters-' . $itemNumber . '-field-ubox-filter-terms">';
$form['field_ubox_search_filters']['und'][$itemNumber]['field_ubox_filter_terms']['#suffix'] = '</div>';
$form['field_ubox_search_filters']['und'][$itemNumber]['field_ubox_filter_type']['und']['#ajax'] = array(
'callback' => 'ubox_select_taxonomy_from_contenttype',
'wrapper' => 'ajax-field-ubox-search-filters-' . $itemNumber . '-field-ubox-filter-terms',
'method' => 'replace',
'effect' => 'fade',
);
}
}
// Restrict access to the "query alter" field
$form['field_ubox_query_alter']['#access'] = user_access('alter ubox search query');
// Custom validation handler
$form['#validate'][] = 'ssevent_ubox_node_form_validate';
}
}
}
function ubox_select_taxonomy_from_contenttype($form, $form_state, $iterator_selected = 0) {
// On est dans une soumission et pas dans l'ajout d'un element en ajax
if(isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] != 'field_ubox_search_filters_add_more' && $form_state['triggering_element']['#array_parents'][3] != 'remove_button') {
// Element multiple selectionné
$iterator_selected = $form_state['triggering_element']['#array_parents'][2];
}
// Récupère les vocabulaires pour chaque type de contenu selectionné
if(!isset($form['field_ubox_search_filters']['und'][$iterator_selected]['field_ubox_filter_type'])) {
$vocabularies = array();
}
else if(isset($form['field_ubox_search_filters']['und'][$iterator_selected]['field_ubox_filter_type']['und']['#value'])) {
$vocabularies = ubox_api_get_vocabulary($form['field_ubox_search_filters']['und'][$iterator_selected]['field_ubox_filter_type']['und']['#value']);
}
else {
// Si aucune valeur n'est présente, c'est que l'on est sur le premier affichage du formulaire, on charge donc les valeurs par défaut
$vocabularies = ubox_api_get_vocabulary($form['field_ubox_search_filters']['und'][$iterator_selected]['field_ubox_filter_type']['und']['#default_value']);
}
// Si les vocabulaires sont vides, on prend tous les vocabulaires disponibles
if(empty($vocabularies)) {
$vocabularies_default = _ubox_get_vocabularies();
if(!empty($vocabularies_default)) {
foreach($vocabularies_default as $vocabulary_default) {
$vocabularies[$vocabulary_default->vid] = $vocabulary_default;
}
}
}
$terms = array();
// Pour chaque vocabulaire, on récupère ses terms
foreach($vocabularies as $vocabulary) {
if(is_string($vocabulary)) {
// Get vocabulary and so vid from machine name
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary);
}
$terms += _ubox_get_terms_hierarchical_display($vocabulary);
}
// Get only terms authorized by administration page
$terms = array_intersect_key($terms, _ubox_get_terms());
$form['field_ubox_search_filters']['und'][$iterator_selected]['field_ubox_filter_terms']['und']['#options'] = $terms;
return $form['field_ubox_search_filters']['und'][$iterator_selected]['field_ubox_filter_terms'];
}
/**
* Helper function return all the taxonomy terms of a given node type
* @param $type
* The array of machine name of the content type that the function should look for taxonomy terms
* the array format should be : array('machine_name');
* @return
* An array of taxonomy terms containing tid => human name.
*/
function ubox_api_get_vocabulary($type = array()) {
// break if there are no types specified
if (empty($type) || !is_array($type)) {
return FALSE;
}
$output = array();
foreach (field_info_fields() as $field) {
if ($field['type'] == 'taxonomy_term_reference' && isset($field['bundles']['node']) && is_array($field['bundles']['node'])) {
foreach ($field['bundles']['node'] as $content_type) {
if (in_array($content_type, $type)) {
foreach ($field['settings']['allowed_values'] as $value) {
$output[$value['vocabulary']] = $value['vocabulary'];
}
}
}
}
}
return $output;
}
/**
* Custom validation handler for ubox node form
*/
function ssevent_ubox_node_form_validate($form, &$form_state) {
// Get filters data
$field_ubox_search_exposeonly = $form_state['values']['field_ubox_search_exposeonly'][LANGUAGE_NONE][0]['value'];
$field_ubox_search_filters = $form_state['values']['field_ubox_search_filters'][LANGUAGE_NONE];
$has_exposed_filter = FALSE;
foreach ($field_ubox_search_filters as $k => $search_filter) {
if(isset($search_filter['entity'])) {
$filter = $search_filter['entity'];
if (!empty($filter->field_ubox_filter_expose[LANGUAGE_NONE]) && $filter->field_ubox_filter_expose[LANGUAGE_NONE][0]['value'] !== '_none') {
$has_exposed_filter = TRUE;
if (!empty($filter->field_ubox_filter_exclude[LANGUAGE_NONE]) && $filter->field_ubox_filter_exclude[LANGUAGE_NONE][0]['value']) {
form_set_error('field_ubox_search_filters][und]['.$k.'][field_ubox_filter_exclude', t("Exposed filters could not be excluded"));
}
}
}
}
// ExposeOnly field needs at least one exposed filter.
if ($field_ubox_search_exposeonly && $has_exposed_filter == FALSE) {
form_set_error('field_ubox_search_exposeonly', t("You must have at least one exposed filter to check the 'Expose only' field."));
}
} // ssevent_ubox_node_form_validate
/**
* Implementing hook_field_widget_form_alter
*/
function ubox_field_widget_form_alter(&$element, &$form_state, $context) {
if(isset($element['#field_name'])) {
switch($element['#field_name']) {
case 'field_ubox_display_region':
$element['#options'] += ubox_get_ubox_area();
break;
case 'field_ubox_display_weight':
for($i=-UBOX_WEIGHT_MAX;$i<=UBOX_WEIGHT_MAX;$i++){
$element['#options'][$i] = $i;
}
break;
case 'field_ubox_template':
// Get ubox templates declared in hook_ubox_template().
$ubox_templates = module_invoke_all('ubox_template');
foreach ($ubox_templates as $template_key => $template) {
$element['#options'][$template_key] = $template['name'];
}
// Template field is required
$element['#required'] = TRUE;
unset($element['#options']['_none']);
break;
case 'field_ubox_template_size':
// Get ubox templates declared in hook_ubox_template_size().
$ubox_template_sizes = module_invoke_all('ubox_template_size');
foreach ($ubox_template_sizes as $size_key => $size) {
$element['#options'][$size_key] = $size['name'];
}
// Template field is required
$element['#required'] = TRUE;
unset($element['#options']['_none']);
break;
}
}
}
/**
* Implementation of hook_form_alter for block_admin_configure.
*/
function ubox_form_block_admin_configure_alter(&$form, &$form_state) {
if ($form['module']['#value'] == 'ubox') {
$ubox_areas = variable_get('ubox_areas', array());
// Add a delete link for custom ubox areas
if (isset($ubox_areas[$form['delta']['#value']])) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('ubox_delete_ubox_area_form_submit_redirect')
);
}
}
} // ubox_form_block_admin_configure_alter
function ubox_delete_ubox_area_form_submit_redirect(&$form, &$form_state) {
drupal_goto('admin/structure/block/delete-ubox-area/'. $form['delta']['#value']);
}
/**
* Alters the block admin form to add delete links next to ubox blocks.
*/
function ubox_form_block_admin_display_form_alter(&$form, $form_state) {
$ubox_areas = variable_get('ubox_areas', array());
foreach ($form['blocks'] as $k => $block) {
if ($block['module']['#value'] == 'ubox' && isset($ubox_areas[$block['delta']['#value']])) {
$form['blocks'][$k]['delete'] = array(
'#type' => 'link',
'#title' => t("Delete"),
'#href' => 'admin/structure/block/delete-ubox-area/'. $block['delta']['#value']
);
}
}
} // ubox_form_block_admin_display_form_alter
/* *********************************************************************
* THEME
* ****************************************************************** */
/**
* Implements hook_entity_info_alter().
*/
function ubox_entity_info_alter(&$entity_info) {
// Create aditionnal view mode for embeded ubox
$entity_info['node']['view modes'] += array(
'ubox_embed' => array(
'label' => t('Ubox embeded'),
'custom settings' => FALSE,
),
);
// Create additionnal view modes according to field_ubox_template allowed values
$field = field_info_field('field_ubox_template');
if(!empty($field) && isset($field['settings']) && isset($field['settings']['allowed_values']) && !empty($field['settings']['allowed_values'])) {
foreach($field['settings']['allowed_values'] as $key=>$label){
$entity_info['node']['view modes'] += array(
'ubox_' . $key => array(
'label' => t('Ubox: !view_mode', array('!view_mode'=>$label)),
'custom settings' => FALSE,
),
);
}
}
// Get ubox templates declared in hook_ubox_template().
$ubox_templates = module_invoke_all('ubox_template');
foreach ($ubox_templates as $template_key => $template) {
$entity_info['node']['view modes'] += array(
'ubox_' . $template_key => array(
'label' => t('Ubox: !view_mode', array('!view_mode'=>$template['name'])),
'custom settings' => FALSE,
),
);
}
} // ubox_entity_info_alter
/**
* Implementation of hook_theme().
*/
function ubox_theme($existing, $type, $theme, $path) {
$theme_path = drupal_get_path('module', 'ubox');
$themes = array(
'ubox' => array(
'render element' => 'elements',
'template' => 'templates/ubox',
'path' => $theme_path,
'file' => 'ubox.theme.inc'
),
'ubox_link' => array(
'variables' => array('link' => array(), 'zone' => NULL),
'template' => 'templates/ubox_link',
'path' => $theme_path,
'file' => 'ubox.theme.inc'
),
);
return $themes;
}
/**
* Implements hook_ubox_template().
*/
function ubox_ubox_template() {
$templates = array();
$templates['list'] = array(
'name' => "List"
);
$templates['slideshow'] = array(
'name' => "Slideshow"
);
return $templates;
} // ubox_ubox_template
/**
* Implements hook_ubox_template_size().
*/
function ubox_ubox_template_size() {
return array(
'1_1' => array('name' => "Full (1/1)"),
'1_2' => array('name' => "Half (1/2)"),
'1_3' => array('name' => "One third (1/3)"),
'2_3' => array('name' => "Two third (2/3)"),
);
} // ubox_ubox_template_size
/* *********************************************************************
* BLOCKS / UBOX AREA
* ****************************************************************** */
// See implementations of hook_ubox_area()
/**
* Implements hook_block_info().
*/
function ubox_block_info() {
$blocks = array();
// Get ubox areas declared in hook_ubox_area().
$ubox_areas = module_invoke_all('ubox_area');
foreach($ubox_areas as $ubox_area_delta => $ubox_area) {
$blocks[$ubox_area_delta] = array(
'info' => $ubox_area['name'],
'cache' => isset($ubox_area['cache']) ? $ubox_area['cache'] : DRUPAL_CACHE_PER_PAGE
);
}
// Custom Ubox area (added by form, saved in database)
$ubox_areas = variable_get('ubox_areas', array());
foreach($ubox_areas as $ubox_area_delta => $ubox_area_name) {
$blocks[$ubox_area_delta] = array(
'info' => $ubox_area_name,
'cache' => DRUPAL_CACHE_PER_PAGE
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function ubox_block_view($delta) {
global $_domain, $language;
$content = array();
/**
* Get Ubox datas to put on Ubox area (=delta)
* Show only datas to current page
*/
/**
* Select all ubox node to check which can be display
*/
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'ubox')
->propertyCondition('status', 1)
->propertyCondition('language', array($language->language, 'und'), 'IN')
->fieldCondition('field_ubox_display_region', 'value', $delta , '=')
->fieldOrderBy('field_ubox_display_weight', 'value', 'ASC');
if(!empty($_domain)){ // Domain module
$query->fieldCondition('gid', 'value', $_domain['domain_id'], '=');
}
$entities = $query->execute();
if(!empty($entities)) {
$nodes = node_load_multiple(array_keys($entities['node']));
$ubox_count = 1;
foreach($nodes as $node) {
// Vérifie que l'ubox à les droits d'être affichée
if(node_access('view', $node)) {
/**
* Pour chaque ubox récupéré, on vérifie qu'elle peut être affichée
* (prise en compte des wildcards)
*/
$current_path = $_GET['q'];
if(isset($node->field_ubox_display_pages[LANGUAGE_NONE])) {
$page_match = drupal_match_path($current_path, $node->field_ubox_display_pages[LANGUAGE_NONE][0]['value']);
}
else {
$page_match = TRUE;
}
if(isset($node->field_ubox_display_notpages[LANGUAGE_NONE])) {
$page_nodisplay_match = drupal_match_path($current_path, $node->field_ubox_display_notpages[LANGUAGE_NONE][0]['value']);
}
else {
$page_nodisplay_match = FALSE;
}
if ($page_match && !$page_nodisplay_match) {
$node->classes_array[] = "ubox-".$ubox_count;
$node->classes_array[] = ($ubox_count%2) ? "odd" : "even";
/**
* On récupère les items et les intègre au contenu de l'ubox
*/
$content[] = drupal_render(node_view($node, 'ubox_embed', $language->language));
$ubox_count++;
}
} // END NODE ACCESS
} // ENDFOREACH
} // END IF ENTITIES
// Return block
// todo: do we need drupal_render(node_view()), or could we pass a render array of ubox/nodes ?
return array(
'content' => implode($content),
);
}
/* *********************************************************************
* IMAGE STYLE
* ****************************************************************** */
/**
* Implements hook_image_default_styles().
*/
function ubox_image_default_styles() {
$styles = array();
// Get medium effect (core)
$medium = image_style_load('medium');
if (!isset($medium['effects'])) {
$medium = array('effects' => array());
}
// Get ubox data
$ubox_template = module_invoke_all('ubox_template');
$ubox_template_size = module_invoke_all('ubox_template_size');
$ubox_area = ubox_get_ubox_area();
foreach ($ubox_template as $template_key => $template) {
foreach ($ubox_template_size as $size_key => $size) {
// Imagestyle preset without ubox area
$imagestyle = array();
$imagestyle[] = 'ubox';
$imagestyle[] = $template_key;
$imagestyle[] = $size_key;
$styles[implode('--', $imagestyle)] = array('effects' => $medium['effects']);
foreach ($ubox_area as $area_key => $area_name) {
$imagestyle = array();
$imagestyle[] = 'ubox';
$imagestyle[] = $template_key;
$imagestyle[] = $size_key;
$imagestyle[] = $area_key;
$styles[implode('--', $imagestyle)] = array('effects' => $medium['effects']);
} // foreach area
} // foreach template sizes
} // foreach templates
return $styles;
} // ubox_image_default_styles
/* *********************************************************************
* VARIABLES
* ****************************************************************** */
/* *********************************************************************
* BOOST
* ****************************************************************** */
/**
* Implementation of hook_boost_clearcache_update
*/
function ubox_boost_clearcache_update($node) {
$paths = array();
// Take care of node references
switch($node->type) {
case 'ubox':
$paths += boost_clearcache_get_path_from_textarea_field($node->field_ubox_display_pages, $node->language);
$paths += boost_clearcache_get_path_from_textarea_field($node->field_ubox_display_notpages, $node->language);
break;
default: // update ubox when content references are edited
// find ubox where this entity is referenced by
$query = db_select('field_data_field_ubox_content_existing', 'fdfue');
$query->leftJoin('field_data_field_ubox_pages', 'fdfup', 'fdfup.revision_id = fdfue.revision_id');
$query->leftJoin('field_data_field_ubox_display_notpages', 'fdfup2', 'fdfup2.revision_id = fdfue.revision_id');
$query
->fields('fdfue', array('entity_id'))
->fields('fdfup', array('field_ubox_pages_value'))
->fields('fdfup2', array('field_ubox_display_notpages_value'))
->condition('fdfue.field_ubox_content_existing_target_id', $node->nid);
$entities = $query->execute();
if(!empty($entities)) {
foreach($entities as $entitynode) {
if(!empty($entitynode->field_ubox_pages_value)) {
$paths += _boost_clearcache_textarea_to_array($entitynode->field_ubox_pages_value);
}
if(!empty($entitynode->field_ubox_display_notpages_value)) {
$paths += _boost_clearcache_textarea_to_array($entitynode->field_ubox_display_notpages_value);
}
}
}
break;
}
return $paths;
}
/* *********************************************************************
* HELPERS
* ****************************************************************** */
function _ubox_get_vocabularies(){
$vocabularies = array();
foreach(variable_get('ubox_settings_taxonomy',array()) as $voc){
if($voc > 0){
$vocabulary = taxonomy_vocabulary_load($voc);
$vocabularies[$vocabulary->vid] = $vocabulary;
}
}
return $vocabularies;
}
function _ubox_get_terms() {
$terms = array();
// Get back vocabularies authorized in administration page
$vocabularies = _ubox_get_vocabularies();
foreach($vocabularies as $vocabulary){
$terms += _ubox_get_terms_hierarchical_display($vocabulary);
}
return $terms;
}
function _ubox_get_terms_hierarchical_display($vocabulary) {
$terms = array();
$terms_vocabulary = taxonomy_get_tree($vocabulary->vid);
foreach($terms_vocabulary as $term) {
// First hierarchy, no parents for this term
if($term->depth == 0) {
$term->name = $vocabulary->name . ' ]] ' . $term->name;
}
else {
// Build hierarchical display
$term_parent = $terms[$term->parents[0]];
$term->name = $term_parent . ' > ' . $term->name;
}
$terms[$term->tid] = $term->name;
}
return $terms;
}
function _ubox_get_contenttype(){
$contenttypes = array();
$ct = _node_types_build()->names;
foreach(variable_get('ubox_settings_contenttype',array_keys($ct)) as $contenttype){
if($contenttype != '0' && $contenttype != 'ubox'){
$contenttypes[$contenttype] = $ct[$contenttype];
}
}
return $contenttypes;
}
function ubox_get_ubox_area() {
// Custom Ubox area (added by form, saved in database)
$ubox_areas = variable_get('ubox_areas', array());
// Get ubox from hook_ubox_area
$hook_ubox_area = module_invoke_all('ubox_area');
foreach($hook_ubox_area as $ubox_area_delta => $ubox_area) {
$ubox_areas[$ubox_area_delta] = $ubox_area['name'];
}
return $ubox_areas;
}