This is frustrating as all manipulation of the role object must occur after the object has been saved to the database, which can result in unnecessary UPDATEs and INSERTs. I propose that we implement hook_user_role_presave() in a similar fashion to most other presave hooks.

If this was backported to D7 it would assist with implementing UUID in contrib.

Comments

skwashd’s picture

Status: Active » Needs review
StatusFileSize
new1.32 KB

Here is the patch which implements it.

catch’s picture

Category: feature » task
Issue tags: -backport +Needs backport to D7

Patch looks good, I'm not sure the hook should start with "Inform other modules", although that's from other comments in the same file so not for this patch to worry about.

This is just adding a hook that's missing (and it's silly to have insert/update hooks without presave), so it makes sense to me to backport it to D7.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

skwashd’s picture

@catch I agree the docs aren't great, but it is consistent with the language used for the other hooks which handle saving roles.

catch’s picture

Yep, we should clean up the docs in another issue all in one go, not here.

dave reid’s picture

Feels like this needs a few comment tweaks...

+++ b/modules/user/user.api.phpundefined
@@ -367,6 +367,23 @@ function hook_user_view_alter(&$build) {
 /**
+ * Inform other modules that a user role is about to be saved.
+ *

Should be "Act on a user role being inserted or updated." to fit with existing first lines of exiting hook_entity/THING_presave()s.

+++ b/modules/user/user.moduleundefined
@@ -2821,6 +2821,9 @@ function user_role_save($role) {
+
+  module_invoke_all('user_role_presave', $role);
+
   if (!empty($role->rid) && $role->name) {
     $status = drupal_write_record('role', $role, 'rid');
     module_invoke_all('user_role_update', $role);

Let's add a comment here (again consistency with our other THING_save() comments):
// Let modules modify the user role before it is saved to the database.

Powered by Dreditor.

catch’s picture

Status: Reviewed & tested by the community » Needs work

@Dave - the first one has already been discussed - the comments in user.api.php all need fixing, so we should open a separate issue to fix them in one go (or alternatively fix them all here but that's not really in scope).

I agree it's worth adding a comment by the module_invoke_all() though, so CNW for that at least.

skwashd’s picture

Status: Needs work » Needs review
StatusFileSize
new1.77 KB

Rerolled with additional comment as per @6

catch’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, let's do comment cleanup in another issue.

skwashd’s picture

dave reid’s picture

Yeah I know the other docs need cleanup, just not sure why we can't make that change here for this function since its new...

dries’s picture

Status: Reviewed & tested by the community » Needs work

The example is a bit silly. It would be helpful to have a helpful example. If not in the example code, maybe explain in the documentation _why_ this is a useful hook.

skwashd’s picture

Status: Needs work » Needs review
StatusFileSize
new1.43 KB

Rerolled

I agree the example is a bit silly. I was trying to find something generic to demonstrate it. The docs have been updated.

gordon’s picture

Status: Needs review » Reviewed & tested by the community

This patch looks fine, however I do agree with @skwashd in that using an example from uuid is too specific for an example.

However we do have issue #1120440 which is a clean up of user.api.inc so we should most likely revisit it there.

+1 RTBTC

dries’s picture

Category: task » feature
Status: Reviewed & tested by the community » Fixed

This looks good.

Committed to 7.x and 8.x.

Status: Fixed » Closed (fixed)
Issue tags: -Needs backport to D7, -UUID

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