-
Notifications
You must be signed in to change notification settings - Fork 1
/
openy_block_modal.module
118 lines (104 loc) · 3.24 KB
/
openy_block_modal.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
<?php
/**
* @file
* Contains openy_block_modal.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_theme().
*/
function openy_block_modal_theme($existing, $type, $theme, $path) {
return [
'modal_block' => [
'variables' => [
'modalId' => NULL,
'title' => NULL,
'description' => NULL,
'modal_header' => NULL,
'modal_content' => NULL,
'modal_link_title' => NULL,
'modal_link' => NULL,
'mode' => NULL,
'entity' => NULL,
],
'template' => 'modal-block',
],
];
}
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function openy_block_modal_block_content_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
/** @var \Drupal\block_content\Entity\BlockContent $entity */
switch ($entity->bundle()) {
case 'modal_block':
$modal_header = $entity->field_modal_header->value;
$modal_content = $entity->field_modal_content->get(0);
$linkUrl = NULL;
$link = $entity->field_link->get(0);
if ($link) {
$linkUrl = $link->getUrl();
}
$build['modal'] = [
'#theme' => 'modal_block',
'#modalId' => $entity->id(),
'#title' => $entity->field_title->value,
'#description' => [
'#type' => 'processed_text',
'#text' => $entity->field_description->value,
'#format' => $entity->field_description->format,
],
'#modal_link' => $linkUrl,
'#modal_link_title' => $entity->field_link_title->value,
'#mode' => $entity->field_mode->value,
'#entity' => $entity,
'#cache' => [
'tags' => ['openy_cron'],
],
];
if (isset($modal_header)) {
$build['modal']['#modal_header'] = $modal_header;
}
if (isset($modal_content)) {
$build['modal']['#modal_content'] = [
'#type' => 'processed_text',
'#text' => $modal_content->value,
'#format' => $modal_content->format,
];
}
$build['#attached']['library'][] = 'openy_block_modal/modal-block';
$build['#attached']['library'][] = 'core/drupal.ajax';
break;
}
}
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function openy_block_modal_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if($entity_form['#entity_type'] == 'block_content' && $entity_form['#bundle'] == 'modal_block') {
$parents = $entity_form['#parents'];
$input_name = array_shift($parents);
foreach ($parents as $p) {
$input_name = "{$input_name}[{$p}]";
}
$input_name = "{$input_name}[field_mode]";
$fields_visible = [
'visible' => [
[
':input[name="'.$input_name.'"]' => ['value' => 'fields'],
],
],
];
$node_visible = [
'visible' => [
[
':input[name="'.$input_name.'"]' => ['value' => 'node'],
],
],
];
$entity_form['field_modal_header']['widget'][0]['value']['#states'] = $fields_visible;
$entity_form['field_modal_content']['widget'][0]['#states'] = $fields_visible;
$entity_form['field_link']['widget'][0]['uri']['#states'] = $node_visible;
}
}