From 78a2640ab4683432057d69e44dca67fc358b1613 Mon Sep 17 00:00:00 2001 From: Kevin Wenger Date: Thu, 25 Apr 2024 11:34:33 +0200 Subject: [PATCH] replace usage of user_role_names() deprecated in 10.2 and removed in 11.0 --- CHANGELOG.md | 3 +++ template_whisperer.install | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06d0bd6..2205a55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - add cpsell project words for Gitlab-CI +### Changed +- replace usage of user_role_names() deprecated in 10.2 and removed in 11.0 + ## [4.0.1] - 2024-03-01 ### Changed - re-enable PHPUnit Symfony Deprecation notice diff --git a/template_whisperer.install b/template_whisperer.install index b8bdfad..8496c29 100644 --- a/template_whisperer.install +++ b/template_whisperer.install @@ -6,6 +6,8 @@ */ use Drupal\Core\Url; +use Drupal\user\Entity\Role; +use Drupal\user\RoleInterface; /** * Implements hook_install(). @@ -82,10 +84,19 @@ function template_whisperer_update_8001(&$sandbox) { $roleStorage = \Drupal::service('entity_type.manager') ->getStorage('user_role'); + $roles = Role::loadMultiple(); + unset($roles[RoleInterface::ANONYMOUS_ID]); + // Get all roles with `administer template whisperer` permissions. - $roles_with_tw_administer = user_role_names(TRUE, 'administer template whisperer'); + $roles_with_tw_administer = array_filter($roles, function ($role) { + return $role->hasPermission('administer template whisperer'); + }); + $roles_with_tw_administer = array_map(fn(RoleInterface $role) => $role->label(), $roles_with_tw_administer); // Get all roles with `administer template whisperer suggestion entities. - $roles_with_tw_suggestion = user_role_names(TRUE, 'administer template whisperer suggestion entities'); + $roles_with_tw_suggestion = array_filter($roles, function ($role) { + return $role->hasPermission('administer template whisperer suggestion entities'); + }); + $roles_with_tw_suggestion = array_map(fn(RoleInterface $role) => $role->label(), $roles_with_tw_suggestion); $rids = array_merge($roles_with_tw_administer, $roles_with_tw_suggestion); $rids = array_keys($rids);