-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslt_contacts.module
73 lines (67 loc) · 2.46 KB
/
slt_contacts.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
<?php
/**
* @file
* Code for the SLT contacts module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*
* Change default value for the exposed country selector.
*/
function slt_contacts_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$view = $form_state->getStorage('view');
if (isset($view['view']) && $view['view']->id() === 'contacts' && $view['view']->current_display === 'page_contacts') {
// We re-use the 'All' option to avoid validation errors and add a process
// function to actually remove the filter when 'All' is selected.
$form['country']['#options']['All'] = t('- Choose a country -');
$form['country']['#process'][] = 'slt_contacts_reset_country_default_filter';
}
}
/**
* Process function for the exposed country filter in the contacts view.
*
* This ensures that no data is returned with "Choose a country" is the
* currently selected option.
*
* @param array $element
* Form element to process.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return array
* Processed element.
*
* @see \Drupal\views\ViewExecutable::getExposedInput()
*/
function slt_contacts_reset_country_default_filter(array $element, FormStateInterface $form_state) {
$view = $form_state->getStorage('view');
if (isset($view['view']) && $form_state->getValue('country') === 'All') {
$input = $view['view']->getExposedInput();
unset($input['country']);
// We set a dummy input value to ensure ViewExecutable::getExposedInput()
// doesn't bail out due to an empty input which would counteract the
// removal of the 'country' filter value and would cause the query to
// return all the entries as if 'All' were still selected.
$input['__dummy'] = 'Dummy input';
$view['view']->setExposedInput($input);
}
return $element;
}
/**
* Implements hook_views_data_alter().
*
* Change the sort handler for the seurity title field so that they can
* be sorted according to a predefined order (title hierarchy) [SLT-14].
*/
function slt_contacts_views_data_alter(array &$data) {
$data['node__field_security_title']['hierarchical_security_title_sort'] = [
'title' => t('Hierarchical security title sort'),
'group' => t('Content'),
'help' => t('Sort contacts by security title hierarchically.'),
'sort' => [
'field' => 'field_security_title_value',
'id' => 'hierarchical_security_title_sort',
],
];
}