-
Notifications
You must be signed in to change notification settings - Fork 2
/
elasticsearch_connector.module
311 lines (277 loc) · 10.4 KB
/
elasticsearch_connector.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
<?php
/**
* @file
* Provides hook implementations and functions accessible from other modules.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
use Illuminate\Support\Str;
/**
* Implements hook_help().
*/
function elasticsearch_connector_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.elasticsearch_connector':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Abstraction of making connection to the elasticsearch server. This module is API for a whole bunch of functionality connected with this module. Provides an interface to connect to a elasticsearch cluster and implements the official Elasticsearch-php library.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Create cluster') . '</dt>';
$output .= '<dd>' . t('To be described...') . '</dd>';
$output .= '<dt>' . t('Create index') . '</dt>';
$output .= '<dd>' . t('To be described...') . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_cron().
*/
function elasticsearch_connector_cron() {
// TODO: Check cluster node state and update cluster nodes if any changes.
// Do this only if we have auto-node update configuration enabled.
// The default state of the auto mode will be activated!
}
/**
* Alter the mapping of Drupal data types to Search API data types.
*
* @param array $mapping
* An array mapping all known (and supported) Drupal data types to their
* corresponding Search API data types. A value of FALSE means that fields of
* that type should be ignored by the Search API.
*
* @see \Drupal\search_api\Utility\DataTypeHelperInterface::getFieldTypeMapping()
*/
function elasticsearch_connector_search_api_field_type_mapping_alter(array &$mapping) {
$mapping['object'] = 'object';
$mapping['nested_object'] = 'object';
}
/**
* Implements hook_search_api_views_field_handler_mapping_alter().
*/
function elasticsearch_connector_search_api_views_field_handler_mapping_alter(array &$mapping) {
$mapping['object'] = $mapping['nested_object'] = [
'id' => 'elasticsearch_object',
];
}
/**
* Implements hook_search_api_views_handler_mapping_alter().
*/
function elasticsearch_connector_search_api_views_handler_mapping_alter(array &$mapping) {
$mapping['object'] = $mapping['nested_object'] = [
'argument' => [
'id' => 'search_api',
],
'filter' => [
'id' => 'elasticsearch_object',
],
'sort' => [
'id' => 'elasticsearch_object',
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function elasticsearch_connector_form_view_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['#attached']['library'][] = 'elasticsearch_connector/drupal.elasticsearch_connector.views_ui';
}
/**
* Default index settings
*
* @return array
*/
function elasticsearch_connector_default_index_third_party_settings() {
return [
'index' => [
'number_of_shards' => 5,
'number_of_replicas' => 1,
'refresh_interval' => 1,
],
];
}
/**
* Implements hook_form_FORM_alter() on behalf of
* elasticsearch_connector.module.
*
* @see \Drupal\search_api\Form\IndexForm method buildForm at
* /core/lib/Drupal/Core/Entity/EntityForm.php
*/
function elasticsearch_connector_form_search_api_index_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// We need to restrict by form ID here because this function is also called
// via hook_form_BASE_FORM_ID_alter (which is wrong, e.g. in the case of the
// form ID search_api_field_config).
if (!in_array($form_id, [
'search_api_index_form',
'search_api_index_edit_form',
])) {
return;
}
/** @var \Drupal\search_api\IndexInterface $index */
$index = $form_state->getFormObject()->getEntity();
$settings = [];
if (!$index->isNew()) {
$settings = $index->getThirdPartySettings('elasticsearch_connector');
}
$settings = array_merge(
elasticsearch_connector_default_index_third_party_settings(),
$settings
);
$form['third_party_settings']['elasticsearch_connector'] = [
'#tree' => TRUE,
'#type' => 'details',
'#title' => t('Elasticsearch Index Settings'),
'#collapsed' => TRUE,
];
$form['third_party_settings']['elasticsearch_connector']['index']['number_of_shards'] = [
'#type' => 'number',
'#title' => t('Number of shards'),
'#required' => TRUE,
'#min' => 1,
'#max' => 100,
'#default_value' => $settings['index']['number_of_shards'],
'#description' => t('Enter the number of shards for the index.'),
];
$form['third_party_settings']['elasticsearch_connector']['index']['number_of_replicas'] = [
'#type' => 'number',
'#title' => t('Number of replica'),
'#required' => TRUE,
'#min' => 1,
'#max' => 100,
'#default_value' => $settings['index']['number_of_replicas'],
'#description' => t('Enter the number of replica shards for the index.'),
];
$form['third_party_settings']['elasticsearch_connector']['index']['refresh_interval'] = [
'#type' => 'number',
'#title' => t('Refresh interval'),
'#required' => TRUE,
'#field_suffix' => 's',
'#min' => -1,
'#default_value' => $settings['index']['refresh_interval'],
'#description' => t('How often to perform a refresh operation, which makes recent changes to the index visible to search.'),
];
}
/**
* Implements hook_form_FORM_ID_alter() on behalf of facets module
*/
function elasticsearch_connector_form_facets_facet_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\facets\Entity\Facet $entity */
$entity = $form_state->getFormObject()->getEntity();
$facet_source_id = $entity->getFacetSourceId();
if (empty($facet_source_id)) {
return;
}
$plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
$facet_source = $plugin_manager->createInstance($facet_source_id, ['facet' => $entity]);
if (!($facet_source instanceof SearchApiDisplay)) {
return;
}
$index = $facet_source->getIndex();
$source_fields = $source_fields = $form['facet_source_configs'][$facet_source_id]['field_identifier']['#options'];
$index_fields = $index->getFields();
// Remove hidden fields from source fields.
foreach ($index_fields as $field) {
if ($field->isHidden() && isset($source_fields[$field->getFieldIdentifier()])) {
unset($source_fields[$field->getFieldIdentifier()]);
continue;
}
}
$form['facet_source_configs'][$facet_source_id]['field_identifier']['#options'] = $source_fields;
// Detect nested object fields.
$nested_source_fields = [];
foreach ($source_fields as $source_field_key => $source_field) {
if (Str::contains($source_field_key, '__')) {
[$object_field] = explode('__', $source_field_key);
if (isset($index_fields[$object_field]) && $index_fields[$object_field]->getType() === 'nested_object') {
$nested_source_fields[$source_field_key] = $source_field;
}
}
}
if (empty($nested_source_fields)) {
return;
}
// Third-party settings
$settings = [];
if (!$entity->isNew()) {
$settings = $entity->getThirdPartySettings('elasticsearch_connector');
}
$settings = array_merge(
[
'nested' => [
'filter_field_value' => '',
'filter_field_identifier' => '',
],
],
$settings
);
// Add settings for nested object facets.
$form['facet_source_configs'][$facet_source_id]['field_identifier']['#weight'] = 0;
$state_selector = sprintf(':input[name="facet_source_configs[%s][field_identifier]"]', $facet_source_id);
$state_values = [];
foreach (array_keys($nested_source_fields) as $trigger_value) {
$state_values[] = ['value' => $trigger_value];
}
$form['facet_source_configs'][$facet_source_id]['nested']['filter_field_identifier'] = [
'#type' => 'select',
'#options' => $nested_source_fields,
'#title' => t('Nested filter field'),
'#description' => t('The complementary field from the selected nested field which contains specific type identification.'),
'#required' => FALSE,
'#default_value' => $settings['nested']['filter_field_identifier'],
'#weight' => 1,
'#states' => [
'visible' => [
$state_selector => $state_values,
],
],
];
$form['facet_source_configs'][$facet_source_id]['nested']['filter_field_value'] = [
'#type' => 'textfield',
'#title' => t('Nested filter field value'),
'#default_value' => $settings['nested']['filter_field_value'],
'#description' => t('The value to filter nested buckets against before building aggregations of a specific type.'),
'#weight' => 1,
'#states' => [
'visible' => [
$state_selector => $state_values,
],
],
];
foreach (array_keys($form['actions']) as $action) {
if ($action !== 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'elasticsearch_connector_form_facets_facet_form_submit';
}
}
}
/**
* Implements save handler for altered facets form.
*/
function elasticsearch_connector_form_facets_facet_form_submit(array $form, FormStateInterface $form_state) {
/** @var \Drupal\facets\Entity\Facet $entity */
$entity = $form_state->getFormObject()->getEntity();
$facet_source_id = $entity->getFacetSourceId();
if (empty($facet_source_id)) {
return;
}
$plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
/** @var \Drupal\facets\FacetSource\FacetSourcePluginInterface $facet_source */
$facet_source = $plugin_manager->createInstance($facet_source_id, ['facet' => $entity]);
if (!($facet_source instanceof SearchApiDisplay)) {
return;
}
$value = $form_state->getValue([
'facet_source_configs',
$facet_source_id,
'nested',
]);
if (!empty($value)) {
$entity->setThirdPartySetting('elasticsearch_connector', 'nested', $value);
}
else {
$entity->unsetThirdPartySetting('elasticsearch_connector', 'nested');
}
$entity->save();
}