Hey there,

I'm trying to force a group when a group node is created and then hide the group selection box from the user by doing #access=FALSE.. unfortunately, this results in the group not being set properly.. any idea why? or how to circumvent this?

Thanks a lot in advance!

Comments

amitaibu’s picture

Status: Active » Fixed

Make sure in your validate/ submit handler to re-populate the group audience field with the value you want -- form_set_value()

arski’s picture

Status: Fixed » Active

Hey, thanks for the quick reply!

Which field(s) would that be? I see in my validate handler that og_groups is set correctly, so the value is in there.. are there any additional fields that I need to re-fill?

And do I need to do this both in the validate AND submit handler, or is one enough?

Cheers

slowflyer’s picture

Have you found a solution?

If yes, how?

cheers?

joberg’s picture

Hi slowflyer,

I managed to do this yesterday, first to hide the audience fields you use this in your form_alter:


 $form['og_nodeapi']['visible']['og_groups']['#access']= False;

Then to force a group selection, in your $form['#submit'] function u can use something like this:

$form_item['#parents'] = array('og_groups', 'id_of_the_audience'); //For example, if we wanted to update $form_state['values']['one']['two'] to 'new value', we'd pass in $form_item['#parents'] = array('one', 'two').

form_set_value($form_item, $value, &$form_state);//$value = 'new value' 

$form_state['rebuild'] = TRUE;//rebuild the form state

For more information take a look at this, it helped me figure it out http://api.lullabot.com/form_set_value

Regards,
James

TwiiK’s picture

I had the same problem and tried like a billion times with unset() and #access, but everything resulted in the post being orphaned or other nasty errors because this is not some standard form item, but some kind of og black magic apparently. This was actually how I ended up doing it:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'group_post_node_form':
      $form['og_nodeapi']['visible']['og_groups']['#prefix'] = '<div style="display:none">';
      $form['og_nodeapi']['visible']['og_groups']['#suffix'] = '</div>';
      break;
  }
}

joberg's suggestion sounds like it could work, but I haven't used form_set_value before and since this has nothing to with security or otherwise I don't care if some html wizard user uses firebug to remove the css and moves his post. :)

General rant (sorry):
I hate how I have to battle with every drupal core or contrib module every time I create something for a client. This module has existed since the very beginning of drupal, why are such trivial features not in yet? I haven't been part of any community where the users themselves have the permission to move a post. This should be reserved for administrators and an optional feature. And the "show post in multiple groups"-feature should also be an optional feature.

And Views, grrrrr. I've usualy managed to stay away from that ghastly thing, but og seems to almost have Views as a dependency.

arski’s picture

Hey there,

totally agree with you on the point that this should be an option in the OG settings.

But Views.. only hardcore coders don't use Views - I mean why would someone want to go the long way around if there's a shortcut.. same reason you use a CMS like Drupal in the first place :)

TwiiK’s picture

I agree that me and my coworkers don't use Drupal as a CMS, but more like a framework and have started looking for something that's a more of a framework than Drupal, but I began as a designer and only started programming 2-3 years ago, but even I find it much easier to get down and dirty with the code than with views.

It's hard to find the correct views, it's hard to search views, grep'ing them doesn't work, adding additional features to views is a hassle if the functionality isn't already there, copying/pasting code is faster than exporting/importing views.

But this is off topic, and I apologize for the rant. I also don't contribute anything to the Drupal community apart from the occasional reply so I should perhaps keep my mouth shut. OG is a great set of modules and it sped up development of this site so I appreciate the effort put in by everyone. :)

hefox’s picture

Check out hook_nodeapi 'presave' op also

Alternativally, in hook_form_alter change the #type to 'value' and set '#value' to what '#default_value' of the element that has the value; (I think you're encountering the checkboxes bug).

arski’s picture

Category: support » feature

Hmm, but seriously, couldn't OG admins make this a setting, or a permission-based feature?

joberg’s picture

Hey Hefox,

Thanks for the heads up, im actually kind of new in Drupal and its been a long learning curve(still on it).....

What would the difference between presave and submit be?

Thanks!

danielmtv2’s picture

please, how can i hide the group audience and group type in the add node form but in D7
thanks

ezeedub’s picture

Category: feature » support

@hefox Thanks for the 'presave' tip! It helped me track down a nasty problem I've been having with hiding the group selection fieldset.

I've tried various things, including using form_set_value() as prescribed above, but I guess I'm not yet familiar enough with FAPI, and couldn't get it to work.

So, trying your suggestion, I'm setting #access = false on $form['og_nodeapi'] to hide the selection fieldset. And in hook_nodeapi/presave, I see something interesting: $node->og_groups[<group_nid>] = true, which down the road in the form processing gets interpreted as "1" and ends up changing the post's group from the correct to "1" (which I can verify by looking in the og_ancestry table).

Anyway, I can correct this problem in my presave like so

$group = og_get_group_context();
if (!empty($group)) {
  $node->og_groups[$group->nid] = $group->nid;
}

but I'd like to know why/how this value is being incorrectly stored as a boolean in the first place. Anyone have any ideas?

krembo’s picture

Hi, joberg.

I've tried to hide the field in the way you mentioined, but id didn't work... You use just this line in hook_form_alter or adding anything else? Do you change the weight of custom module too?

joberg’s picture

Hi, krembo.

Dont remember if the modules weight has anything to do, but I do have my custom modules with more weight than the rest so maybe it does affect. I use the utility module for this http://drupal.org/project/util

krembo’s picture

Yep! It was the weight of module. Thanks again.

capellic’s picture

Just to clarify the code in #4 above.

In your hook_form_alter function, do this for when the form ID matches the form in question.

$form['og_nodeapi']['#type'] = 'hidden';
$form['#submit'][] = 'yourmodulename_force_gid';

Then add this function in your existing module.

/**
 * Submit function that forces the gids for the hidden field.
 */
function yourmodulename_force_gid($form, &$form_state) {
  $form_item['#parents'] = array('og_groups');
  form_set_value($form_item, $form_state['values']['og_initial_groups'], &$form_state);
  $form_state['rebuild'] = TRUE;
}

And that works for me.

SunRhythms’s picture

Is there a way to create a custom content type on the fly at the point of group creation... That way when you create the group it has it's own associated content type with the necessary particular forms on creation for posts or threads? It should basically do the above which is forcing the group's name while hiding the form ... but this could control content creation so that it's more user friendly. So when a group user creates a group content you don't have to worry about selecting a group name it's already preselected and automated when the group is created. I know it sounds weird because groups and group contents are both custom content types. What I'm asking is we create the group's type of threads or post at the point of group creation automatically. The only problem with this is every group would have its own content type as well as their their posts. Maybe I can do a hook form alter to add this functionality to just one group content type and then as the list of groups and and group content grows it is just a better managed and organized application.any suggestions.

jdrichmond’s picture

Version: 6.x-2.1 » 6.x-2.x-dev

I had this same problem with 6.x-3.0-alpha3+13-dev. There isn't an id or unique style to hook into. I was forced to add the following line to the og.module. Yes, I know that hacking core is bad. It was the only way I could get it done. Tried the hook_form_alter() and was unable to alter the form to add anything into it.

[line 2179] $form['og_nodeapi']['#attributes'] = array('style' => 'display:none');

By the way, I love this module. Please add a class or id into this module so that developers can style or hide it if necessary. Thank you.

ramdhanh’s picture

i tried this, and voilaaaa.. nice one :-bd

$form['og_nodeapi']['#prefix'] = '<div style="display:none">';
$form['og_nodeapi']['#suffix'] = '</div>';