forked from localgovdrupal/localgov_guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localgov_guides.module
231 lines (218 loc) · 7.92 KB
/
localgov_guides.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
<?php
/**
* @file
* Localgov Guides module hooks.
*/
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\localgov_guides\ChildParentRelationship;
use Drupal\localgov_roles\RolesHelper;
/**
* Implements hook_theme().
*/
function localgov_guides_theme($existing, $type, $theme, $path) {
return [
'guides_contents_block' => [
'variables' => [
'links' => [],
'format' => [],
],
],
'guides_prev_next_block' => [
'variables' => [
'previous_url' => NULL,
'previous_title' => NULL,
'next_url' => NULL,
'next_title' => NULL,
],
],
'node__localgov_guides_overview__full' => [
'template' => 'node--localgov-guides-overview--full',
'base hook' => 'node',
],
'node__localgov_guides_page__full' => [
'template' => 'node--localgov-guides-page--full',
'base hook' => 'node',
],
];
}
/**
* Implements hook_localgov_role_default().
*/
function localgov_guides_localgov_roles_default() {
// Content editing permissions.
$perms = [
RolesHelper::EDITOR_ROLE => [
'create localgov_guides_overview content',
'create localgov_guides_page content',
'delete any localgov_guides_overview content',
'delete any localgov_guides_page content',
'delete localgov_guides_overview revisions',
'delete localgov_guides_page revisions',
'delete own localgov_guides_overview content',
'delete own localgov_guides_page content',
'edit any localgov_guides_overview content',
'edit any localgov_guides_page content',
'edit own localgov_guides_overview content',
'edit own localgov_guides_page content',
'revert localgov_guides_overview revisions',
'revert localgov_guides_page revisions',
'view localgov_guides_overview revisions',
'view localgov_guides_page revisions',
],
RolesHelper::AUTHOR_ROLE => [
'create localgov_guides_overview content',
'create localgov_guides_page content',
'delete own localgov_guides_overview content',
'delete own localgov_guides_page content',
'edit own localgov_guides_overview content',
'edit own localgov_guides_page content',
'revert localgov_guides_overview revisions',
'revert localgov_guides_page revisions',
'view localgov_guides_overview revisions',
'view localgov_guides_page revisions',
],
RolesHelper::CONTRIBUTOR_ROLE => [
'create localgov_guides_overview content',
'create localgov_guides_page content',
'delete own localgov_guides_overview content',
'delete own localgov_guides_page content',
'edit own localgov_guides_overview content',
'edit own localgov_guides_page content',
'view localgov_guides_overview revisions',
'view localgov_guides_page revisions',
],
];
// Content scheduling permissions required by localgov_workflows.
if (\Drupal::moduleHandler()->moduleExists('localgov_workflows')) {
$perms[RolesHelper::EDITOR_ROLE] = array_merge($perms[RolesHelper::EDITOR_ROLE], [
'add scheduled transitions node localgov_guides_overview',
'add scheduled transitions node localgov_guides_page',
'reschedule scheduled transitions node localgov_guides_overview',
'reschedule scheduled transitions node localgov_guides_page',
'view scheduled transitions node localgov_guides_overview',
'view scheduled transitions node localgov_guides_page',
]);
$perms[RolesHelper::AUTHOR_ROLE] = array_merge($perms[RolesHelper::AUTHOR_ROLE], [
'add scheduled transitions node localgov_guides_overview',
'add scheduled transitions node localgov_guides_page',
'reschedule scheduled transitions node localgov_guides_overview',
'reschedule scheduled transitions node localgov_guides_page',
'view scheduled transitions node localgov_guides_overview',
'view scheduled transitions node localgov_guides_page',
]);
$perms[RolesHelper::CONTRIBUTOR_ROLE] = array_merge($perms[RolesHelper::CONTRIBUTOR_ROLE], [
'add scheduled transitions node localgov_guides_overview',
'add scheduled transitions node localgov_guides_page',
'reschedule scheduled transitions node localgov_guides_overview',
'reschedule scheduled transitions node localgov_guides_page',
'view scheduled transitions node localgov_guides_overview',
'view scheduled transitions node localgov_guides_page',
]);
}
return $perms;
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function localgov_guides_node_insert(NodeInterface $node) {
if ($node->bundle() == 'localgov_guides_page') {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(ChildParentRelationship::class)
->pageUpdateOverview($node);
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*
* Implements reference back on overview to page when pages are created.
*/
function localgov_guides_node_update(NodeInterface $node) {
if ($node->bundle() == 'localgov_guides_page') {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(ChildParentRelationship::class)
->pageUpdateOverview($node);
}
}
/**
* Implements hook_ENTITY_TYPE_prepare_form().
*
* Check back-reference fields before editing overview.
*/
function localgov_guides_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form) {
if ($node->bundle() == 'localgov_guides_overview') {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(ChildParentRelationship::class)
->overviewPagesCheck($node);
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*
* Check back-reference fields before saving.
* Especially if someone has changed a page since form load!
*/
function localgov_guides_node_presave(NodeInterface $node) {
if ($node->bundle() == 'localgov_guides_overview') {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(ChildParentRelationship::class)
->overviewPagesCheck($node);
}
}
/**
* Implements hook_modules_installed().
*/
function localgov_guides_modules_installed($modules) {
$services = in_array('localgov_services_navigation', $modules);
$topics = in_array('localgov_topics', $modules);
if ($services || $topics) {
\Drupal::service('config.installer')->installOptionalConfig();
localgov_guides_optional_fields_settings($services, $topics);
}
}
/**
* Set form settings for optional services and topic fields on installation.
*
* @param bool $services
* If localgov_services is (being) installed.
* @param bool $topics
* If localgov_topics is (being) installed.
*/
function localgov_guides_optional_fields_settings($services, $topics) {
$properties = [
'targetEntityType' => 'node',
'bundle' => 'localgov_guides_overview',
];
if ($form_displays = \Drupal::entityTypeManager()->getStorage('entity_form_display')->loadByProperties($properties)) {
foreach ($form_displays as $form_display) {
assert($form_display instanceof EntityFormDisplayInterface);
if ($services && !$form_display->getComponent('localgov_services_parent')) {
$form_display->setComponent('localgov_services_parent', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
'match_limit' => 10,
],
'weight' => 51,
])->save();
}
if ($topics && !$form_display->getComponent('localgov_topics')) {
$form_display->setComponent('localgov_topic_classified', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
'match_limit' => 10,
],
'weight' => 50,
])->save();
}
}
}
}