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

the greenman’s picture

Thanks 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

bjlewis2’s picture

This would dramatically improve the usability of every site using OG Create Permissions!

Where do I put the code so that I can test it?

Taxoman’s picture

Version: 7.x-1.0 » 7.x-1.x-dev

Subscribing

Frederic wbase’s picture

bump

eglobal’s picture

subscribing

bjlewis2’s picture

I 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!

eglobal’s picture

I also posted at the end of og_create_perms.module. Works great for me too.

bjlewis2’s picture

StatusFileSize
new1.67 KB

I 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.

/**
 * Implements hook_form_alter
 * Checks to see what content types a user can post, and to which groups
 * Then alters the groups audience field to only show the appropriate groups when creating content
 */

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;
}

Then when I tried to roll a patch, this is the only error that is left:

Brians-MacBook-Air:og_create_perms Brian$ git diff > groupswidget-1139180-8.patch
Brians-MacBook-Air:og_create_perms Brian$ git apply -v groupswidget-1139180-8.patch
Checking patch og_create_perms.module...
error: while searching for:
return $perms;
}

error: patch failed: og_create_perms.module:144
error: og_create_perms.module: patch does not apply

Unfortunately I'm not a coder, so... any help?

Here's the patch for anyone who might be able to help.

bjlewis2’s picture

StatusFileSize
new2.23 KB

Until 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.

bjlewis2’s picture

Status: Active » Needs review

Is anyone available to review this code? I feel like it 99% there, and would make for a great 7.x-1.1 release!

bjlewis2’s picture

The 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.

Notice: Undefined index: #audience in og_create_perms_form_alter() (line 12 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).
Notice: Undefined index: #audience in og_create_perms_form_alter() (line 13 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).
Notice: Undefined index: #node in og_create_perms_form_alter() (line 14 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).
Notice: Trying to get property of non-object in og_create_perms_form_alter() (line 14 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).
Warning: Invalid argument supplied for foreach() in og_create_perms_form_alter() (line 19 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).
Warning: Invalid argument supplied for foreach() in og_create_perms_form_alter() (line 29 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).

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.

Notice: Undefined index: #node in og_create_perms_form_alter() (line 14 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).
Notice: Trying to get property of non-object in og_create_perms_form_alter() (line 14 of /Users/Brian/Sites/d7/sites/all/modules/custom/og_create_widget_modifier/og_create_widget_modifier.module).

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.

sodapopbob’s picture

Sub. This seems like a needed addition to the current code.

Has anyone had any luck with it so far?

Moosefish’s picture

Tested 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.

mrfelton’s picture

Updated the patch in #8 to fix formatting issues and resolve several undefined index warning like those mentioned in #11.

Moosefish’s picture

Tested Patch in #14, working without errors, removed errors from created by original patch.

jide’s picture

This 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...).

jide’s picture

StatusFileSize
new1.16 KB

Here 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.

arnoldbird’s picture

Both 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.

alesr’s picture

the 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.

socialnicheguru’s picture

would this work with OG 7.2?

ufok’s picture

I 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!

golddragon007’s picture

#21 it's a funny bug, I fixed now here: https://www.drupal.org/node/2455021

chidosoft’s picture

Pls how do I add the function to my site?