This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch_node_page.module
More file actions
1131 lines (1002 loc) · 41.7 KB
/
search_node_page.module
File metadata and controls
1131 lines (1002 loc) · 41.7 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
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
* This is implements the blocks used to display the search.
*/
/**
* Implements hook_menu().
*/
function search_node_page_menu() {
$items = array();
$items ['search_node/api/%/auth'] = array(
'title' => 'Search node authentication',
'description' => 'Authentication call to get token based on API key',
'page callback' => 'search_node_page_authenticate',
'page arguments' => array(2),
'access arguments' => array('search content'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function search_node_page_permission() {
return array(
'search content' => array(
'title' => t('Search node - search content'),
),
);
}
/**
* Implements hook_theme().
*/
function search_node_page_theme() {
return array(
'search_node_page_search_box' => array(
'variables' => array(),
'template' => 'search-node-page-search-box',
'path' => drupal_get_path('module', 'search_node_page') . '/templates',
),
'search_node_page_search_results' => array(
'variables' => array(),
'template' => 'search-node-page-search-results',
'path' => drupal_get_path('module', 'search_node_page') . '/templates',
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function search_node_page_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' || $module == 'panels') {
return 'plugins/' . $plugin;
}
}
/**
* Authentication callback.
*
* @param $search_server
* The machine name of the search api server to use.
*/
function search_node_page_authenticate($search_server) {
// Ensure auth in not cached.
drupal_page_is_cacheable(FALSE);
$server = search_api_server_load($search_server);
if ($server) {
// Check for overridden api-key and host from settings.php
$apikey = variable_get('search_api_' . $server->machine_name . '_apikey_readonly', $server->options['apikey_readonly']);
$host = variable_get('search_api_' . $server->machine_name . '_host', $server->options['host']);
$search_node = new SearchNodeClient($host, $apikey);
$auth = $search_node->authenticate();
if ($auth['status'] == 200) {
drupal_json_output(array(
'status' => $auth['status'],
'token' => $search_node->getToken(),
));
}
else {
// Auth failed.
drupal_json_output($auth);
}
}
else {
drupal_json_output(array(
'status' => 404,
'token' => t('Search node server not found.'),
));
}
}
/**
* Implements hook_libraries_info().
*
* For defining external libraries.
*/
function search_node_page_libraries_info() {
// A very simple library. No changing APIs (hence, no versions), no variants.
// Expected to be extracted into 'sites/all/libraries/simple'.
$libraries['angular'] = array(
'name' => 'Angular JS',
'vendor url' => 'https://angularjs.org/',
'download url' => 'https://angularjs.org/',
'version arguments' => array(
'file' => 'angular.min.js',
'pattern' => '/AngularJS v(\d+\.\d+.\d+)/',
'lines' => 5,
),
'files' => array(
'js' => array('angular.min.js'),
),
);
$libraries['search_node'] = array(
'name' => 'Search node Angular library',
'vendor url' => 'http://example.com/simple',
'download url' => 'http://example.com/simple',
'version arguments' => array(
'file' => 'search.min.js',
'pattern' => '/@version v(\d+\.\d+.\d+(-\w*\d*)*)/',
'lines' => 5,
),
'library path' => drupal_get_path('module', 'search_node_page') . '/js',
'files' => array(
'js' => array(
'assets.min.js',
'search.min.js'
),
),
);
return $libraries;
}
/**
* Implements hook_block_info().
*/
function search_node_page_block_info() {
return array(
'search_node_search_box' => array(
'info' => t('Search node - Search field'),
'cache' => DRUPAL_NO_CACHE,
),
'search_node_search_result' => array(
'info' => t('Search node - Results'),
'cache' => DRUPAL_NO_CACHE,
),
);
}
/**
* Implements hook_block_configure().
*/
function search_node_page_block_configure($delta) {
$form = array();
switch($delta) {
case 'search_node_search_box':
// Load saved configuration.
$defaults = variable_get('search_node_page_search_box', array());
$form_state = array();
$form = search_node_page_configuration_form($form, $form_state, $defaults);
break;
case 'search_node_search_result' :
/**
* @TODO: What options should be here?
*/
break;
}
return $form;
}
/**
* Implements hook_form_HOOK_alter().
*
* Change the block configuration form to support ajax updates.
*/
function search_node_page_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
if ($form['delta']['#value'] == 'search_node_search_box') {
// Try to load selected server.
$selected_server = !empty($form_state['values']['search_node']['server']) ? $form_state['values']['search_node']['server'] : $form['settings']['search_node']['server']['#default_value'];
$server = search_api_server_load($selected_server);
// Build indexes available base on the loaded server.
if ($server) {
$server_indexes = search_api_index_load_multiple(FALSE, array('server' => $server->machine_name));
}
if (!empty($server_indexes)) {
// Build index selection.
$indexes = array('_none_' => t('Select index'));
foreach ($server_indexes as $index) {
$indexes[$index->machine_name] = $index->name . ' (' . $index->options['search_node_indexes'] . ')';
}
$form['settings']['search_node']['index']['#options'] = $indexes;
$form['settings']['search_node']['autocomplete']['index']['#options'] = $indexes;
// Build field and filter selection.
$fields = array();
$dates = array();
$filters = array(
'taxonomy' => array(),
'boolean' => array(),
);
$selected_index = !empty($form_state['values']['search_node']['index']) ? $form_state['values']['search_node']['index'] : $form['settings']['search_node']['index']['#default_value'];
$index = search_api_index_load($selected_index);
foreach ($index->options['fields'] as $field => $options) {
$type = isset($options['entity_type']) ? $options['entity_type'] : $options['type'];
switch ($type) {
case 'taxonomy_term':
// Load field.
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$filters['taxonomy'][$field] = $vocab->name . ' (' . $field . ')';
// Also add it to searchable fields.
$fields[$field] = $vocab->name . ' (' . $field . ')';
// Add filter types ("or"/"and" for filters available).
$form['settings']['search_node']['filters']['type'][$field] = array(
'#type' => 'select',
'#title' => $fields[$field],
'#options' => array('and' => t('and'), 'or' => t('or')),
'#default_value' => isset($form_state['pane']->configuration['search_node']['filters']['type'][$field]) ? $form_state['pane']->configuration['search_node']['filters']['type'][$field] : 'and',
);
break;
case 'boolean':
$info = field_info_field($field);
$bundles = array_keys($info['bundles']);
$entity_type = reset($bundles);
$bundle_name = reset($info['bundles'])[0];
$info = field_info_instance($entity_type, $field, $bundle_name);
$filters['boolean'][$field] = t($info['label']) . ' (' . $field . ')';;
break;
case 'date':
// Check if date has an value2 in the index to indicate a to date.
if (isset($index->options['fields'][$field . '2'])) {
$field_name = explode(':', $field)[0];
$info = field_info_field($field_name);
$bundles = array_keys($info['bundles']);
$entity_type = reset($bundles);
$bundle_name = reset($info['bundles'])[0];
$info = field_info_instance($entity_type, $field_name, $bundle_name);
$dates[$field] = t($info['label']) . ' (' . $field_name . ')';
break;
}
// Fall through to default.
default:
// The index save some fields with ":".
$name = explode(':', $field);
$fields[$field] = array_shift($name) . ' (' . $field . ')';
break;
}
}
$form['settings']['search_node']['options']['fields']['#options'] = $fields;
$form['settings']['search_node']['options']['filters']['taxonomy']['#options'] = $filters['taxonomy'];
$form['settings']['search_node']['options']['filters']['boolean']['#options'] = $filters['boolean'];
// Update sort fields.
$form['settings']['search_node']['sorting']['field']['#options'] = array('_none_' => t('None')) + $fields;
// Updater interval fields.
$form['settings']['search_node']['intervals']['fields']['#options'] = $fields;
// Update force filters with the options.
$form['settings']['search_node']['forces']['field']['#options'] = $fields;
// Update force filters with the options.
$form['settings']['search_node']['forces']['field']['#options'] = array('_none_' => t('Select field')) + $filters;
// Highlight fields.
$form['settings']['search_node']['highlight']['fields']['#options'] = $fields;
// Load taxonomy based on selected field.
$field = !empty($form_state['values']['search_node']['forces']['field']) ? $form_state['values']['search_node']['forces']['field'] : $form['settings']['search_node']['forces']['field']['#default_value'];
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$terms = array();
foreach (taxonomy_get_tree($vocab->vid) as $term) {
$terms[$term->name] = $term->name;
}
$form['settings']['search_node']['forces']['selected']['#options'] = $terms;
$fields = array();
$selected_index = !empty($form_state['values']['search_node']['autocomplete']['index']) ? $form_state['values']['search_node']['autocomplete']['index'] : $form['settings']['search_node']['autocomplete']['index']['#default_value'];
$index = search_api_index_load($selected_index);
foreach ($index->options['fields'] as $field => $options) {
if (isset($options['entity_type']) && $options['entity_type'] == 'taxonomy_term') {
// Load field.
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$filters[$field] = $vocab->name . ' (' . $field .')';
}
else {
// The index save some fields with ":".
$name = explode(':', $field);
$fields[$field] = array_shift($name) . ' (' . $field .')';
}
}
$form['settings']['search_node']['autocomplete']['field']['#options'] = array('_none_' => t('None')) + $fields;;
}
}
}
/**
* Implements hook_form_HOOK_alter().
*
* @TODO: Merge the two alter functions, they differ in the way they access
* the form elements.
*
* Change the panes configuration form to support ajax updates.
*/
function search_node_page_form_search_node_page_search_box_content_type_edit_form_alter(&$form, &$form_state, $form_id) {
if (!isset($form['search_node'])) {
// When forms are ajaxed in panels, the form is missing.
$form = search_node_page_configuration_form($form, $form_state, $form_state['input']['search_node']);
}
// Try to load selected server.
$selected_server = !empty($form_state['input']['search_node']['server']) ? $form_state['input']['search_node']['server'] : $form['search_node']['server']['#default_value'];
$server = search_api_server_load($selected_server);
// Build indexes available base on the loaded server.
if ($server) {
$server_indexes = search_api_index_load_multiple(FALSE, array('server' => $server->machine_name));
}
if (!empty($server_indexes)) {
// Build index selection.
$indexes = array('_none_' => t('Select index'));
foreach ($server_indexes as $index) {
$indexes[$index->machine_name] = $index->name . ' (' . $index->options['search_node_indexes'] . ')';
}
$form['search_node']['index']['#options'] = $indexes;
$form['search_node']['autocomplete']['index']['#options'] = $indexes;
// Build field and filter selection.
$fields = array();
$dates = array();
$filters = array(
'taxonomy' => array(),
'boolean' => array(),
);
$selected_index = !empty($form_state['input']['search_node']['index']) ? $form_state['input']['search_node']['index'] : $form['search_node']['index']['#default_value'];
$index = search_api_index_load($selected_index);
foreach ($index->options['fields'] as $field => $options) {
$type = isset($options['entity_type']) ? $options['entity_type'] : $options['type'];
switch ($type) {
case 'taxonomy_term':
// Load field.
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$filters['taxonomy'][$field] = $vocab->name . ' (' . $field . ')';
// Also add it to searchable fields.
$fields[$field] = $vocab->name . ' (' . $field . ')';
// Add filter types ("or"/"and" for filters available).
$form['search_node']['filters']['type'][$field] = array(
'#type' => 'select',
'#title' => $fields[$field],
'#options' => array('and' => t('and'), 'or' => t('or')),
'#default_value' => isset($form_state['pane']->configuration['search_node']['filters']['type'][$field]) ? $form_state['pane']->configuration['search_node']['filters']['type'][$field] : 'and',
);
break;
case 'boolean':
$info = field_info_field($field);
$bundles = array_keys($info['bundles']);
$entity_type = reset($bundles);
$bundle_name = reset($info['bundles'])[0];
$info = field_info_instance($entity_type, $field, $bundle_name);
$filters['boolean'][$field] = t($info['label']) . ' (' . $field . ')';;
break;
case 'date':
// Check if date has an value2 in the index to indicate a to date.
if (isset($index->options['fields'][$field . '2'])) {
$field_name = explode(':', $field)[0];
$info = field_info_field($field_name);
$bundles = array_keys($info['bundles']);
$entity_type = reset($bundles);
$bundle_name = reset($info['bundles'])[0];
$info = field_info_instance($entity_type, $field_name, $bundle_name);
$dates[$field] = t($info['label']) . ' (' . $field_name . ')';
break;
}
// Fall through to default.
default:
// The index save some fields with ":".
$name = explode(':', $field);
$fields[$field] = array_shift($name) . ' (' . $field . ')';
break;
}
}
$form['search_node']['options']['fields']['#options'] = $fields;
$form['search_node']['options']['filters']['taxonomy']['#options'] = $filters['taxonomy'];
$form['search_node']['options']['filters']['boolean']['#options'] = $filters['boolean'];
// Update sort fields.
$form['search_node']['sorting']['field']['#options'] = array('_none_' => t('None')) + $fields;
// Updater interval fields.
$form['search_node']['intervals']['fields']['#options'] = $fields;
// Update dates intervals.
$form['search_node']['dates']['fields']['#options'] = $dates;
// Update force filters with the options.
$form['search_node']['forces']['field']['#options'] = array('_none_' => t('Select field')) + $filters['taxonomy'];
// Highlight fields.
$form['search_node']['highlight']['fields']['#options'] = $fields;
// Load taxonomy based on selected field.
$field = !empty($form_state['input']['search_node']['forces']['field']) ? $form_state['input']['search_node']['forces']['field'] : $form['search_node']['forces']['field']['#default_value'];
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$terms = array();
foreach (taxonomy_get_tree($vocab->vid) as $term) {
$terms[$term->name] = $term->name;
}
$form['search_node']['forces']['selected']['#options'] = $terms;
if (!empty($server_indexes)) {
$fields = array();
$selected_index = !empty($form_state['values']['search_node']['autocomplete']['index']) ? $form_state['values']['search_node']['autocomplete']['index'] : $form['settings']['search_node']['autocomplete']['index']['#default_value'];
$index = search_api_index_load($selected_index);
foreach ($index->options['fields'] as $field => $options) {
if (isset($options['entity_type']) && $options['entity_type'] == 'taxonomy_term') {
// Load field.
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$filters[$field] = $vocab->name . ' (' . $field . ')';
}
else {
// The index save some fields with ":".
$name = explode(':', $field);
$fields[$field] = array_shift($name) . ' (' . $field . ')';
}
}
}
}
}
/**
* The block and pane configuration form.
*
* @param $form
* @param array $form_state
* @param array $defaults
* @return mixed
*/
function search_node_page_configuration_form($form, array &$form_state = array(), $defaults = array()) {
$form['search_node'] = array(
'#type' => 'fieldset',
'#title' => 'Search node configuration',
'#tree' => TRUE,
);
$form['search_node']['id'] = array(
'#type' => 'textfield',
'#title' => t('Search id'),
'#description' => t('This is used to uniquely identify this search field in frontend cache.'),
'#default_value' => isset($defaults['id']) ? $defaults['id'] : REQUEST_TIME,
'#required' => TRUE,
);
// Get list of all search node search API servers.
$search_nodes = array();
$servers = search_api_server_load_multiple(FALSE);
foreach ($servers as $server) {
if ($server->class == 'search_api_search_node_service') {
$search_nodes[$server->machine_name] = $server->name;
}
}
// Allow selection of search server.
$form['search_node']['server'] = array(
'#type' => 'select',
'#title' => t('Search node server'),
'#description' => t('Select the search api search node server to use'),
'#options' => array('_none_' => t('Select server')) + $search_nodes,
'#default_value' => isset($defaults['server']) ? $defaults['server'] : '_none_',
'#required' => TRUE,
'#ajax' => array(
'callback' => 'search_node_page_block_index_ajax_callback',
'wrapper' => 'search-node-page-block-indexes',
),
);
$indexes = array('_none_' => t('Select index'));
$form['search_node']['index'] = array(
'#type' => 'select',
'#title' => t('Search index'),
'#description' => t('Select the search api search node server to use'),
'#options' => $indexes,
'#default_value' => isset($defaults['index']) ? $defaults['index'] : '_none_',
'#required' => TRUE,
'#prefix' => '<div id="search-node-page-block-indexes">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => 'search_node_page_block_options_ajax_callback',
'wrapper' => 'search-node-page-block-options',
),
);
$form['search_node']['options'] = array(
'#type' => 'fieldset',
'#title' => 'Search options',
'#tree' => TRUE,
'#prefix' => '<div id="search-node-page-block-options">',
'#suffix' => '</div>',
);
$form['search_node']['options']['match_type'] = array(
'#type' => 'select',
'#title' => t('Search Match'),
'#description' => t('For more information see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-phrase'),
'#options' => array(
'best_fields' => t('Best fields (default)'),
'most_fields' => t('Most fields'),
'cross_fields ' => t('Cross fields'),
'phrase' => t('Phrase match'),
'phrase_prefix ' => t('Phrase prefix match'),
),
'#default_value' => isset($defaults['options']['match_type']) ? $defaults['options']['match_type'] : 'best_fields',
'#required' => TRUE,
);
$form['search_node']['options']['match_operator'] = array(
'#type' => 'select',
'#title' => t('Search Match operator'),
'#description' => t('The operator to use between words'),
'#options' => array(
'or' => 'or (default)',
'and' => 'and',
),
'#default_value' => isset($defaults['options']['match_operator']) ? $defaults['options']['match_operator'] : 'or',
'#required' => TRUE,
);
$form['search_node']['options']['cache_expire'] = array(
'#type' => 'textfield',
'#title' => t('Cache expire'),
'#description' => t('Filter and search cache expire time in seconds.'),
'#default_value' => isset($defaults['options']['cache_expire']) ? $defaults['options']['cache_expire'] : '30',
'#required' => TRUE,
);
$form['search_node']['options']['size'] = array(
'#type' => 'textfield',
'#title' => t('Result size'),
'#description' => t('The number of results to show on the search result page - more result will be displayed using a pager.'),
'#default_value' => isset($defaults['options']['size']) ? $defaults['options']['size'] : '8',
'#required' => TRUE,
);
$form['search_node']['options']['initial_query_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Enable initial query text'),
'#description' => t('Enable a default/initial search query to perform when the search is loaded.'),
'#default_value' => isset($defaults['options']['initial_query_enable']) ? $defaults['options']['initial_query_enable'] : array(),
'#required' => FALSE,
);
$form['search_node']['options']['initial_query_text'] = array(
'#type' => 'textfield',
'#title' => t('Initial query text'),
'#description' => t('Search string to perform when the search is loaded.'),
'#default_value' => isset($defaults['options']['initial_query_text']) ? $defaults['options']['initial_query_text'] : '',
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[id="edit-search-node-options-initial-query-enable"]' => array('checked' => TRUE),
),
),
);
$search_fields = array();
$form['search_node']['options']['fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Search fields'),
'#description' => t('Select the fields to search in the index'),
'#options' => $search_fields,
'#default_value' => isset($defaults['options']['fields']) ? $defaults['options']['fields'] : array(),
'#required' => TRUE,
);
$filters = array();
$form['search_node']['options']['filters']['taxonomy'] = array(
'#type' => 'checkboxes',
'#title' => t('Search filters'),
'#description' => t('Select the filters to use'),
'#options' => $filters,
'#default_value' => isset($defaults['options']['filters']['taxonomy']) ? $defaults['options']['filters']['taxonomy'] : array(),
);
$filters = array();
$form['search_node']['options']['filters']['boolean'] = array(
'#type' => 'checkboxes',
'#title' => t('Search filters (boolean)'),
'#description' => t('Select the filters to use as true/false filters'),
'#options' => $filters,
'#default_value' => isset($defaults['options']['filters']['boolean']) ? $defaults['options']['filters']['boolean'] : array(),
);
// This field set is empty to start with and field with options based on
// available filters in the alter function.
$form['search_node']['filters']['type'] = array(
'#type' => 'fieldset',
'#title' => t('Filters type'),
'#description' => t('Defines which type to use internal in a filter. Defaults to "and"'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['search_node']['sorting'] = array(
'#type' => 'fieldset',
'#title' => t('Sorting'),
'#description' => t('Sorting the search result.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$selected = array();
$form['search_node']['sorting']['field'] = array(
'#type' => 'radios',
'#title' => t('Date fields'),
'#options' => $selected,
'#default_value' => isset($defaults['sorting']['field']) ? $defaults['sorting']['field'] : array(),
);
$form['search_node']['sorting']['order'] = array(
'#type' => 'radios',
'#title' => t('Sort direction'),
'#options' => array(
'asc' => t('Ascending'),
'desc' => t('Descending'),
),
'#default_value' => isset($defaults['sorting']['order']) ? $defaults['sorting']['order'] : array(),
);
$form['search_node']['dates'] = array(
'#type' => 'fieldset',
'#title' => t('Search dates'),
'#description' => t('Search items with both start and end dates.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$search_fields = array();
$form['search_node']['dates']['fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Date fields'),
'#options' => $search_fields,
'#default_value' => isset($defaults['dates']['fields']) ? $defaults['dates']['fields'] : array(),
);
$form['search_node']['intervals'] = array(
'#type' => 'fieldset',
'#title' => t('Search intervals'),
'#description' => t('Special search fields (boxes) that allows range searches (intervals).'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$search_fields = array();
$form['search_node']['intervals']['fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Interval fields'),
'#description' => t('Select the interval fields to search.'),
'#options' => $search_fields,
'#default_value' => isset($defaults['intervals']['fields']) ? $defaults['intervals']['fields'] : array(),
);
$form['search_node']['forces'] = array(
'#type' => 'fieldset',
'#title' => t('Forced filters'),
'#description' => t('Forced filters are filters that is allways applied and can not be removed be the user. You should not select an allready used filter, as that would create some strange UI behaviour.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
/**
* @TODO: Added support for more than one field.
*/
$form['search_node']['forces']['field'] = array(
'#type' => 'select',
'#title' => t('Field to force'),
'#description' => t('Select the filter field to force'),
'#options' => array('_none_' => t('Select field')),
'#default_value' => isset($defaults['forces']['field']) ? $defaults['forces']['field'] : '_none_',
'#ajax' => array(
'callback' => 'search_node_page_block_force_field_ajax_callback',
'wrapper' => 'search-node-page-block-force-field-selected',
),
);
$selected = array();
$form['search_node']['forces']['selected'] = array(
'#type' => 'checkboxes',
'#title' => t('Search filters'),
'#description' => t('Select the filters to use'),
'#options' => $selected,
'#default_value' => isset($defaults['forces']['selected']) ? $defaults['forces']['selected'] : array(),
'#prefix' => '<div id="search-node-page-block-force-field-selected">',
'#suffix' => '</div>',
);
$form['search_node']['highlight'] = array(
'#type' => 'fieldset',
'#title' => t('Highlighting'),
'#description' => t('Highlight search results.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$search_fields = array();
$form['search_node']['highlight']['fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Search fields'),
'#description' => t('Select the fields to highlight'),
'#options' => $search_fields,
'#default_value' => isset($defaults['highlight']['fields']) ? $defaults['highlight']['fields'] : array()
);
$form['search_node']['templates'] = array(
'#type' => 'fieldset',
'#title' => t('Search templates'),
'#description' => t('The AngularJS templates use by the frontend. You should customize these to fit your search results.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$path = drupal_get_path('module', 'search_node_page');
$form['search_node']['templates']['box'] = array(
'#type' => 'textfield',
'#title' => 'Search box and filters',
'#default_value' => isset($defaults['templates']['box']) ? $defaults['templates']['box'] : '/' . $path . '/templates/angular/search.html',
'#required' => TRUE,
);
$form['search_node']['templates']['result'] = array(
'#type' => 'textfield',
'#title' => 'Search result',
'#default_value' => isset($defaults['templates']['result']) ? $defaults['templates']['result'] : '/' . $path . '/templates/angular/result.html',
'#required' => TRUE,
);
$form['search_node']['templates']['pager'] = array(
'#type' => 'textfield',
'#title' => 'Search pager',
'#default_value' => isset($defaults['templates']['pager']) ? $defaults['templates']['pager'] : '/' . $path . '/templates/angular/pager.html',
'#required' => TRUE,
);
$form['search_node']['autocomplete'] = array(
'#type' => 'fieldset',
'#title' => t('Autocomplete (type-a-head)'),
'#description' => t('Enable auto-complete feature.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$indexes = array('_none_' => t('Select index'));
$form['search_node']['autocomplete']['index'] = array(
'#type' => 'select',
'#title' => t('Search index'),
'#description' => t('Select the search api index to use'),
'#options' => $indexes,
'#default_value' => isset($defaults['autocomplete']['index']) ? $defaults['autocomplete']['index'] : '_none_',
'#ajax' => array(
'callback' => 'search_node_page_block_auto_field_ajax_callback',
'wrapper' => 'search-node-page-block-auto-field',
),
);
$form['search_node']['autocomplete']['field'] = array(
'#type' => 'select',
'#title' => t('Field to use'),
'#description' => t('Select the field use'),
'#options' => array('_none_' => t('Select field')),
'#default_value' => isset($defaults['autocomplete']['field']) ? $defaults['autocomplete']['field'] : '_none_',
'#prefix' => '<div id="search-node-page-block-auto-field">',
'#suffix' => '</div>',
);
$form['search_node']['autocomplete']['minChars'] = array(
'#type' => 'textfield',
'#title' => t('Minimum number of chars to match'),
'#default_value' => isset($defaults['autocomplete']['minChars']) ? $defaults['autocomplete']['minChars'] : '3',
);
$form['search_node']['autocomplete']['size'] = array(
'#type' => 'textfield',
'#title' => t('Number of hits to return'),
'#default_value' => isset($defaults['autocomplete']['size']) ? $defaults['autocomplete']['size'] : '1',
);
return $form;
}
/**
* Ajax callback to update the block and panes configuration form.
*/
function search_node_page_block_index_ajax_callback(array $form, array &$form_state) {
return isset($form['settings']['search_node']['index']) ? $form['settings']['search_node']['index'] : $form['search_node']['index'];
}
/**
* Ajax callback to update the block and panes configuration form.
*/
function search_node_page_block_options_ajax_callback(array $form, array &$form_state) {
return isset($form['settings']['search_node']['options']) ? $form['settings']['search_node']['options'] : $form['search_node']['options'];
}
/**
* Ajax callback to update the block and panes configuration form.
*/
function search_node_page_block_force_field_ajax_callback(array $form, array &$form_state) {
return isset($form['settings']['search_node']['forces']['selected']) ? $form['settings']['search_node']['forces']['selected'] : $form['search_node']['forces']['selected'];
}
/**
* Ajax callback to update the block and panes configuration form.
*/
function search_node_page_block_auto_field_ajax_callback(array $form, array &$form_state) {
return isset($form['settings']['search_node']['autocomplete']['field']) ? $form['settings']['search_node']['autocomplete']['field'] : $form['search_node']['autocomplete']['field'];
}
/**
* Implements hook_block_save().
*/
function search_node_page_block_save($delta = '', $edit = array()) {
switch($delta) {
case 'search_node_search_box':
variable_set('search_node_page_search_box', $edit['search_node']);
break;
}
}
/**
* Implements hook_block_view().
*/
function search_node_page_block_view($delta='') {
$block = array();
switch($delta) {
case 'search_node_search_box' :
// Build configuration object.
$configuration = variable_get('search_node_page_search_box', array());
$block['content'] = search_node_page_build_search_box($configuration);
break;
case 'search_node_search_result':
$block['content'] = array(
'#theme' => 'search_node_page_search_results',
);
break;
}
return $block;
}
/**
* Builds the angular configuration and injects it into the page.
*
* @param array $configuration
* The configuration for this search box.
*
* @return array
* Render array to display the search box placeholder.
*/
function search_node_page_build_search_box($configuration = array()) {
if (empty($configuration)) {
drupal_set_message(t('Search node - search box needs to be configured before it can be used.'), 'error');
}
else {
// Check if the angular library is installed.
if (($library = libraries_load('angular')) && empty($library['installed'])) {
// Something went wrong.
drupal_set_message($library['error message'], 'error');
}
// Load server settings.
$server = search_api_server_load($configuration['server']);
// Load index.
$index = search_api_index_load($configuration['index']);
// Load boost for the fields.
$boost = array();
$fields = array_filter($configuration['options']['fields']);
$fieldsOptions = array_intersect_key($index->options['fields'], $fields);
foreach ($fieldsOptions as $field => $fieldOption) {
if (!empty($fieldOption['boost'])) {
$boost[$field] = $fieldOption['boost'];
}
}
// Check for host override in settings.php.
$host = variable_get('search_api_' . $server->machine_name . '_host', $server->options['host']);
// Check that the index have not been override in settings.
$search_node_index = $index->options['search_node_indexes'];
$search_node_index = variable_get('search_api_' . $server->machine_name . '_index_' . $index->machine_name, $search_node_index);
$search_subtype = '';
if ($configuration['forces']['field'] == 'field_target_group') {
foreach ($configuration['forces']['selected'] as $value) {
if (!empty($value)) {
$search_subtype = '_' . strtolower($value);
}
}
}
// Check for template overrides in settings.php.
$search_box_path = variable_get('search_template_box_' . $configuration['index'] . $search_subtype , $configuration['templates']['box']);
$search_result_path = variable_get('search_template_result_' . $configuration['index'] . $search_subtype, $configuration['templates']['result']);
$search_pager_path = variable_get('search_template_pager_' . $configuration['index'] . $search_subtype, $configuration['templates']['pager']);
// Build array to convert to json configuration.
$conf = array(
'id' => $configuration['id'],
'templates' => array(
'box' => $search_box_path,
'result' => $search_result_path,
'pager' => $search_pager_path,
),
'provider' => array(
'service' => 'searchNodeProvider',
'host' => $host,
'auth' => '/search_node/api/' . $server->machine_name . '/auth',
'index' => $search_node_index,
'fields' => array_values($fields),
'boost' => $boost,
'match_type' => isset($configuration['options']['match_type']) ? $configuration['options']['match_type'] : 'best_fields',
'match_operator' => isset($configuration['options']['match_operator']) ? $configuration['options']['match_operator'] : 'or',
'pager' => array(
'size' => (int) $configuration['options']['size'],
'page' => 0
),
'cacheExpire' => (int) $configuration['options']['cache_expire'],
),
);
// Add initial query search string.
if (isset($configuration['options']['initial_query_enable']) && $configuration['options']['initial_query_enable']) {
$conf['initialQueryText'] = $configuration['options']['initial_query_text'];
}
// Add highlight.
if (isset($configuration['highlight']['fields'])) {
$fields = array_filter($configuration['highlight']['fields']);
if (!empty($fields)) {
$conf['provider']['highlight'] = array(
'fields' => $fields,
);
}
}
// Build taxonomy filters.
if (!empty($configuration['options']['filters']['taxonomy'])) {
$filters = array();
foreach (array_filter($configuration['options']['filters']['taxonomy']) as $field) {
$info = field_info_field($field);
$vocab = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
$terms = array();
foreach (taxonomy_get_tree($vocab->vid) as $term) {
$terms[$term->name] = array(
'value' => $term->name,
);
}