-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarbase_layout_builder.module
408 lines (351 loc) · 14.9 KB
/
varbase_layout_builder.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* @file
* Contains varbase_layout_builder.module.
*/
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\layout_builder\Plugin\Block\InlineBlock;
use Drupal\varbase_layout_builder\Element\VarbaseLayoutBuilderUX;
use Drupal\varbase_layout_builder\Plugin\Block\VarbaseLayoutBuilderInlineBlock;
// Include all helpers.
include_once __DIR__ . '/includes/helpers.inc';
/**
* Implements hook_entity_operation().
*/
function varbase_layout_builder_entity_operation(EntityInterface $entity) {
$account = \Drupal::currentUser();
$entity_type_id = $entity->getEntityTypeId();
$entity_bundle = $entity->bundle();
if ($account->hasPermission('configure any layout')
|| $account->hasPermission('configure all ' . $entity_bundle . ' ' . $entity_type_id . ' layout overrides')
|| $account->hasPermission('access layout builder page')) {
$route_name = "layout_builder.overrides." . $entity_type_id . ".view";
$route_parameters = [
$entity_type_id => $entity->id(),
];
// If current user has access to route, then add the operation link. The
// access check will only return TRUE if the bundle is Layout Builder-
// enabled, overrides are allowed, and user has necessary permissions.
$access_manager = \Drupal::service('access_manager');
if (!$access_manager->checkNamedRoute($route_name, $route_parameters, $account)) {
return;
}
$operations['layout'] = [
'title' => t('Layout'),
'url' => Url::fromRoute($route_name, $route_parameters),
'weight' => 50,
];
return $operations;
}
}
/**
* Implements hook_form_alter().
*/
function varbase_layout_builder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
// Only for layout builder forms.
if (isset($form_id) && strpos($form_id, '_layout_builder_form') !== FALSE) {
// Attache the Varbase Layout Builder enhancements library.
$form['#attached']['library'][] = 'varbase_layout_builder/enhancements';
// Attache the Varbase Layout Builder configure section admin library.
$form['#attached']['library'][] = 'varbase_layout_builder/configure-section.admin';
// No revision information or revision log message.
if (isset($form['revision_information'])) {
$form['revision_information']['#disabled'] = TRUE;
$form['revision_information']['#attributes']['style'][] = 'display: none;';
$form['revision_information']['#prefix'] = '<div style="display: none;">';
$form['revision_information']['#suffix'] = '</div>';
}
// Hide revision.
if (isset($form['revision'])) {
$form['revision']['#default_value'] = TRUE;
$form['revision']['#disabled'] = TRUE;
$form['revision']['#attributes']['style'][] = 'display:none;';
}
// Hide revision log message.
if (isset($form['revision_log_message'])) {
$form['revision_log_message']['#disabled'] = TRUE;
$form['revision_log_message']['#attributes']['style'][] = 'display:none;';
}
// Hide rabbit hole fields.
if (isset($form['rabbit_hole'])) {
$form['rabbit_hole']['#access'] = FALSE;
}
if (isset($form['actions']['rebuild-layout'])) {
$form['actions']['rebuild-layout']['#attributes']['class'][] = 'btn btn-primary';
}
// Style the Reorder sections action button.
if (isset($form['actions']['move_sections'])) {
$form['actions']['move_sections']['#attributes']['class'][] = 'btn btn-primary';
}
}
}
$config = \Drupal::config('varbase_layout_builder.settings');
$use_claro = $config->get('use_claro');
if (isset($use_claro) && $use_claro == 1) {
$applicable_forms = [
'layout_builder_add_block',
'layout_builder_update_block',
'media_image_edit_form',
];
$add_admin_theme = FALSE;
if (in_array($form_id, $applicable_forms)) {
$add_admin_theme = TRUE;
}
else {
$current_path = \Drupal::service('path.current')->getPath();
$url_object = \Drupal::service('path.validator')->getUrlIfValid($current_path);
if (isset($url_object)
&& is_object($url_object)
&& $url_object->isRouted()
&& preg_match('/^layout_builder\.overrides\.[a-z0-9_]+\.view$/', $url_object->getRouteName())) {
$add_admin_theme = TRUE;
}
}
if ($add_admin_theme && \Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
if (\Drupal::service('theme_handler')->themeExists('gin')) {
// Add Gin theme library.
$form['#attached']['library'][] = 'varbase_layout_builder/gin';
}
elseif (\Drupal::service('theme_handler')->themeExists('claro')) {
// Add claro theme library.
$form['#attached']['library'][] = 'varbase_layout_builder/claro';
}
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for node_form.
*/
function varbase_layout_builder_form_node_form_alter(&$form, FormStateInterface $form_state) {
if (isset($form['actions'])) {
$actions_array_keys = array_keys($form['actions']);
foreach ($actions_array_keys as $action) {
if ($action != 'preview'
&& isset($form['actions'][$action]['#type'])
&& $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = '_varbase_layout_builder_form_node_form_submit';
}
}
}
}
/**
* Process the Varbase Layout Builder form node form submit.
*
* @param array $form
* The form object.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form status.
*/
function _varbase_layout_builder_form_node_form_submit(array &$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$node_type_display = \Drupal::service('entity_display.repository')->getViewDisplay('node', $node->bundle(), 'default');
// Only When submitted after a new creation of a node with which it's
// content type has an enabled layout builder on it's default display.
if (!($form_state->getFormObject()->getOperation() === 'edit')
&& !empty($node_type_display)
&& $node_type_display->isLayoutBuilderEnabled()) {
// Check if layout selection is not none.
$layout_selection_is_none = TRUE;
$layout_selection_value = $form_state->getValue('layout_selection');
if (is_array($layout_selection_value)
&& isset($layout_selection_value[0])
&& $layout_selection_value[0]['target_id']
&& !empty($layout_selection_value[0]['target_id'])) {
$layout_selection_is_none = FALSE;
}
// Get a valid edit layout url.
$edit_layout_url = \Drupal::pathValidator()->getUrlIfValid('node/' . $node->id() . '/layout');
// Have the permission name to change the layout for this content type.
$bundle_permission_name = 'configure all ' . $node->bundle() . ' node layout overrides';
$user = \Drupal::currentUser();
$user_has_permission_to_manage_the_layout = ($user->hasPermission('configure any layout') || $user->hasPermission($bundle_permission_name));
if (!empty($edit_layout_url)
&& $layout_selection_is_none
&& $user_has_permission_to_manage_the_layout) {
// Remove destination if we do have it before we create the node.
$request = \Drupal::request();
if ($request->query->has('destination')) {
$request->query->remove('destination');
}
$form_state->setRedirectUrl($edit_layout_url);
}
}
}
/**
* Implements hook_element_plugin_alter().
*/
function varbase_layout_builder_element_plugin_alter(array &$definitions) {
$definitions['layout_builder']['class'] = VarbaseLayoutBuilderUX::class;
}
/**
* Implements hook_block_alter().
*/
function varbase_layout_builder_block_alter(array &$definitions) {
foreach ($definitions as &$definition) {
if ($definition['class'] === InlineBlock::class) {
$definition['class'] = VarbaseLayoutBuilderInlineBlock::class;
}
}
}
/**
* Implements hook_layout_alter().
*/
function varbase_layout_builder_layout_alter(&$definitions) {
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
foreach ($definitions as $key => $definition) {
if ($definition->getClass() === 'Drupal\bootstrap_layout_builder\Plugin\Layout\BootstrapLayout') {
$definitions[$key]->setClass('\Drupal\varbase_layout_builder\Plugin\Layout\VarbaseLayoutBuilderBootstrapLayout');
}
}
}
else {
foreach ($definitions as $key => $definition) {
if ($definition->getClass() === 'Drupal\bootstrap_layout_builder\Plugin\Layout\BootstrapLayout') {
$definitions[$key]->setClass('\Drupal\Core\Layout\LayoutDefault');
}
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function varbase_layout_builder_preprocess_blb_container_wrapper(&$variables) {
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
$variables['attributes']['class'][] = 'vlb-section';
}
}
/**
* Implements hook_theme().
*/
function varbase_layout_builder_theme() {
return [
'blb_container_wrapper' => [
'template' => 'vlb-container-wrapper',
'variables' => [
'attributes' => [],
'children' => [],
],
],
'blb_container' => [
'template' => 'vlb-container',
'variables' => [
'attributes' => [],
'children' => [],
],
],
'blb_section' => [
'template' => 'vlb-section',
'render element' => 'content',
'base hook' => 'layout',
],
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function varbase_layout_builder_preprocess_blb_section(&$variables) {
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
if (isset($variables['content']['section_header']['#attributes'])) {
$variables['region_attributes']['section_header'] = new Attribute($variables['content']['section_header']['#attributes']);
}
if (isset($variables['content']['#settings']['container_width'])
&& $variables['content']['#settings']['container_width'] !== '') {
// VLB layout defaults.
$vlb_layout_defaults = \Drupal::config('varbase_layout_builder.layout_defaults');
// Container width layout configs.
$container_width_layout_options = $vlb_layout_defaults->get('container_width');
$container_widths = [];
if (isset($container_width_layout_options['build_options'])) {
$container_widths = $container_width_layout_options['build_options'];
}
if (isset($container_widths[$variables['content']['#settings']['container_width']])) {
$variables['content']['#settings']['container_width_classes'] = $container_widths[$variables['content']['#settings']['container_width']];
}
}
}
}
/**
* Implements hook_library_info_alter().
*/
function varbase_layout_builder_library_info_alter(&$libraries, $extension) {
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
if ($extension == 'bootstrap_styles'
&& isset($libraries['plugin.scroll_effects.build'])) {
$libraries['plugin.scroll_effects.build'] = ['dependencies' => ['varbase_layout_builder/aso.local']];
}
elseif ($extension == 'bootstrap_layout_builder'
&& isset($libraries['layout_builder_form_style'])) {
$new_path = '/' . \Drupal::service('extension.list.module')->getPath('varbase_layout_builder') . '/js';
$new_js = [];
$replacements = [
'js/layout-tab-scripts.js' => $new_path . '/vlb-layout-tab-scripts.js',
];
foreach ($libraries['layout_builder_form_style']['js'] as $source => $options) {
if (isset($replacements[$source])) {
$new_js[$replacements[$source]] = $options;
}
else {
$new_js[$source] = $options;
}
}
$libraries['layout_builder_form_style']['js'] = $new_js;
}
}
}
/**
* Implements hook_element_info_alter().
*/
function varbase_layout_builder_element_info_alter(array &$info) {
// Add Admin theme condition class switcher.
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() == \Drupal::config('system.theme')->get('admin')) {
if (isset($info['layout_builder']) && isset($info['layout_builder']['#pre_render'])) {
foreach ($info['layout_builder']['#pre_render'] as $key => $class_name) {
if ($class_name === '\Drupal\section_library\SectionLibraryRender::preRender') {
unset($info[$key]);
$info['layout_builder']['#pre_render'][$key] = '\Drupal\varbase_layout_builder\Theme\VarbaseSectionLibraryRender::preRender';
}
}
}
}
}
/**
* Implements hook_css_alter().
*/
function varbase_layout_builder_css_alter(&$css, AttachedAssetsInterface $assets) {
if (varbase_layout_builder__is_layout_builder_route()) {
if (\Drupal::service('theme_handler')->themeExists('stable9')) {
$stable_theme_css = \Drupal::service('extension.list.theme')->getPath('stable9') . '/css';
unset($css[$stable_theme_css . '/layout_builder/layout-builder.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.theme.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.details.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.reset.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.base.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.table.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.tabledrag.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.form.css']);
unset($css[$stable_theme_css . '/core/dialog/off-canvas.button.css']);
}
unset($css['core/misc/dialog/off-canvas/css/reset.css']);
unset($css['core/misc/dialog/off-canvas/css/wrapper.css']);
unset($css['core/misc/dialog/off-canvas/css/titlebar.css']);
unset($css['core/misc/dialog/off-canvas/css/dropbutton.css']);
unset($css['core/misc/dialog/off-canvas/css/messages.css']);
unset($css['core/misc/dialog/off-canvas/css/details.css']);
unset($css['core/misc/dialog/off-canvas/css/form.css']);
unset($css['core/misc/dialog/off-canvas/css/button.css']);
unset($css['core/misc/dialog/off-canvas/css/base.css']);
unset($css['core/misc/dialog/off-canvas/css/table.css']);
unset($css['core/misc/dialog/off-canvas/css/utility.css']);
unset($css['core/misc/dialog/off-canvas/css/drupal.css']);
// unset($css['core/misc/dialog/off-canvas/css/tabledrag.css']);
// unset($css['core/misc/dialog/off-canvas/css/throbber.css']);.
if (\Drupal::service('theme.manager')->getActiveTheme()->getName() !== \Drupal::config('system.theme')->get('admin')) {
unset($css['core/modules/layout_builder/css/off-canvas.css']);
}
}
}