Problem/Motivation

Split from #2025089: Deprecate user_role_grant_permissions(), user_role_revoke_permissions() and user_role_change_permissions().

Proposed resolution

- user_roles() can often be replaced with Role::loadMultiple()
- user_role_names() can be replaced with array_map() within result of Role::loadMultiple()

Remaining tasks

- review/commit

User interface changes

no

API changes

user_roles() and user_role_names() are deprecated without replacement

Data model changes

no

Release notes snippet

Issue fork drupal-3348093

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Postponed » Active

There's no need to postpone this.

Ranjit1032002 made their first commit to this issue’s fork.

ranjit1032002’s picture

Status: Active » Needs review
StatusFileSize
new4.06 KB

Uploaded a patch for the issue mentioned, please review.
Thank You.

andypost’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +@deprecated, +Needs change record

needs to replace user_role_names() too and deprecate both functions https://www.drupal.org/about/core/policies/core-change-policies/drupal-d... and add test

Updated IS

andypost’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record

Created MR - replaced all usage and deprecated

Needs reviews and polishing

andypost’s picture

Issue tags: +PHPStan

PHPStan reports false positive - it has no idea that roles has string IDs

Running PHPStan on *all* files.
 ------ --------------------------------------------------- 
  Line   core/modules/user/src/Form/RoleSettingsForm.php    
 ------ --------------------------------------------------- 
  62     Cannot unset offset 'anonymous' on array<int,      
         Drupal\user\Entity\Role>.                          
  63     Cannot unset offset 'authenticated' on array<int,  
         Drupal\user\Entity\Role>.                          
 ------ --------------------------------------------------- 

 ------ ----------------------------------------------------- 
  Line   core/modules/user/src/Plugin/views/filter/Roles.php  
 ------ ----------------------------------------------------- 
  57     Cannot unset offset 'anonymous' on array<int,        
         Drupal\user\Entity\Role>.                            
  58     Cannot unset offset 'authenticated' on array<int,    
         Drupal\user\Entity\Role>.                            
 ------ ----------------------------------------------------- 

 [ERROR] Found 4 errors   
andypost’s picture

-    $roles = $this->roleStorage->loadMultiple();
+    $roles = Role::loadMultiple();
...
     $admin_roles = $this->roleStorage->getQuery()

Replaced roleStorage with static loadMultiple() allowed to pass PHPStan but it needs follow-up to get where it took integer type for IDs

andypost’s picture

alexpott’s picture

Re #9 add to the baseline rather than make incorrect changes for PHPStan. PHPStan should not decide correctness when it is wrong.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Per #11

andypost’s picture

Status: Needs work » Needs review

Updated baseline and reverted to injected storage

Meantime update of phpstan's baseline removed one false-positive

diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon
index 4b0b775af4..ae976aebef 100644
--- a/core/phpstan-baseline.neon
+++ b/core/phpstan-baseline.neon
@@ -880,11 +880,6 @@ parameters:
                        count: 3
                        path: modules/block_content/tests/src/Kernel/Migrate/d6/MigrateCustomBlockContentTranslationTest.php
 
-               -
-                       message: "#^Variable \\$book_links in empty\\(\\) is never defined\\.$#"
-                       count: 1
-                       path: modules/book/book.module
-
                -
                        message: "#^Variable \\$callable in empty\\(\\) always exists and is not falsy\\.$#"
                        count: 1

I did not commit guessing other issue

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs change record updates

If we can update the IS and CR similar to #3348092: Deprecate user_role_permissions() please

ameymudras made their first commit to this issue’s fork.

andypost’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs change record updates

Unrelated failure, updated IS and CR

andypost’s picture

Issue tags: +Security

There's 3 threads are open in MR and it needs addressing, maybe in follow-up

Basically some places using to call HTML::escape() before using list of names for checkboxes but never for radios form elements. Looking at code I think we can remove escaping as element doing that at render time.

$roles = array_map(['\Drupal\Component\Utility\Html', 'escape'], $roles);

anchal_gupta’s picture

StatusFileSize
new26.56 KB
new3.18 KB

I have uploaded the patch.
Address #17 comment

smustgrave’s picture

@Anchal_gupta this issue is being worked on in the MR not patches. Hiding patch 18 and updating credit.

smustgrave’s picture

Status: Needs review » Needs work

Actually NW per #17

There's 3 threads are open in MR and it needs addressing, maybe in follow-up

andypost’s picture

Status: Needs work » Needs review

Removed escaping with extra commit and rebased

Also filed follow-up to remove 2 extra escaping #3352972: Stop calling Html::escape() for checkbox options

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Applied MR and did searches for user_role_names() and user_role() and all instances, minus trigger_error section, have been addressed.

IS was recently updated so think that's fine.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

For the comment I just made on the MR.

murilohp made their first commit to this issue’s fork.

murilohp’s picture

Status: Needs work » Needs review

Just trying to move this forward, updated the code and fixed #23, moving back to NR

smustgrave’s picture

Status: Needs review » Needs work

quick look into the build I don't immediately see the why but seems there were some failure in the last commit. Needs investigating.

andypost’s picture

Status: Needs work » Needs review

rebased, squashed 2 commits from murilohp and fixed it by filtering roles by permission first and then returning labels

elber made their first commit to this issue’s fork.

elber’s picture

Hi sorry @andypost I didn't see your comment before, I just did the rebase, I reverted the commit "initial-default" because it pushed non-related changes.

andypost’s picture

@elber you can just use git rebase -i HEAD~2 and drop useless commits as they will confuse reviewers

andypost’s picture

Removed this commits and rebased again (the failed test CKEditor5AllowedTagsTest is known as failing randomly)

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Point #23 appears to have been addressed

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Posted another round of review on the MR

andypost’s picture

Status: Needs work » Needs review

Thank you! fixed both points and reverted removal of Html::escape() for #3352972: Stop calling Html::escape() for checkbox options

smustgrave’s picture

Status: Needs review » Postponed

So there is a build failure, I believe because #3352972: Stop calling Html::escape() for checkbox options needs to land first. So temporarily postponing this until that lands.

andypost’s picture

Status: Postponed » Needs review

No, it was typo - closing ) in wrong place, I'm sure the #3352972: Stop calling Html::escape() for checkbox options should go second at beta stage

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Gotcha. In that case lets move it forward.

catch’s picture

Status: Reviewed & tested by the community » Needs review

Left a couple of comments on the MR.

alexpott’s picture

Status: Needs review » Needs work

Unfortunately the deprecation message needs a bit of work as Use to inline implementation instead. doesn't make much sense. I think it should be something like:

@trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal\user\Entity\Role::loadMultiple() and, if necessary, an inline implementation instead. See https://www.drupal.org/node/3349759', E_USER_DEPRECATED);

Note this needs fixing for both user_role_names() and user_roles().

@catch there's a follow-up for the escaping inconsistency - see #3352972: Stop calling Html::escape() for checkbox options

andypost’s picture

Status: Needs work » Needs review

@alexpott thanks, changed message to suggested, and rebased

@catch user_role_names() doing this array_map() but having wrapper function for single function call sounds bad idea. Moreover, places where we need labels from array of entities are not a lot but could be a global helper (follow-up?)

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Lets see if we can get this in for 10.1

Seems #39 has been addressed.

And #40

places where we need labels from array of entities are not a lot but could be a global helper (follow-up?)

Think that should be a follow up as seems out of scope for this particular issue.

I can open that up if we all agree.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Needs a re-roll for phpstan-baseline.neon (and also the deprecation version although that's also easy to fix on commit).

andypost’s picture

Status: Needs work » Needs review

Rebased and updated deprecation message

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Reroll looks good!

  • catch committed 26e376b1 on 11.x
    Issue #3348093 by andypost, murilohp, elber, Ranjit1032002, ameymudras,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

OK let's go ahead without the helper method and rely on array_map(). Guessing we have quite a lot of usages in core compared to contrib. Also thanks for pointing me to the existing checkboxes issue.

Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.