-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_nodelist.module
2192 lines (1945 loc) · 70.9 KB
/
ding_nodelist.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
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Helps creating customizable lists of content
* that can be placed on a panel.
*/
define('NL_WIDGET_LIST', 'simple');
define('NL_WIDGET_SINGLE', 'single');
define('NL_WIDGET_CAROUSEL', 'carousel');
define('NL_WIDGET_HOR_ACCORDION', 'horizontal_accordion');
define('NL_WIDGET_VER_ACCORDION', 'vertical_accordion');
define('NL_WIDGET_SLIDER', 'slider');
define('NL_WIDGET_TAXONOMY_LIKE', 'taxonomy');
define('NL_COL_FULL', 'full');
define('NL_COL_HALF', 'half');
define('NL_COL_THIRD', 'third');
define('NL_COL_QRT', 'quarter');
define('NL_TPL_ACTIVE', 0);
define('NL_TPL_HIDDEN', 1);
define('NL_CACHE_LIFETIME', 1800);
/**
* Implements hook_menu().
*/
function ding_nodelist_menu() {
$items = array();
$items['admin/config/ding/ding_nodelist'] = array(
'title' => 'Ding nodelist',
'description' => 'View and customize lists of content.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_nodelist_settings_form'),
'access arguments' => array('configure nodelist'),
'type' => MENU_NORMAL_ITEM,
'file' => 'ding_nodelist.admin.inc',
);
$items['admin/config/ding/ding_nodelist/settings'] = array(
'title' => 'Settings',
'description' => 'Configure general settings.',
'tab_parent' => 'admin/config/ding/ding_nodelist',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'ding_nodelist.admin.inc',
);
$items['admin/config/ding/ding_nodelist/templates'] = array(
'title' => 'Templates',
'description' => 'Manage content templates.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_nodelist_templates_form'),
'access arguments' => array('configure nodelist'),
'type' => MENU_LOCAL_TASK,
'file' => 'ding_nodelist.admin.inc',
);
$items['ding_nodelist/autocomplete/%'] = array(
'page callback' => 'ding_nodelist_autocomplete',
'page arguments' => array(2),
// @todo: implements permissions for nodelist managements
//'access arguments' => array('create nodelist instances'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function ding_nodelist_permission() {
return array(
'configure nodelist' => array(
'title' => t('Configure nodelist'),
'description' => t('Configure the behavior of nodelist'),
),
'nodelist cache settings' => array(
'title' => t('Nodelist cache'),
'description' => t('Manage nodelist cache settings'),
),
// @todo: this permission is declared but not implemented.
// IPE is known to have additional non-documented features regarding permissions.
// 'create nodelist instances' => array(
// 'title' => t('Manage nodelist instances'),
// 'description' => t('Create/edit/delete nodelist instances'),
// ),
);
}
/**
* Implements hook_ctools_plugin_directory().
* This lets ctools know to scan the module for a content_type plugin file.
*/
function ding_nodelist_ctools_plugin_directory($module, $plugin) {
// we'll be nice and limit scandir() calls
if ($module == 'ctools' && $plugin == 'content_types') {
return 'plugins/content_types';
}
}
/**
* Implements hook_ajax_render_alter().
* This method is a workaround to impossiblity
* of telling Drupal the order of CSS files in order to
* cascade styles properly. See the link.
*
* @link http://drupal.org/node/561858#comment-3171846
* @global boolean $ipe_mode
* @param array $commands
* Ajax render commands.
*/
function ding_nodelist_ajax_render_alter(&$commands) {
global $ipe_mode, $ipe_redirect;
if ($ipe_mode) {
$path = drupal_get_path('module', 'ding_nodelist') . "/css/ipe.css";
$styles = '<style type="text/css" media="all">@import url("' . $path . '");</style>';
$commands[] = ajax_command_append('head', $styles);
}
if ($ipe_redirect) {
ctools_include('ajax');
ctools_add_js('ajax-responder');
$commands[] = ctools_ajax_command_redirect($ipe_redirect);
}
}
/**
* Implements hook_BASE_FORM_ID_alter().
* Add custom handler to IPE Save/Cancel buttons to force page reload.
*/
function ding_nodelist_form_panels_ipe_edit_control_form_alter(&$form, &$form_state) {
$form['buttons']['submit']['#submit'][] = '_ding_nodelist_ipe_buttons_submit';
$form['buttons']['cancel']['#submit'][] = '_ding_nodelist_ipe_buttons_submit';
// Add necessary scripts for later call in ding_nodelist_ajax_render_alter().
ctools_include('ajax');
ctools_add_js('ajax-responder');
}
/**
* Save/Cancel IPE buttons submit handler.
*/
function _ding_nodelist_ipe_buttons_submit($form, &$form_state) {
global $ipe_redirect;
$ipe_redirect = $_SERVER['HTTP_REFERER'];
}
/**
* Ctools plugin callback for settings form.
*/
function ding_nodelist_content_type_edit_form($form, &$form_state) {
drupal_add_css(drupal_get_path('module', 'ding_nodelist') . '/css/ding_nodelist-admin.css', 'file');
global $ipe_mode;
// Tell to load additional CSS to make edit form look normally
// in case of IPE.
if (strpos($form_state['form_info']['path'], '/ipe/')) {
$ipe_mode = TRUE;
}
// Cache the form for correct workflow.
$form_state['cache'] = TRUE;
// Save original form to restore it later.
// @todo: remove this workaround code and find ctools/drupal-way approach.
if (!isset($form_state['restore'])) {
$form_state['original_form'] = $form;
}
$trigger = !empty($form_state['triggering_element']) ? $form_state['triggering_element'] : NULL;
// CT/widget select change handlers.
// Process them first since they affect the rest of the form.
if ($trigger) {
if ($trigger['#name'] == 'content_type') {
// Refresh CT setting.
$node_type = $form_state['triggering_element']['#value'];
// Reset other form values.
$form_state['values']['content_type'] = $node_type;
$form_state['values']['selected_nodes'] = array();
$form_state['values']['widget_type'] = '';
$form_state['values']['taxonomy_filters'] = array();
unset($form_state['input']);
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
elseif ($trigger['#name'] == 'widget_type') {
$node_type = $form_state['values']['content_type'];
// Reset default scrolling delay.
unset($form_state['input']['autoscroll_delay']);
$form_state['values']['autoscroll_delay'] = '';
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
}
$conf = $form_state['conf'];
// Show only allowed content types.
$node_types = variable_get('ding_nodelist_node_types', array());
$all_node_types = node_type_get_names();
$node_types_opts = array();
foreach ($node_types as $nt => $active) {
if ($active) {
$node_types_opts[$nt] = $all_node_types[$nt];
}
}
if (empty($node_types_opts)) {
drupal_set_message(t('No eligible content types were found, please configure a few first.'), 'error', FALSE);
}
$node_type = isset($conf['content_type']) ? $conf['content_type'] : current(array_keys($node_types));
$default_conf = array(
'content_type' => $node_type,
'widget_type' => NL_WIDGET_CAROUSEL,
'columns' => NL_COL_FULL,
'sort_field' => 'created',
'limit' => 5,
'more_text' => t('More'),
'more_link' => '',
'teaser_length' => 100,
'autoscroll_delay' => 3000,
);
$form['#prefix'] = '<div id="ding_nodelist-form-wrapper">';
$form['#suffix'] = '</div>';
// Initial settings.
$form['ding_nodelist_wrapper'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#tree' => FALSE,
'#title' => t('Configure nodelist'),
'#collapsible' => FALSE,
);
$form['ding_nodelist_wrapper']['content_type'] = array(
'#type' => 'select',
'#title' => t('Content type'),
'#required' => TRUE,
'#default_value' => $node_type,
'#options' => $node_types_opts,
'#description' => t('Select the content type of the list\'s items.'),
'#ajax' => array(
'wrapper' => 'ding_nodelist-selected-nodes',
'callback' => 'ding_nodelist_js',
'method' => 'replace',
'effect' => 'fade',
),
);
// Set up all the available view modes for this type of nodes.
$entity = entity_get_info('node');
$node_formats = array();
foreach ($entity['view modes'] as $view_mode => $settings) {
$node_formats[$view_mode] = $settings['label'];
}
$widget_types = _ding_nodelist_get_widget_types($node_type);
$widget_type = isset($conf['widget_type']) ? $conf['widget_type'] : $default_conf['widget_type'];
$form['ding_nodelist_wrapper']['widget_type'] = array(
'#type' => 'select',
'#title' => t('List type'),
'#required' => TRUE,
'#default_value' => $widget_type,
'#options' => $widget_types,
'#empty_option' => t('- Select -'),
'#description' => t('If single item selected, only the first item will be rendered'),
'#ajax' => array(
'wrapper' => 'ding_nodelist-item-template',
'callback' => 'ding_nodelist_js_template',
'method' => 'replace',
'effect' => 'fade',
),
'#prefix' => '<div class=ding_nodelist-widget-config>',
);
$form['ding_nodelist_wrapper']['widget_dependent'] = array(
'#type' => 'container',
'#prefix' => '<div id="ding_nodelist-item-template">',
'#suffix' => '</div></div>',
);
$autoscrolling_widgets = array(NL_WIDGET_CAROUSEL, NL_WIDGET_HOR_ACCORDION);
if (in_array($widget_type, $autoscrolling_widgets)) {
$generic_autoscroll_delay = variable_get('ding_nodelist_' . $widget_type . '_delay', $default_conf['autoscroll_delay']);
$form['ding_nodelist_wrapper']['widget_dependent']['autoscroll_delay'] = array(
'#type' => 'textfield',
'#title' => t('Delay (miliseconds)'),
'#default_value' => !empty($conf['autoscroll_delay']) ? $conf['autoscroll_delay'] : $generic_autoscroll_delay,
'#description' => t('Time in milliseconds an item will be shown before switching to the next one'),
'#size' => 4,
);
}
$form['ding_nodelist_wrapper']['override_title_text'] = array(
'#type' => 'textfield',
'#title' => t('List title'),
'#description' => t('Enter a title to be shown above the list items. Leave empty to omit the title.')
. ' ' . t('You may use %keywords from contexts, as well as %title to contain the original title.'),
'#required' => FALSE,
'#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
);
// Manually controlled list.
$form['ding_nodelist_wrapper']['nodelist_content'] = array(
'#type' => 'fieldset',
'#title' => t('Content'),
);
$form['ding_nodelist_wrapper']['nodelist_content']['list_type'] = array(
'#type' => 'radios',
'#options' => array(
'custom-dynamic' => t('Custom / Dynamic'),
'all' => t('All content'),
),
'#default_value' => isset($conf['list_type']) ? $conf['list_type'] : 'all',
);
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name=list_type]' => array('value' => 'custom-dynamic'),
),
),
);
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['nodes_switch'] = array(
'#type' => 'checkbox',
'#title' => t('Custom content?'),
'#prefix' => '<hr />',
'#default_value' => isset($conf['nodes_switch']) ? $conf['nodes_switch'] : 0,
);
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_nodes'] = array(
'#type' => 'container',
'#collapsible' => FALSE,
'#tree' => FALSE,
'#title' => t('Custom item list'),
'#collapsible' => FALSE,
'#prefix' => '<div class="clear-block" id="ding_nodelist-wrapper">',
'#suffix' => '</div>',
'#states' => array(
'invisible' => array(
':input[name=nodes_switch]' => array('checked' => FALSE),
),
),
);
// Render selected nodes.
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_nodes']['selected_nodes'] = array(
'#prefix' => '<div id="ding_nodelist-selected-nodes">',
'#suffix' => '</div>',
'#theme' => '_ding_nodelist_items_table',
'#tree' => TRUE,
);
$delta = 0;
$weight = 0;
$max_weight = $weight;
$node_count = NULL;
$filter_count = NULL;
if (isset($form_state['values']['selected_nodes_count'])) {
$node_count = $form_state['values']['selected_nodes_count'];
}
else {
$node_count = empty($form_state['values']['selected_nodes']) ? 1 : count($form_state['values']['selected_nodes']);
}
if (!empty($conf['selected_nodes'])) {
$delta = count($conf['selected_nodes']);
foreach ($conf['selected_nodes'] as $key => $node) {
$weight = $node['weight'];
$max_weight = ($max_weight < $weight) ? $weight : $max_weight;
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_nodes']['selected_nodes'][$key] = _ding_nodelist_selected_node($key, $node_type, $node, $weight);
}
}
// Add new node field.
$weight = ++$max_weight;
for (; $delta < $node_count; $delta++) {
$key = $delta;
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_nodes']['selected_nodes'][$key] = _ding_nodelist_selected_node($key, $node_type, NULL, $weight);
}
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_nodes']['add_node'] = array(
// Used to identify the button in validate/submit handler(s).
'#name' => 'add_node',
'#parents' => array('ding_nodelist_nodes', 'add_node'),
'#type' => 'submit',
'#value' => t('Add content'),
'#weight' => 1,
'#submit' => array('ding_nodelist_add_node_submit'),
'#limit_validation_errors' => array(array('selected_nodes')),
'#ajax' => array(
'wrapper' => 'ding_nodelist-selected-nodes',
'callback' => 'ding_nodelist_js_add_node',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['dynamic_switch'] = array(
'#type' => 'checkbox',
'#title' => t('Dynamic content?'),
'#prefix' => '<hr />',
'#default_value' => isset($conf['dynamic_switch']) ? $conf['dynamic_switch'] : 0,
);
// Dynamic list settings.
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
':input[name=dynamic_switch]' => array('checked' => FALSE),
),
),
);
// Render selected filters.
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['taxonomy_filters'] = array(
'#type' => 'container',
'#prefix' => '<div id="ding_nodelist-taxonomy-filters">',
'#suffix' => '</div>',
'#theme' => '_ding_nodelist_filters_table',
'#tree' => TRUE,
);
if (isset($form_state['values']['taxonomy_filters_count'])) {
$filter_count = $form_state['values']['taxonomy_filters_count'];
}
else {
$filter_count = empty($form_state['values']['taxonomy_filters']) ? 1 : count($form_state['values']['taxonomy_filters']);
}
// Retrieve vocabularies current CT references.
$allowed_fields = array();
$field_vocabulary = array();
$fields = field_info_instances('node', $node_type);
foreach ($fields as $field) {
$field_config = field_info_field($field['field_name']);
// Check if the field is a term reference field.
if ($field_config['type'] == 'taxonomy_term_reference') {
$allowed_fields[$field['field_name']] = $field['label'];
$field_vocabulary[$field['field_name']] = $field_config['settings']['allowed_values'][0]['vocabulary'];
}
}
// Check if filter vocabulary was changed.
$changed_filter_id = NULL;
if (!empty($form_state['triggering_element']) && isset($form_state['triggering_element']['#parents'][2])
&& $form_state['triggering_element']['#parents'][2] == 'filter_field') {
$changed_filter_id = $form_state['triggering_element']['#parents'][1];
$changed_filter = $form_state['values']['taxonomy_filters'][$changed_filter_id];
// Update auto-complete, preserve other fields.
$filter['filter_field'] = $changed_filter['filter_field'];
$filter['filter_vocab'] = $field_vocabulary[$filter['filter_field']];
// Important: delete old input!
unset($form_state['input']['taxonomy_filters'][$changed_filter_id]);
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['taxonomy_filters'][$changed_filter_id] = _ding_nodelist_taxonomy_filter($allowed_fields, $filter, $changed_filter_id);
}
$delta = 0;
if (!empty($conf['taxonomy_filters'])) {
$delta = count($conf['taxonomy_filters']);
foreach ($conf['taxonomy_filters'] as $key => $filter) {
// Skip just changed filter.
if ($changed_filter_id === $key) {
continue;
}
$tags = array();
foreach ($filter['filter_terms'] as $tid) {
$tags[$tid] = taxonomy_term_load($tid);
}
$filter['filter_terms'] = taxonomy_implode_tags($tags);
$filter['filter_vocab'] = $field_vocabulary[$filter['filter_field']];
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['taxonomy_filters'][$key] = _ding_nodelist_taxonomy_filter($allowed_fields, $filter, $key);
}
ksort($form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['taxonomy_filters']);
}
// Add new filter field.
for (; $delta < $filter_count; $delta++) {
$key = $delta;
if ($changed_filter_id === $key) {
continue;
}
// Set default field value.
$first_field = array_shift(array_keys($allowed_fields));
$filter = array(
'filter_field' => $first_field,
'filter_vocab' => $field_vocabulary[$first_field],
);
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['taxonomy_filters'][$key] = _ding_nodelist_taxonomy_filter($allowed_fields, $filter, $key);
}
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['add_filter'] = array(
'#name' => 'add_filter',
'#type' => 'submit',
'#value' => t('Add filter'),
'#weight' => 0,
'#submit' => array('ding_nodelist_add_filter_submit'),
'#limit_validation_errors' => array(array('taxonomy_filters')),
'#ajax' => array(
'wrapper' => 'ding_nodelist-taxonomy-filters',
'callback' => 'ding_nodelist_js_add_filter',
'method' => 'replace',
'effect' => 'fade',
),
);
// Date field filtering.
// @todo: remove hard code and make more unversal.
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['content_filter_date'] = array(
'#type' => $node_type == 'ding_event' ? 'checkbox' : 'hidden',
'#title' => t('Show upcoming events only'),
'#description' => t('If checked only future events will be shown'),
'#default_value' => $node_type == 'ding_event' && isset($conf['content_filter_date']) ? $conf['content_filter_date'] : FALSE,
'#prefix' => '<div id="ding_nodelist-content-filter-date">',
'#suffix' => '</div>',
);
// Content filter.
$form['ding_nodelist_wrapper']['nodelist_content']['nodelist_content_wrapper']['ding_nodelist_dynamic']['content_filter_promoted'] = array(
'#type' => 'checkbox',
'#title' => t('Show "Promoted to front page" content only'),
'#default_value' => isset($conf['content_filter_promoted']) ? $conf['content_filter_promoted'] : FALSE,
);
// Various options.
$form['ding_nodelist_wrapper']['nodelist_various_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Various'),
);
// Initialize drop-down array's values.
$options = array(0 => 'none')
+ array_combine(range(1, 25), range(1, 25))
+ array_combine(range(30, 50, 5), range(30, 50, 5))
+ array_combine(range(50, 100, 10), range(50, 100, 10));
$form['ding_nodelist_wrapper']['nodelist_various_settings']['limit'] = array(
'#type' => 'select',
'#title' => t('Item limit'),
'#default_value' => isset($conf['limit']) ? $conf['limit'] : $default_conf['limit'],
'#options' => $options,
'#description' => t('Limit amount of items displayed. This value is used only for Dynamic and All content displays.'),
'#states' => array(
'invisible' => array(
':input[name=nodes_switch]' => array('checked' => TRUE),
':input[name=dynamic_switch]' => array('checked' => FALSE),
),
'visible' => array(
':input[name=list_type]' => array(
array('value' => 'custom-dynamic'),
array('value' => 'all'),
),
),
),
);
$form['ding_nodelist_wrapper']['nodelist_various_settings']['ding_nodelist_sorting'] = array(
'#type' => 'container',
);
$sort_options = array(
'created' => t('Creation date'),
'title' => t('Title'),
'sticky' => t('Sticky first'),
);
if ($node_type == 'ding_event') {
$sort_options['event_date'] = t('Event date');
}
$form['ding_nodelist_wrapper']['nodelist_various_settings']['ding_nodelist_sorting']['sort_field'] = array(
'#type' => 'select',
'#title' => t('Sort by'),
'#options' => $sort_options,
'#empty_option' => t('No sorting'),
'#default_value' => isset($conf['sort_field']) ? $conf['sort_field'] : '',
'#prefix' => '<div id="ding_nodelist-sort-field">',
'#suffix' => '</div>',
);
$sort_order = isset($conf['sort_order']) ? $conf['sort_order'] : '';
$form['ding_nodelist_wrapper']['nodelist_various_settings']['ding_nodelist_sorting']['sort_order'] = array(
'#type' => 'select',
'#title' => t('Sorting order'),
'#options' => array(
'asc' => t('Ascending'),
'desc' => t('Descending'),
),
'#default_value' => $sort_order,
'#states' => array(
// Hide the settings when the sorting order is not applicable.
'invisible' => array(
':input[name="sort_field"]' => array(array('value' => ''), array('value' => 'sticky')),
),
),
);
$form['ding_nodelist_wrapper']['nodelist_various_settings']['columns'] = array(
'#type' => 'select',
'#title' => t('Width (columns)'),
'#options' => array(
NL_COL_FULL => t('Full width'),
NL_COL_HALF => '1/2',
NL_COL_THIRD => '1/3',
NL_COL_QRT => '1/4',
),
'#default_value' => isset($conf['columns']) ? $conf['columns'] : $default_conf['columns'],
);
// Forms for Bottom settings.
$form['ding_nodelist_wrapper']['ding_nodelist_bottom_links'] = array(
'#type' => 'fieldset',
'#title' => t('Bottom links'),
'#collapsible' => FALSE,
'#description' => t('List of "read more" links.'),
);
// Container for bottom links
$form['ding_nodelist_wrapper']['ding_nodelist_bottom_links']['more_links'] = array(
'#prefix' => '<div id="ding_nodelist-more-link">',
'#suffix' => '</div>',
'#theme' => '_ding_nodelist_links_table',
'#tree' => TRUE,
);
// Get all bottom links.
$delta = 0;
if (isset($form_state['values']['bottom_links_count'])) {
$links_count = $form_state['values']['bottom_links_count'];
}
else {
$links_count = empty($form_state['values']['more_links']) ? 1 : count($form_state['values']['more_links']);
}
if (isset($conf['more_links'])) {
$delta = count($conf['more_links']);
foreach ($conf['more_links'] as $key => $link) {
$form['ding_nodelist_wrapper']['ding_nodelist_bottom_links']['more_links'][$key] = _ding_nodelist_links_form($key, $link);
}
}
// Add new bottom link field.
for ($delta; $delta < $links_count; $delta++) {
$key = $delta;
$form['ding_nodelist_wrapper']['ding_nodelist_bottom_links']['more_links'][$key] = _ding_nodelist_links_form($key, NULL);
}
$form['ding_nodelist_wrapper']['ding_nodelist_bottom_links']['ding_bottom_links_more'] = array(
'#type' => 'submit',
'#value' => t('Add more link'),
'#submit' => array('ding_nodelist_add_bottom_link_submit'),
'#ajax' => array(
'wrapper' => 'ding_nodelist-more-link',
'callback' => 'ding_nodelist_js_add_links',
'effect' => 'fade',
'method' => 'replace',
),
);
// Cache settings.
$form['ding_nodelist_wrapper']['cache_settings'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
'#title' => t('Cache settings'),
);
// Load cache settigns and prepare form.
if (user_access('nodelist cache settings')) {
ctools_include('plugins');
$simple_cache_plugin = ctools_plugin_get_info('panels', 'cache');
ctools_plugin_load_includes($simple_cache_plugin);
$pane = $form_state['pane'];
$settings = !empty($pane->cache['settings'])
? $pane->cache['settings']
: array(
'lifetime' => variable_get('ding_nodelist_cache_default_time', NL_CACHE_LIFETIME),
'granularity' => 'context',
);
$cache_form = panels_simple_cache_settings_form($settings, $form_state['display'], $pane->pid);
// @todo: Granularity is forced to 'context' now.
unset($cache_form['granularity']);
$form['ding_nodelist_wrapper']['cache_settings'] = array_merge($form['ding_nodelist_wrapper']['cache_settings'], $cache_form);
}
// Restore other form elements if this is not the first form display.
if (isset($form_state['restore'])) {
$form = array_merge($form_state['original_form'], $form);
}
// Remove some standard controls.
unset($form['override_title']);
unset($form['override_title_text']);
unset($form['override_title_markup']);
return $form;
}
/**
* Generate a form piece for dynamic node select form.
*
* @param string $key
* Unique key for this piece.
* @param string $type
* Node type.
* @param array $node
* Array with the node oject.
* @param int $weight
* Piece weight.
* @return array
* Form structure.
*/
function _ding_nodelist_selected_node($key, $type, $node = NULL, $weight = 0) {
$form = array(
'#tree' => TRUE,
'#weight' => $weight,
);
// Set default value for 'node'
$node_default = (!is_null($node) && isset($node['node'])) ? $node['node'] : '';
$form['node'] = array(
'#type' => 'textfield',
'#title' => t('Select new node to add to list'),
'#title_display' => 'invisible',
'#autocomplete_path' => 'ding_nodelist/autocomplete/' . $type,
'#parents' => array('selected_nodes', $key, 'node'),
'#default_value' => $node_default,
);
$form['nid'] = array(
'#type' => 'value',
'#value' => NULL,
'#parents' => array('selected_nodes', $key, 'nid'),
);
// Set default value for 'nid' if not empty field
if (!is_null($node) && isset($node['nid']) && ctype_digit($node['nid'])) {
$form['nid']['#value'] = $node['nid'];
}
elseif (!is_null($node) && isset($node['node'])) {
$tmp_node = explode(':', $node['node']); // EXPERIMENTAL FIX! previous value '$node' gave warnings.
$tmp_node = array_pop($tmp_node);
$tmp_node = substr($tmp_node, 0, -1);
if (ctype_digit($tmp_node)) {
$node['nid'] = $tmp_node;
}
}
$form['type'] = array(
'#type' => 'value',
'#value' => $type,
'#parents' => array('selected_nodes', $key, 'type'),
);
$form['weight'] = array(
'#type' => 'weight',
'#title' => $node['nid'] !== '' ? t('Weight for node @label', array('@label' => $node['nid'])) : t('Weight for new choice'),
'#title_display' => 'invisible',
'#default_value' => $weight,
'#delta' => 50,
'#parents' => array('selected_nodes', $key, 'weight'),
);
$form['node_delete_' . $key] = array(
'#name' => 'node_delete_' . $key,
'#type' => 'submit',
'#title_display' => 'invisible',
'#value' => t('Delete'),
'#parents' => array('selected_nodes', $key, 'node_delete_' . $key),
'#submit' => array('ding_nodelist_node_delete_submit'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'wrapper' => 'ding_nodelist-selected-nodes',
'callback' => 'ding_nodelist_js_add_node',
'method' => 'replace',
'effect' => 'fade',
),
);
return $form;
}
/**
* Builds more link form.
*/
function _ding_nodelist_links_form($key, $link) {
$form = array(
'#tree' => TRUE,
'#weight' => $key,
);
$form['text'] = array(
'#type' => 'textfield',
'#title' => t('Link title'),
'#title_display' => 'invisible',
'#size' => 20,
'#tree' => TRUE,
'#weight' => -1,
'#parents' => array('more_links', $key, 'text'),
'#default_value' => (!empty($link['text'])) ? $link['text'] : '',
);
$form['links'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#title_display' => 'invisible',
'#required' => FALSE,
'#description' => check_plain(t('Local path or external URL starting with http(s). Enter "<front>" to link to home page.')),
'#parents' => array('more_links', $key, 'links'),
'#default_value' => (!empty($link['links'])) ? $link['links'] : '',
);
return $form;
}
/**
* Builds filter form.
*/
function _ding_nodelist_taxonomy_filter($fields, $filter, $key) {
$form = array(
'#tree' => TRUE,
'#weight' => $key,
);
$form['filter_field'] = array(
'#type' => 'select',
'#title_display' => 'invisible',
'#options' => $fields,
'#default_value' => !is_null($filter) && !empty($filter['filter_field']) ? $filter['filter_field'] : '',
'#ajax' => array(
'wrapper' => 'ding_nodelist-taxonomy-filters',
'callback' => 'ding_nodelist_js_add_filter',
'method' => 'replace',
'effect' => 'fade',
),
'#parents' => array('taxonomy_filters', $key, 'filter_field'),
);
$form['filter_terms'] = array(
'#vocabulary_name' => $filter['filter_vocab'],
'#type' => 'textfield',
'#title_display' => 'invisible',
'#default_value' => !is_null($filter) && !empty($filter['filter_terms']) ? $filter['filter_terms'] : '',
'#parents' => array('taxonomy_filters', $key, 'filter_terms'),
'#maxlength' => 1024,
'#element_validate' => array('_ding_nodelist_taxonomy_autocomplete_validate'),
'#autocomplete_path' => !is_null($filter) && !empty($filter['filter_field']) ? 'taxonomy/autocomplete/' . $filter['filter_field'] : NULL,
);
$form['filter_delete_' . $key] = array(
'#name' => 'filter_delete_' . $key,
'#type' => 'submit',
'#title_display' => 'invisible',
'#value' => t('Delete'),
'#parents' => array('taxonomy_filters', $key, 'filter_delete_' . $key),
'#submit' => array('ding_nodelist_filter_delete_submit'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'wrapper' => 'ding_nodelist-taxonomy-filters',
'callback' => 'ding_nodelist_js_add_filter',
'method' => 'replace',
'effect' => 'fade',
),
);
return $form;
}
/**
* Custom submit function to add more choices.
*/
function ding_nodelist_add_node_submit($form, &$form_state) {
// Add new node to list.
$form_state['values']['selected_nodes_count'] = count($form_state['values']['selected_nodes']) + 1;
unset($form_state['input']['selected_nodes']);
foreach ($form_state['values']['selected_nodes'] as $key => $node) {
if (!empty($node['node'])) {
$tmp_nid = explode(':', $node['node']);
$tmp_nid = array_pop($tmp_nid);
$tmp_nid = substr($tmp_nid, 0, -1);
// TODO: Implement more complex check for given title + ID
if (ctype_digit($tmp_nid)) {
$form_state['values']['selected_nodes'][$key]['nid'] = $tmp_nid;
}
}
}
// Copy 'input' since limit_validation_errors empties 'values'.
$form_state['values'] += $form_state['input'];
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
/**
* Custom submit function for adding more filters.
*/
function ding_nodelist_add_filter_submit($form, &$form_state) {
// Add new filter.
$form_state['values']['taxonomy_filters_count'] = count($form_state['values']['taxonomy_filters']) + 1;
// Copy 'input' since limit_validation_errors empties 'values'.
$form_state['values'] += $form_state['input'];
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
/**
* Custom submit function for filter deletion.
*/
function ding_nodelist_filter_delete_submit($form, &$form_state) {
$deleted_filter = $form_state['triggering_element']['#parents'][1];
unset($form_state['values']['taxonomy_filters'][$deleted_filter]);
unset($form_state['input']['taxonomy_filters'][$deleted_filter]);
// Re-index after deletion and save.
$form_state['input']['taxonomy_filters'] = array_values($form_state['input']['taxonomy_filters']);
$form_state['values'] = $form_state['input'];
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
/**
* Custom submit function for filter deletion.
*/
function ding_nodelist_node_delete_submit($form, &$form_state) {
$deleted_node = $form_state['triggering_element']['#parents'][1];
unset($form_state['values']['selected_nodes'][$deleted_node]);
unset($form_state['input']['selected_nodes'][$deleted_node]);
// Re-index after deletion and save.
$form_state['input']['selected_nodes'] = array_values($form_state['input']['selected_nodes']);
$form_state['values'] = $form_state['input'];
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
/**
* Custom submit function for adding more links.
*/
function ding_nodelist_add_bottom_link_submit(&$form, &$form_state) {
// Add new link to list.
$form_state['values']['bottom_links_count'] = count($form_state['values']['more_links']) + 1;
unset($form_state['input']['more_links']);
// Saving all the values of the form.
_ding_nodelist_content_type_form_save($form_state, TRUE);
}
/**
* Edit form validation function.
*/
function ding_nodelist_content_type_edit_form_validate($form, &$form_state) {
$form_state['restore'] = TRUE;
// Do not validate if filter was changed or deleted.
$te = empty($form_state['triggering_element']) ? NULL : $form_state['triggering_element'];
if (strpos($te['#name'], 'widget_type') === 0 || $te['#name'] == 'filter_field') {
return;
}
$list_type = $form_state['values']['list_type'];
if ($list_type === 'custom-dynamic') {
$node_switch = isset($form_state['values']['nodes_switch']) ? $form_state['values']['nodes_switch'] : 0;
$dynamic_switch = isset($form_state['values']['dynamic_switch']) ? $form_state['values']['dynamic_switch'] : 0;
if (!$node_switch && !$dynamic_switch) {
form_set_error('', t('Select at least one content pick rule (custom and/or dynamic).'));
}
else {
// Check for empty nodes.
//$no_custom_content = FALSE;
if (!empty($form_state['values']['selected_nodes']) && $form_state['values']['nodes_switch']) {
$node_cnt = count($form_state['values']['selected_nodes']);
foreach ($form_state['values']['selected_nodes'] as $key => $node) {
if ($te['#name'] == 'add_node' || $te['#value'] === 'Finish' || $node_cnt > 1) {
if (empty($node['node'])) {
form_set_error("selected_nodes][$key", t('No custom list content specified.'));
}
}
}
}