/
localgov_publications.install
218 lines (192 loc) · 6.05 KB
/
localgov_publications.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the LocalGov Publications module.
*/
use Drupal\block\Entity\Block;
use Drupal\filter\Entity\FilterFormat;
use Drupal\filter\FilterFormatInterface;
/**
* Implements hook_install().
*/
function localgov_publications_install() {
localgov_publications_install_book_settings();
localgov_publications_install_pathauto_settings();
localgov_publications_install_filter();
localgov_publications_install_blocks();
}
/**
* Updates book settings.
*
* Add our localgov_publication_page content type to
* book.settings.allowed_types. This lets editors create publication pages.
*/
function localgov_publications_install_book_settings(): void {
$config = \Drupal::configFactory()->getEditable('book.settings');
$allowed_types = $config->get('allowed_types');
$allowed_types[] = 'localgov_publication_page';
$config->set('allowed_types', $allowed_types);
$config->save();
}
/**
* Updates pathauto settings.
*
* Add our localgov-publication-cover-page-alias token to
* pathauto.settings.safe_tokens. This prevents double escaping in the
* resulting URL.
*/
function localgov_publications_install_pathauto_settings(): void {
$config = \Drupal::configFactory()->getEditable('pathauto.settings');
$safe_tokens = $config->get('safe_tokens');
$safe_tokens[] = 'localgov-publication-cover-page-alias';
$config->set('safe_tokens', $safe_tokens)->save();
}
/**
* Adds our localgov_publications_heading_ids filter to the wysiwyg format.
*
* This is required for the ToC block to work consistently.
*/
function localgov_publications_install_filter(): void {
$wysiwygFormat = FilterFormat::load('wysiwyg');
if (!$wysiwygFormat instanceof FilterFormatInterface) {
return;
}
$wysiwygFormat->setFilterConfig('localgov_publications_heading_ids', [
'status' => TRUE,
'settings' => [
'keep_existing_ids' => TRUE,
],
]);
$wysiwygFormat->save();
}
/**
* Installs this module's default blocks.
*/
function localgov_publications_install_blocks(): void {
$activeTheme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
/** @var \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler */
$themeHandler = \Drupal::service('theme_handler');
$themes = ['localgov_base', 'localgov_scarfolk'];
if (!in_array($activeTheme, $themes)) {
$themes[] = $activeTheme;
}
$blocks = localgov_publications_install_block_definitions();
// Loop over every theme and block definition, so we set up all the blocks in
// all the relevant themes.
foreach ($themes as $theme) {
// Get info about the theme, so we can check we're placing blocks into
// regions that actually exist.
$themeInfo = $themeHandler->getTheme($theme);
if (empty($themeInfo)) {
continue;
}
$regions = array_keys($themeInfo->info['regions']);
foreach ($blocks as $block) {
if (!in_array($block['region'], $regions)) {
continue;
}
$block['id'] = $theme . '_' . $block['plugin'];
$block['theme'] = $theme;
Block::create($block)->save();
}
}
}
/**
* Block definitions for localgov_publications_install_blocks().
*/
function localgov_publications_install_block_definitions(): array {
$blocks[] = [
'plugin' => 'localgov_publications_page_header_block',
'region' => 'content_top',
'settings' => [
'label' => 'Publications page header block.',
'label_display' => '0',
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
],
'visibility' => [
'entity_bundle:node' => [
'id' => 'entity_bundle:node',
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
'bundles' => [
'localgov_publication_cover_page' => 'localgov_publication_cover_page',
'localgov_publication_page' => 'localgov_publication_page',
],
],
],
'weight' => -100,
];
$blocks[] = [
'plugin' => 'publication_navigation',
'region' => 'sidebar_first',
'settings' => [
'label' => 'Publication navigation',
'label_display' => 'visible',
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
],
'visibility' => [
'entity_bundle:node' => [
'id' => 'entity_bundle:node',
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
'bundles' => [
'localgov_publication_page' => 'localgov_publication_page',
],
],
],
'weight' => 0,
];
$blocks[] = [
'plugin' => 'localgov_publications_toc_block',
'region' => 'sidebar_first',
'settings' => [
'label' => 'On this page',
'label_display' => 'visible',
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
],
'visibility' => [
'entity_bundle:node' => [
'id' => 'entity_bundle:node',
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
'bundles' => [
'localgov_publication_page' => 'localgov_publication_page',
],
],
],
'weight' => 1,
];
return $blocks;
}
/**
* Fixes error messages after this module is installed.
*
* Remove references to the book content type from the key value store that are
* left over after book's config is removed.
*/
function localgov_publications_update_10001() {
// If the book content type is still installed in the site, don't do anything.
$entityTypeManager = \Drupal::service('entity_type.manager');
$bookType = $entityTypeManager->getStorage('node_type')->load('book');
if ($bookType) {
return;
}
// See localgov_publications_modules_installed() for what this code does.
// It's added here so it runs on existing installs too.
$kvStore = \Drupal::keyValue('entity.definitions.bundle_field_map');
$fieldMap = $kvStore->get('node');
if (isset($fieldMap['body']['bundles']['book'])) {
unset($fieldMap['body']['bundles']['book']);
$kvStore->set('node', $fieldMap);
}
\Drupal::cache('discovery')->delete('entity_field_map');
}