-
Notifications
You must be signed in to change notification settings - Fork 21
/
az_quickstart.post_update.php
71 lines (65 loc) · 2.58 KB
/
az_quickstart.post_update.php
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
<?php
/**
* @file
* Post update functions for AZ Quickstart.
*/
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\user\Entity\Role;
/**
* Force import of core block_content view.
*
* Ensure legacy Quickstart block_content view is replaced with the Drupal core
* version updated in 10.1.x.
*/
function az_quickstart_post_update_force_import_core_block_view(&$sandbox) {
$config_to_import = 'views.view.block_content';
$module_path = \Drupal::service('extension.list.module')->getPath('block_content');
$config_storage = new FileStorage($module_path . '/config/optional');
$entity_type_id = \Drupal::service('config.manager')->getEntityTypeIdByName($config_to_import);
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
/** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
$entity_type = $storage->getEntityType();
$id = $storage->getIDFromConfigName($config_to_import, $entity_type->getConfigPrefix());
$active = $storage->load($id);
$config_record = $config_storage->read($config_to_import);
// Add a config hash if necessary.
if (empty($config_record['_core']['default_config_hash'])) {
$config_record['_core']['default_config_hash'] = Crypt::hashBase64(serialize($config_record));
}
try {
$entity = $storage->updateFromStorageRecord($active, $config_record);
$entity->save();
}
catch (EntityStorageException $e) {
\Drupal::logger('az_quickstart')->notice($e->getMessage());
}
}
/**
* Remove unneeded block_content permissions (core + contrib) from AZQS roles.
*/
function az_quickstart_post_update_remove_block_content_permissions_from_roles(&$sandbox) {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function (Role $role) {
$update = FALSE;
if (in_array($role->get('id'), ['az_content_admin', 'az_content_editor'])) {
$permissions_to_remove = [
'administer block content types',
'administer block types',
'update any az_custom_menu_block block content',
'update any az_flexible_block block content',
'update any az_quick_links block content',
'view restricted block content',
];
foreach ($permissions_to_remove as $permission) {
if ($role->hasPermission($permission)) {
$role->revokePermission($permission);
$update = TRUE;
}
}
}
return $update;
});
}