-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdb_brugbyen.module
89 lines (72 loc) · 3.2 KB
/
kdb_brugbyen.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
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* @file
* Integration with brugbyen.kk.dk
*/
/**
* Implements hook_form_alter().
*/
function kdb_brugbyen_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if (!in_array($form_id, ['eventseries_default_add_form', 'eventseries_default_edit_form'])) {
return;
}
$form['#attached']['library'][] = 'kdb_brugbyen/fieldset';
$form['#validate'][] = 'kdb_brugbyen_eventseries_default_validate';
$form['field_bb_district']['widget']['#description'] = t('Selected "district" is used for filtering events on brugbyen.');
// Only activate the secondary categories, target groups and tags elements
// when district is non-empty.
$districtIsEmpty = [':input[name="field_bb_district"]' => ['value' => '']];
$form['field_bb_categories']['widget']['#description'] = t('Selected "category" is used for filtering events on brugbyen.');
$form['field_bb_categories']['#states'] = [
// We go with both invisible and disabled. Adding in disabled has the
// sideeffect of not submitting any value, so the fields is automatically
// cleared when district is unset.
'!visible' => $districtIsEmpty,
'disabled' => $districtIsEmpty,
];
// Mark the required fields as such. Setting #required would cause the Form
// API to require the fields unconditionally, but we only require them if
// district is non empty. So we have our own validation and just add the
// required classes to the label. As the fields are hidden when not required,
// we don't need to do this in states (which doesn't work with Select2 and/or
// field widgets for some reason).
$form['field_bb_categories']['widget']['#label_attributes'] = ['class' => ['form-required', 'js-form-required']];
$form['field_bb_target_groups']['widget']['#description'] = t('Selected "target group" is used for filtering events on brugbyen.');
$form['field_bb_target_groups']['#states'] = [
'!visible' => $districtIsEmpty,
'disabled' => $districtIsEmpty,
];
$form['field_bb_target_groups']['widget']['#label_attributes'] = ['class' => ['form-required', 'js-form-required']];
$form['field_bb_tags']['widget']['#description'] = t('Selected "tag" is used for filtering events on brugbyen.');
$form['field_bb_tags']['#states'] = [
'!visible' => $districtIsEmpty,
'disabled' => $districtIsEmpty,
];
}
/**
* Validate handler for brugbyen data.
*
* Validates that categories and target_groups are provided when a district is
* selected.
*/
function kdb_brugbyen_eventseries_default_validate(array &$form, FormStateInterface $form_state) {
if (!empty($form_state->getValue('field_bb_district'))) {
if (empty($form_state->getValue('field_bb_categories'))) {
$form_state->setError(
$form['field_bb_categories']['widget'],
t('@name field is required.', [
'@name' => $form['field_bb_categories']['widget']['#title'],
]),
);
}
if (empty($form_state->getValue('field_bb_target_groups'))) {
$form_state->setError(
$form['field_bb_target_groups']['widget'],
t('@name field is required.', [
'@name' => $form['field_bb_target_groups']['widget']['#title'],
]),
);
}
}
}