-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_search_tips.module
216 lines (195 loc) · 9.73 KB
/
advanced_search_tips.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
<?php
/**
* @file
* Contains advanced_search_tips.module.
*/
use \Drupal\Core\Routing\RouteMatchInterface;
use \Drupal\node\Entity\Node;
use \Drupal\advanced_search\Form\SettingsForm;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_help().
*/
function advanced_search_tips_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the advanced_search_tips module.
case 'help.page.advanced_search_tips':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('My Awesome Module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function advanced_search_tips_theme() {
return [
'advanced_search_tips' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_form_alter
*/
function advanced_search_tips_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$config = \Drupal::service('config.factory')->getEditable(SettingsForm::CONFIG_NAME);
if ($form_id === "advanced_search_form") {
if (isset($form['ajax']['recursive'])) {
if ($config->get("show-hide-include-sub-collections") != 1) {
$form['ajax']['recursive']['#default_value'] = TRUE;
$form['ajax']['recursive']['#access'] = FALSE;
$form['ajax']['recursive']['#attributes'] = ['style' => 'display: none;'];
}else {
if ($config->get("enable-include-sub-collections") === 1) {
$form['ajax']['recursive']['#default_value'] = TRUE;
}
if (!empty($config->get("customize-include-sub-collections-label"))) {
$form['ajax']['recursive']['#title'] = $config->get("customize-include-sub-collections-label");
}
}
}
}
if ($form_id === "advanced_search_settings_form") {
$form['advanced_search_tips'] = [
'#type' => 'fieldset',
'#title' => "Advanced Search Tips",
'#weight' => -1,
];
// get default advanced search tips text
$path = \Drupal::service('extension.path.resolver')->getPath('module', 'advanced_search_tips') . "/assets/default_tip_htmls.txt";
$default_text = file_get_contents($path);
$form['advanced_search_tips']['tips_title'] = array(
'#type' => 'textfield',
'#title' => t('Title:'),
'#default_value' => ($config->get("tips_title") !== null) ? $config->get("tips_title") : "Advanced Search Tips",
'#description' => '<p><strong>Note: </strong></p><ul> <li>For multilingual, switch over <strong>Source </strong> mode, then
<ul>
<li>Tag multilingal content with associate language code ie. English => en, Tamil => ta, or French => fr </li>
<li>If there are complicated hiarachy of html components, only tagging the parent tag instead all of its chidlren </li>
</ul></li></ul>'
);
$form['advanced_search_tips']['tips_html'] = array(
'#type' => 'text_format',
'#title' => t('Detail:'),
'#format' => "full_html",
'#allowed_formats' => ['full_html'],
'#weight' => 0,
'#default_value' => ($config->get("tips_html") !== null) ? $config->get("tips_html") : $default_text,
'#translation' => TRUE,
);
$form['advanced_search_validation'] = [
'#type' => 'fieldset',
'#title' => "Advanced Search Assistance",
'#weight' => -1,
];
$form['advanced_search_validation']['search_request_validation'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Search request validation.'),
'#description' => t('This option is only available if the module Advanced Search Tips is enable.'),
'#default_value' => ($config->get("search_request_validation") !== null) ? $config->get("search_request_validation") : FALSE
);
$form['advanced_search_validation']['enable-include-sub-collections'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Include Sub-Collections checkbox by default.'),
'#description' => t('This option is only available if the module Advanced Search Tips is enable.'),
'#default_value' => ($config->get("enable-include-sub-collections") !== null) ? $config->get("enable-include-sub-collections") : FALSE
);
$form['advanced_search_validation']['show-hide-include-sub-collections'] = array(
'#type' => 'checkbox',
'#title' => t('Show Include Sub-Collections checkbox.'),
'#description' => t('This option is only available if the module Advanced Search Tips is enable.'),
'#default_value' => ($config->get("show-hide-include-sub-collections") !== null) ? $config->get("show-hide-include-sub-collections") : TRUE
);
$form['advanced_search_validation']['customize-include-sub-collections-label'] = array(
'#type' => 'textfield',
'#title' => t('Customize the message of "Include Sub-Collections checkbox"'),
'#default_value' => ($config->get("customize-include-sub-collections-label") !== null) ? $config->get("customize-include-sub-collections-label") : "Include Sub-Collections"
);
$form['form_id'] = array(
'#type' => 'hidden',
'#value' => $form_id,
);
$form['#submit'][] = 'advanced_search_tips_form_submit';
}
if ($form_id === "advanced_search_form") {
$form['#attached']['library'][] = 'advanced_search_tips/advanced.search.tips';
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== "barriodepartments")
$form['#attached']['library'][] = 'advanced_search_tips/bootstrap.modal';
$form["advanced_search_tips_button"] = [
'#type' => 'markup',
"#markup" => new TranslatableMarkup('<span id="search-help"><button type="button" class="btn btn-link dsu" data-toggle="modal" data-target="#advancedSearchTipsModelCenter"><i class="fa-solid fa-circle-info"></i></button></span>')
];
if (\Drupal::languageManager()->isMultilingual()) {
// Get the current language code from Drupal
$lang_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
// Load the HTML string into a DOMDocument
$dom = new DOMDocument();
libxml_use_internal_errors(true); // Suppress errors due to malformed HTML
$dom->loadHTML($config->get("tips_html"));
libxml_clear_errors();
// Create a new DOMXPath object
$xpath = new DOMXPath($dom);
// Query for elements with the lang attribute matching the current language code
$query = "//*[@lang='$lang_code']";
$elements = $xpath->query($query);
$search_tips = "";
// Iterate over the matched elements and output them
foreach ($elements as $element) {
$search_tip .= $dom->saveHTML($element) . "\n";
}
}else {
$search_tip = $config->get("tips_html");
}
$form["advanced_search_tips_modal-popup"] = [
'#type' => 'markup',
"#markup" => new TranslatableMarkup('
<div class="modal fade" aria-hidden="true" aria-labelledby="advancedSearchTipsModelCenterTitle" id="advancedSearchTipsModelCenter" role="dialog" tabindex="-1">
<div class="modal-dialog modal-lg modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title" id="exampleModalLongTitle">
' . t($config->get("tips_title")) .'
</h3>
<p>
<button class="close" aria-label="Close" data-dismiss="modal" type="button"><span aria-hidden="true">×</span></button>
</p>
</div>
<div class="modal-body">
' . $search_tip .'
</div>
</div>
</div>
</div>')
];
}
}
/**
* Implements hook_form_submit
*/
function advanced_search_tips_form_submit(&$form, &$form_state){
if ($form_state->getValue('form_id') !== null && $form_state->getValue('form_id') === "advanced_search_settings_form") {
$config = \Drupal::service('config.factory')->getEditable(SettingsForm::CONFIG_NAME);
$config
->set(SettingsForm::SEARCH_QUERY_PARAMETER, $form_state->getValue(SettingsForm::SEARCH_QUERY_PARAMETER))
->set(SettingsForm::SEARCH_RECURSIVE_PARAMETER, $form_state->getValue(SettingsForm::SEARCH_RECURSIVE_PARAMETER))
->set(SettingsForm::SEARCH_ADD_OPERATOR, $form_state->getValue(SettingsForm::SEARCH_ADD_OPERATOR))
->set(SettingsForm::SEARCH_REMOVE_OPERATOR, $form_state->getValue(SettingsForm::SEARCH_REMOVE_OPERATOR))
->set(SettingsForm::FACET_TRUNCATE, $form_state->getValue(SettingsForm::FACET_TRUNCATE))
->set(SettingsForm::EDISMAX_SEARCH_FLAG, $form_state->getValue(SettingsForm::EDISMAX_SEARCH_FLAG))
->set(SettingsForm::EDISMAX_SEARCH_LABEL, $form_state->getValue(SettingsForm::EDISMAX_SEARCH_LABEL))
->set(SettingsForm::SEARCH_ALL_FIELDS_FLAG, $form_state->getValue(SettingsForm::SEARCH_ALL_FIELDS_FLAG))
->set(SettingsForm::DISPLAY_LIST_FLAG, $form_state->getValue(SettingsForm::DISPLAY_LIST_FLAG))
->set(SettingsForm::DISPLAY_GRID_FLAG, $form_state->getValue(SettingsForm::DISPLAY_GRID_FLAG))
->set(SettingsForm::DISPLAY_DEFAULT, $form_state->getValue(SettingsForm::DISPLAY_DEFAULT))
->set("tips_html", $form_state->getValue("tips_html")['value'])
->set("tips_title", $form_state->getValue("tips_title"))
->set("search_request_validation", $form_state->getValue("search_request_validation"))
->set("enable-include-sub-collections", $form_state->getValue("enable-include-sub-collections"))
->set("show-hide-include-sub-collections", $form_state->getValue("show-hide-include-sub-collections"))
->set("customize-include-sub-collections-label", $form_state->getValue("customize-include-sub-collections-label"))
->save();
}
}