This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_block_overrides.module
129 lines (119 loc) · 4.38 KB
/
views_block_overrides.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
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\Block\ViewsBlock;
use Drupal\views\Plugin\views\display\Block as CoreBlock;
use Drupal\ctools_views\Plugin\Display\Block as CtoolsBlock;
use Drupal\views_block_overrides\Plugin\views\display\CtoolsBlockOverrides;
use Drupal\views_block_overrides\Plugin\Block\ViewsBlockOverride;
/**
* Implements hook_field_widget_FIELD-ID_FORM_alter().
*
* TODO add settings form to alter only specific blocks.
*/
function views_block_overrides_field_widget_block_field_default_form_alter(&$element, FormStateInterface $form_state, $context) {
if (isset($element['settings']['context_mapping'])) {
foreach($element['settings']['context_mapping'] as $context_element_id => &$context_element) {
if ($context_element['#type'] == 'select' && empty($context_element['#options'])) {
// Don't display the context mapping element if it's empty.
$context_element['#access'] = FALSE;
}
}
$hiden_elements = [
'views_label_checkbox',
'views_label_fieldset',
'views_label',
'label',
'label_display',
];
foreach ($hiden_elements as $name) {
if (isset($element['settings'][$name])) {
// $element['settings'][$name]['#access'] = FALSE;
}
}
}
}
/**
* Implements hook_views_plugins_display_alter().
*/
function views_block_overrides_views_plugins_display_alter(&$displays) {
// if (!empty($displays['block']['class']) && $displays['block']['class'] == CtoolsBlock::class) {
// $displays['block']['class'] = CtoolsBlockOverrides::class;
// }
}
/**
* Implements hook_block_alter().
*/
function views_block_overrides_block_alter(&$definitions) {
foreach ($definitions as &$definition) {
if (!empty($definition['class']) && $definition['class'] == ViewsBlock::class) {
// Alter all the views blocks to use the extended ViewsBlockOverride block
// type. This new class extends the basic ViewsBlock but injects the block
// instance in the view object.
// See class Drupal\views_block_overrides\Plugin\Block\ViewsBlockOverride
$definition['class'] = ViewsBlockOverride::class;
$definition['provider'] = 'views_block_overrides';
}
}
}
/**
* Implements hook_preprocess_HOOK() for views template.
*/
function views_block_overrides_preprocess(&$variables) {
if (isset($variables['view']->views_block_overrides['block_instance'])) {
$configuration = $variables['view']->views_block_overrides['block_instance']->getConfiguration();
$variables['views_block_overrides']['configuration'] = $configuration;
}
}
/**
* Implements hook_views_data_alter().
*/
function views_block_overrides_views_data_alter(array &$data) {
/** @var Drupal\views_block_overrides\Plugin\BlockSettingsPluginManager $block_settings_manager */
$block_settings_manager = \Drupal::service('plugin.manager.block_settings');
/** @var Drupal\views_block_overrides\Plugin\BlockSettingsPluginInterface $definition */
foreach ($block_settings_manager->getDefinitions() as $definition_id => $definition) {
if (!isset($definition['area']) || $definition['area'] != TRUE) {
continue;
}
$key = 'views_block_overrides_' . $definition_id;
$data['views'][$key] = [
'title' => t('Views block overrides - @plugin_title', ['@plugin_title' => $definition['title']]),
'help' => t('Views block override block settings area.'),
'area' => [
'id' => 'views_block_overrides_area',
'block_settings_plugin_id' => $definition_id,
],
];
}
}
/**
* Implements hook_theme().
*/
function views_block_overrides_theme() {
return [
'views_block_overrides_area' => [
'variables' => [
'settings' => [],
'context' => []
],
],
];
}
/**
* Implements HOOK_theme_suggestions_HOOK_alter().
*/
function views_block_overrides_theme_suggestions_views_block_overrides_area_alter(array &$suggestions, array $variables, $hook) {
$block_settings_plugin_id = $variables['context']['block_settings_plugin']->getPluginId();
$view = $variables['context']['view'];
$current_display = $view->current_display;
$area_id = $variables['context']['area']->options['id'];
$new_suggestions = [
[$hook, $block_settings_plugin_id],
[$hook, $view->id()],
[$hook, $current_display],
[$hook, $area_id],
];
foreach ($new_suggestions as $suggestion) {
$suggestions[] = implode('__', $suggestion);
}
}