-
Notifications
You must be signed in to change notification settings - Fork 0
/
masquerade_float_block.module
61 lines (52 loc) · 2.09 KB
/
masquerade_float_block.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
<?php
/**
* @file
* File contains JS float block initialization.
*
* The module loads all needed libraries for initializing jQuery UI dialog
* library as well as jQuery.cookies plugin to remember the block position.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_page_attachments_alter().
*/
function masquerade_float_block_page_attachments_alter(array &$page) {
$config = \Drupal::configFactory()->get('masquerade_float_block.settings');
$can_switch_block = \Drupal::currentUser()
->hasPermission('manage masquerade float block visibility');
$mfb_show = \Drupal::requestStack()->getCurrentRequest()->get('mfb_show');
if (!is_null($mfb_show) && $can_switch_block) {
if (in_array($mfb_show, [1, 0])) {
$config = \Drupal::configFactory()
->getEditable('masquerade_float_block.settings');
$config->set('visible', (integer) $mfb_show);
$config->save();
}
else {
drupal_set_message(t('Incorrect mfb_show parameter value: only 1 or 0 possible.'), 'error');
}
}
elseif (!is_null($mfb_show) && !$can_switch_block) {
drupal_set_message(t('You have no access to switch the Masquerade Float Block visibility'), 'error');
}
$visible = $config->get('visible');
if ($visible) {
$form = \Drupal::formBuilder()->getForm('Drupal\masquerade\Form\MasqueradeForm');
$render_service = \Drupal::service('renderer');
$html = $render_service->renderPlain($form);
$block_title = t('Masquerade');
$page['#attached']['library'][] = 'masquerade_float_block/masquerade-float-block';
$page['#attached']['drupalSettings']['masquerade_float_block']['block']['content'] = $html;
$page['#attached']['drupalSettings']['masquerade_float_block']['block']['subject'] = $block_title;
}
}
/**
* Implements hook_help().
*/
function masquerade_float_block_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.masquerade_float_block':
$output = file_get_contents(drupal_get_path('module', 'masquerade_float_block') . '/README.txt');
return '<pre>' . ($output) . '</pre>';
}
}