Part of meta-issue #2225347: [META] Replace calls to ENTITY_TYPE_load() and ENTITY_TYPE_load_multiple() with static method calls

Problem/Motivation

Replacing old API function calls with static method calls.

Proposed resolution

  • All occurences of user_role_load() and entity_load('user_role') need to be changed to Role::load()
  • All occurences of entity_load_multiple('user_role') need to be replaced with Role::loadMultiple()
  • If entity_load() or entity_load_multiple() were used with $reset = TRUE, reset the role cache with \Drupal::entityManager()->getStorage('user_role')->resetCache();

Remaining tasks

There is a patch in comment 21 that needs review
Create or update a change record related to this. Might have to be done on the meta issue?

Comments

temoor’s picture

Status: Active » Needs review
StatusFileSize
new14.6 KB
Michael Hodge Jr’s picture

Status: Needs review » Needs work

Not sure if this is accurate or not, but I applied the patch and grepped the code for these occurrences. Everything looks good except for finding one instance of entity_load_multiple('user_role').

It was located in the User.module file around line 1174. It's contained in the user_roles function. Is this a false positive? I'm assigning back to needs work, but if this should still be there, then feel free to set it to RTBC. The code for reference is below

/**
 * Retrieve an array of roles matching specified conditions.
 *
 * @param $membersonly
 *   Set this to TRUE to exclude the 'anonymous' role.
 * @param $permission
 *   A string containing a permission. If set, only roles containing that
 *   permission are returned.
 *
 * @return
 *   An associative array with the role id as the key and the role object as
 *   value.
 */
function user_roles($membersonly = FALSE, $permission = NULL) {
  $user_roles = &drupal_static(__FUNCTION__);

  // Do not cache roles for specific permissions. This data is not requested
  // frequently enough to justify the additional memory use.
  if (empty($permission)) {
    $cid = $membersonly ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
    if (isset($user_roles[$cid])) {
      return $user_roles[$cid];
    }
  }

  $roles = entity_load_multiple('user_role');
  if ($membersonly) {
    unset($roles[DRUPAL_ANONYMOUS_RID]);
  }

  if (!empty($permission)) {
    $roles = array_filter($roles, function ($role) use ($permission) {
      return $role->hasPermission($permission);
    });
  }

  if (empty($permission)) {
    $user_roles[$cid] = $roles;
  }

  return $roles;
}
temoor’s picture

Assigned: temoor » Unassigned
Status: Needs work » Needs review
StatusFileSize
new14.84 KB

Thanks, @Michael Hodge Jr.
Removed remaining call in attached patch.

Michael Hodge Jr’s picture

Status: Needs review » Reviewed & tested by the community

Much better. Thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: drupal8-entity_system-user-role-load-2322233-3.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: drupal8-entity_system-user-role-load-2322233-3.patch, failed testing.

ohthehugemanatee’s picture

Is this a duplicate of #2322195: Replace all instances of user_load(), user_load_multiple(), entity_load('user') and entity_load_multiple('user') with static method calls?

Edit: no it isn't, I just skimmed over the word "role" in the title. Feelin' clever.

temoor’s picture

Status: Needs work » Needs review
StatusFileSize
new14.84 KB
new415 bytes

Used User instead of Role class in user_role_revoke_permissions()

dawehner’s picture

We still use user_role_load in

./core/modules/user/src/RoleForm.php:41:        'exists' => 'user_role_load',

though you can easily replace this with ['\Drupal\user\Entity\Role', 'load']

temoor’s picture

Thanks, I should pay attention to d8 FAPI

Status: Needs review » Needs work

The last submitted patch, 11: drupal8-entity_system-user-role-load-2322233-11.patch, failed testing.

estoyausente’s picture

Issue tags: -
StatusFileSize
new15.4 KB

Rerolled

estoyausente’s picture

Status: Needs work » Needs review
Issue tags: +Amsterdam2014

Status: Needs review » Needs work

The last submitted patch, 14: drupal8-entity_system-user-role-load-2322233-14.patch, failed testing.

estoyausente’s picture

Assigned: Unassigned » estoyausente
Mirroar’s picture

We'll take a look at it, sprinting at DC Amsterdam.

Mirroar’s picture

Status: Needs work » Needs review
StatusFileSize
new15.64 KB

Okay, so the reason the previous patch failed testing is because within UserRoleAdminTest.php calls to entity_load('user_role', $id, TRUE) were replaced with Role::load($id). However, this does not reset the cache of loaded roles, unlike the old call.

We added the code from entity_load to reset the cache before loading the Roles again. Not sure if this is the best way to reset the entity cache, but it seems to be working.

estoyausente’s picture

Assigned: estoyausente » Unassigned

Hi, @Mirroar, thank for the help. I debugged it and I haven't found the fail (F*** cache grrr).

It seems that now is correct. I think that someone with more experience have to review it, but I think that it's correct anyway.

Mirroar’s picture

It seems the more widely used pattern here is something along the lines of \Drupal::entityManager()->getStorage('user_role')->resetCache(array($role->id())); (user.module does something along those lines, and the Meta Issue also suggests this pattern). So I updated the patch to do that instead. Also adding an interdiff to #14 so the changes are easier to review.

I've also run a grep for any of the functions mentioned in the issue title, and can't find any other references in the codebase. So as far as I know, we got all of them.

Mirroar’s picture

Issue summary: View changes

Updated the issue summary

Mirroar’s picture

Issue summary: View changes
mradcliffe’s picture

Issue summary: View changes
Issue tags: +Needs change record

The changes in the interdiff make sense.

Reviewing the code in the entire patch. There does not seem to be any changes that do not correspond to the meta issue. I think this is RTBC if it passes tests.

We also need to create or update a change record related to this. Is this done on the meta issue?

Mirroar’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Setting RTBC and updating the issue summary again. But I have no Idea how to create a change record for this, so this will need to be done by somebody else, probably.

Mirroar’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs change record

Committed 331f287 and pushed to 8.0.x. Thanks!

  • alexpott committed 331f287 on 8.0.x
    Issue #2322233 by Temoor, Mirroar, estoyausente: Replace all instances...

Status: Fixed » Closed (fixed)

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