-
Notifications
You must be signed in to change notification settings - Fork 3
/
localgov_publications.install
118 lines (104 loc) · 3.74 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
<?php
/**
* @file
* Install, update and uninstall functions for the LocalGov Publications module.
*/
use Drupal\filter\Entity\FilterFormat;
use Drupal\filter\FilterFormatInterface;
/**
* Implements hook_install().
*/
function localgov_publications_install($is_syncing) {
if ($is_syncing) {
return;
}
localgov_publications_install_book_settings();
localgov_publications_install_pathauto_settings();
localgov_publications_install_filter();
}
/**
* 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';
$safe_tokens[] = 'localgov-publication-path';
$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();
}
/**
* 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 (!is_null($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');
}
/**
* Set up the new localgov-publication-path token on existing installations.
*/
function localgov_publications_update_10002() {
// Add our new token into pathauto's list of safe tokens.
$config = \Drupal::configFactory()->getEditable('pathauto.settings');
$safe_tokens = $config->get('safe_tokens');
$safe_tokens[] = 'localgov-publication-path';
$config->set('safe_tokens', $safe_tokens)->save();
// Use the new pattern for publication pages if the old default pattern is
// currently still in place.
$oldPattern = '[node:localgov-publication-cover-page-alias]/[node:book:parents:join-path]/[node:title]';
$newPattern = '[node:localgov-publication-path]/[node:title]';
$config = \Drupal::configFactory()->getEditable('pathauto.pattern.publication_page');
if ($config->get('pattern') === $oldPattern) {
$config->set('pattern', $newPattern)->save();
}
}