forked from az-marketing/az-bef-extend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
az_bef_extend.module
110 lines (94 loc) · 3.8 KB
/
az_bef_extend.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ViewExecutable;
function az_bef_extend_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Attach your custom library
$form['#attached']['library'][] = 'az_bef_extend/az_bef_extend';
$view_id = '';
$display_id = '';
$vert = 0;
$reset = 0;
$config = NULL;
// Check if the form is a Views exposed form
if ($form_id === 'views_exposed_form') {
/** @var ViewExecutable $view */
$view = $form_state->get('view') ?: $form['#view'];
if ($view) {
if($view->id() !== 'watchdog'){
$view_id = $view->id();
$display_id = $view->getDisplay()->display['id'];
$form_state->set('view_id', $view_id);
$form_state->set('display_id', $display_id);
// Load the specific configuration for this view and display
$config = \Drupal::config('az_bef_extend.settings.' . $view_id . '.' . $display_id);
$vert = $form_state->getValue('az_bef_extend_vertical');
$reset = $form_state->getValue('az_bef_extend_move_reset');
// Apply settings to the form
if ($config->get('move_reset_to_top')) {
$form['#attributes']['class'][] = 'top-reset';
}
if ($config->get('vertical_style')) {
$form['#attributes']['class'][] = 'bef-vertical-style';
}
}
}
}
// Check if the form is the Views UI edit display form
if ($form_id == 'views_ui_edit_display_form') {
$view = $form_state->get('view') ?: $form['#view'];
if($view){
$view_id = $view->id();
// The display ID is usually part of the form state or URL
$display_id = $form_state->get('display_id') ?: 'default';
// Load the specific configuration for this view and display
$config = \Drupal::config('az_bef_extend.settings.' . $view_id . '.' . $display_id);
$vert = $config->get('vertical_style');
$reset = $config->get('move_reset_to_top');
}
// Check if BEF is used in this view
$is_bef = false;
if (isset($form['options']) && is_array($form['options'])) {
foreach ($form['options']['exposed_form_options'] as $key => $value) {
if (strpos($key, 'bef') !== false) {
$is_bef = true;
break;
}
}
}
if ($is_bef) {
// Add custom settings to the form
$form['options']['az_bef_extend_vertical'] = [
'#type' => 'checkbox',
'#title' => t('Use vertical filter styling'),
'#default_value' => $vert,
'#description' => t('When checked, this will apply vertical styling to the filters.'),
'#weight' => -50,
];
$form['options']['az_bef_extend_move_reset'] = [
'#type' => 'checkbox',
'#title' => t('Move reset button to top'),
'#default_value' => $reset,
'#description' => t('When checked, and if the reset button is enabled, move it to the top of the filters.'),
'#weight' => -49,
];
// Add a custom submit handler to save these settings
$form['actions']['submit']['#submit'][] = 'az_bef_extend_bef_settings_submit';
}
}
}
/**
* Custom submit handler for the BEF settings.
*/
function az_bef_extend_bef_settings_submit($form, FormStateInterface $form_state) {
$view = $form_state->get('view') ?: $form['#view'];
$view_id = $view->id();
$display_id = $form_state->get('display_id');
// Retrieve the values from the form
$vertical_style = $form_state->getValue('az_bef_extend_vertical');
$move_reset_to_top = $form_state->getValue('az_bef_extend_move_reset');
// Save the settings specific to this view and display
\Drupal::configFactory()->getEditable('az_bef_extend.settings.' . $view_id . '.' . $display_id)
->set('vertical_style', $vertical_style)
->set('move_reset_to_top', $move_reset_to_top)
->save();
}