When viewing the add or edit node page, the Group Audience widget shows all groups to which the user is a member. When the user attempts to post the node to a group to which they are a member, but *do*not* have create permission, they receive an error. The expected behavior is for the widget to only show groups that to which the user is a member and has create permission for that node type.
I've written the following function that adds this feature. I'm new to this, so I'm sorry it's not a proper patch!
function og_create_perms_form_alter(&$form, &$form_state, $form_id) {
if(!isset($form['group_audience']))
return;
$content_gids = $form['group_audience']['und']['#audience']['content groups'];
$other_gids = $form['group_audience']['und']['#audience']['other groups'];
$node_type = $form['#node']->type;
$new_content_gids = array();
$new_other_gids = array();
foreach($content_gids as $gid => $group_name) {
// check to see if user has create permission for this node type
if(og_is_member($gid, 'user') && og_user_access($gid, 'create ' . $node_type . ' content')){
$new_content_gids[$gid] = $group_name;
}
else{
unset($form['group_audience']['und']['#options'][$gid]);
}
}
foreach($other_gids as $gid => $group_name) {
// check to see if user has create permission for this node type
if(og_is_member($gid, 'user') && og_user_access($gid, 'create ' . $node_type . ' content')){
$new_other_gids[$gid] = $group_name;
}
else{
unset($form['group_audience']['und']['#options'][$gid]);
}
}
// Not sure if this is necessary since it's a hidden field
$form['group_audience']['und']['#audience']['content groups'] = $new_content_gids;
$form['group_audience']['und']['#audience']['other groups'] = $new_other_gids;
}
Comments
Comment #1
the greenman commentedThanks very much for the code.
I am not immediately able to test this (developing other projects right now), but I can get a report or two that it is good, I will include it in the module.
Cheers
Peter
Comment #2
bjlewis2 commentedThis would dramatically improve the usability of every site using OG Create Permissions!
Where do I put the code so that I can test it?
Comment #3
Taxoman commentedSubscribing
Comment #4
Frederic wbase commentedbump
Comment #5
eglobal commentedsubscribing
Comment #6
bjlewis2 commentedI pasted the code from the original post to the end of og_create_perms.module and it works great!
Please try this yourself to make sure it is working in a variety of situations!
Comment #7
eglobal commentedI also posted at the end of og_create_perms.module. Works great for me too.
Comment #8
bjlewis2 commentedI tried rolling a patch for this and it found a couple of trailing white spaces. After removing the trailing white spaces, this is the code.
Then when I tried to roll a patch, this is the only error that is left:
Unfortunately I'm not a coder, so... any help?
Here's the patch for anyone who might be able to help.
Comment #9
bjlewis2 commentedUntil something can be done about this, I've made a module (Og create widget modifier) that implements the code above. Install the module in a sites/all/modules/custom folder, (so that og_create_widget_modifier.module is at sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module) and you can enable the module like normal (it'll be listed with the other Organic Groups modules). When you do, this functionality will be in place.
I figured this would be the best way to do it until it can be further tested, since it doesn't modify the original og_create_perms files. This way when the code is included in the official module, you can just disable and delete this extra module.
Comment #10
bjlewis2 commentedIs anyone available to review this code? I feel like it 99% there, and would make for a great 7.x-1.1 release!
Comment #11
bjlewis2 commentedThe following applies after updating to OG 7.x-1.1
Just realized that I get the following notices when I log in as a non-admin user and go to the edit tab of my user account.
I only get the following when I log in as the admin user, and go to the edit tab of my user account:
Edit: I also get this when creating a new user.
I think this could be fixed by adding an if-then statement to check whether the page is a "create content" page or not. But, like I said before, I'm not a coder, and don't know the proper way to do this.
Comment #12
sodapopbob commentedSub. This seems like a needed addition to the current code.
Has anyone had any luck with it so far?
Comment #13
Moosefish commentedTested the original code and it works well for Authenticated Users, no errors.edit: received #11 errors as User 1 when creating new users.The Groups that the user does not have permission to post content to do not display. The Administrator can still see all groups and when attempting to save to unauthorized group get the message 'Error message Attempt to post to an illegal group'. This would be a perfect addition to a future release.
Comment #14
mrfelton commentedUpdated the patch in #8 to fix formatting issues and resolve several undefined index warning like those mentioned in #11.
Comment #15
Moosefish commentedTested Patch in #14, working without errors, removed errors from created by original patch.
Comment #16
jide commentedThis should definitely use hook_og_audience_options_alter()... but this hook does not provide any context about the form being used, so we can't know the node type from this hook implementation :( Of course, we could use the path, but it feels hacky (if the node form is elsewhere for example, it won't work...).
Comment #17
jide commentedHere is a patch. It will not alter the audience widget if the node form is not used from node/add/[type] or node/[nid]/edit.
Comment #18
arnoldbird commentedBoth the #8 patch and #14 patch seem to work after a bit of very light testing.
#8 uses hook_form_alter()
#14 uses hook_og_audience_options_alter()
At first the latter strikes me as appropriate, since it uses a hook specifically made for altering this field's options. Although use of this hook is somewhat limiting since we don't have access to $form... and thus, the code inspects the URL to get the content type. As a result the patch in its present condition doesn't work with overlays, since overlays affect the URL. That is, if you are going to give non-admin users perms to see overlays, or you need the patch to work for admin.
#14 better handles situations where there is no group to select, presenting a friendly message. #8 presents an empty select field.
Comment #19
alesr commentedthe greenman: Can you commit this patch and enable the 7.x-1.x-dev release please.
P.S: This patch only limits groups widget. How to hide a link for creating a content that I permitted for specific role in block Content create links that is on a group page. Any patch for this too? Didn't find it, so I'm asking before I reinvent the wheel.
Comment #20
socialnicheguru commentedwould this work with OG 7.2?
Comment #21
ufok commentedI have problem when group member trying post article into his group. During creating article, his group is selected, but drupal wants to create article "outside a group"????
And after submit i see this error:
"You are not permitted to create article content outside a group"
I was trying configure it hundred times, even on fresh drupal instalations, and i see always this error.
What should i do? Help!
Comment #22
golddragon007 commented#21 it's a funny bug, I fixed now here: https://www.drupal.org/node/2455021
Comment #23
chidosoft commentedPls how do I add the function to my site?