The docs state:
> where the key holds the permission name and the value is an integer or boolean that determines whether to grant or revoke the permission:
However, you can pass a value that is a string rather than an integer, and in fact in form values array of checkboxes, the value of each selected checkbox will be equal to the key, ie
foo => foo, // this is selected
bar => 0, // this is not.
For example, this is what user_admin_permissions_submit() does this.
This needs to be stated explicitly, otherwise module authors may be thinking they have to run through their form values to convert the strings to 1s.
Comment | File | Size | Author |
---|---|---|---|
#6 | 923850-user_role_change_permissions.patch | 1.9 KB | mr.baileys |
#4 | 923850-4.drupal.user_role_change_permissions.patch | 1.89 KB | joachim |
#3 | 923850-user_role_change_permissions.patch | 1.88 KB | mr.baileys |
Comments
Comment #1
mr.baileysFrankly, I'd be more in favor of changing the implementation of user_role_change_permissions to match the docs.
Using the current implementation, any value that evaluates to true will grant the permission when run through user_role_change_permissions:
Comment #2
joachim CreditAttribution: joachim commentedI see your point... but the point of this API function is that you can feed it the values FormAPI gets back for a set of checkboxes.
If we change the function as you describe, a bunch of core forms need to do the extra work of massaging their arrays.
Comment #3
mr.baileysI see your point too. I dislike the fact that API functions are concerned about forms stuff, but the docs do mention "…the submitted values may be directly passed on in a form submit handler.".
Patch attached which documents the $permissions parameter more in detail.
Comment #4
joachim CreditAttribution: joachim commentedRerolled for whitespace and formatting.
I also meant to change the use of user_role_change_permissions to have a () after it, but 'this function' given that's what it is seems to read better to me.
Comment #5
jhodgdonLooks pretty close.
to granted -> to be granted. Also I would like these two sentences to use parallel structure -- both saying "any value" or both saying "values", since that other change needs to be made too. Also, in Drupal we use TRUE/FALSE, not true/false, I think?
I'm also not sure why you want to include bad code in the examples:
I think we should only demonstrate 0 and 1, TRUE and FALSE, and we should always use FALSE instead of false. Giving examples of poor code like -1 and 'access comments' is not really illuminating. PHP programmers know what "evaluates to true" means.
Comment #6
mr.baileysThanks for the review jhodgdon,
I've taken care of the issues you mentioned, although I did leave
'access comments' => 'access comments'
in there: sinceuser_role_change_permissions()
can be used in a form submit handler, this is actually a valid use and should not be considered a bad example (core uses it in this manner).Comment #7
jhodgdonOK, looks good.
Comment #8
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.