entity_bundle_permissions-1.0.0-alpha1/entity_bundle_permissions.install
entity_bundle_permissions.install
<?php
/**
* @file
* Automatically grants all new dynamic permissions on install.
*
* Copyright (C) 2022 Library Solutions, LLC (et al.).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
use Drupal\user\RoleInterface;
/**
* Implements hook_install().
*/
function entity_bundle_permissions_install() {
/** @var \Drupal\user\PermissionHandlerInterface */
$permission_handler = \Drupal::service('user.permissions');
$user_role_storage = \Drupal::entityTypeManager()->getStorage('user_role');
foreach ($permission_handler->getPermissions() as $permission => $info) {
// We use the 'dependencies' key as a sentinel for dynamic permissions.
if ($info['provider'] === 'entity_bundle_permissions' && \array_key_exists('dependencies', $info)) {
/** @var \Drupal\user\RoleInterface[] */
$roles ??= $user_role_storage->loadMultiple([
RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID,
]);
foreach ($roles as $role) {
$role->grantPermission($permission);
}
}
}
foreach ($roles ?? [] as $role) {
$role->save();
}
}
/**
* Install this module's default configuration.
*/
function entity_bundle_permissions_update_10101() {
/** @var \Drupal\Core\Config\ConfigInstaller */
$config_installer = \Drupal::service('config.installer');
$config_installer->installDefaultConfig('module', 'entity_bundle_permissions');
}
