localgov_publications-1.0.14/localgov_publications.install
localgov_publications.install
<?php
/**
* @file
* Install, update and uninstall functions for the LocalGov Publications module.
*/
use Drupal\filter\Entity\FilterFormat;
use Drupal\filter\FilterFormatInterface;
use Drupal\user\RoleInterface;
use Drupal\views\ViewEntityInterface;
/**
* 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(): void {
// 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(): void {
// 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();
}
}
/**
* Grant new 'access publication views' permission to localgov_editor role.
*/
function localgov_publications_update_10003(): void {
// Make sure Editors have the new View permission.
$role = \Drupal::service('entity_type.manager')
->getStorage('user_role')
->load('localgov_editor');
if ($role instanceof RoleInterface) {
$role->grantPermission('access publication views');
$role->save();
}
}
/**
* Use 'access publication views' permission for the publication view.
*/
function localgov_publications_update_10004(): void {
/** @var ?\Drupal\views\ViewEntityInterface $publicationsView */
$publicationsView = \Drupal::service('entity_type.manager')
->getStorage('view')
->load('publications');
if (!$publicationsView instanceof ViewEntityInterface) {
return;
}
$display = $publicationsView->get('display');
if (isset($display['default']['display_options']['access'])) {
$access = $display['default']['display_options']['access'];
// Only update if the defaults haven't been changed.
if ($access['type'] === 'perm' && $access['options']['perm'] === 'access content') {
$display['default']['display_options']['access']['options']['perm'] = 'access publication views';
$publicationsView->set('display', $display)->save();
}
}
}
