0

Here is my situation.

I am using the latest version of Organic Groups on Drupal 7. Every user on my site is part of one group and one group only. Is there a way to make it so that when users are creating group content, the group they belong to is automatically pre-selected? Again, every user is only in one group (but there are numerous groups), so it's superfluous to force them to make a group selection whenever they are creating group content.

0
  1. You can use Field Permission module to not allow a user to see the group field on content type.
  2. Use hook_form_BASE_FORM_ID_alter() to set a default field value for group field based on current logged in user.

as a sample code (not tested)

function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form_state['node'];
  if (!isset($node->nid) || isset($node->is_new)) {
    global $user;
    $groups = og_get_groups_by_user($user);
      if(is_array($groups)) {
      // This needs to be evaluated to check proper mapping of what field expect to set a default default field value
      $form['group_field_name']['#default'] = $groups[0];  
      }
  }
}
  • Hmmmm, but right now, when creating the content type, the group options are None and whatever group the user is in. If I just hid the field using Field Permission, the user's group wouldn't end up getting selected... – Lukas Pleva Mar 15 '14 at 21:18
  • for now do not hide the field, keep it there till you fix second step to make, once you make sure you are able to set the default group then hide the field. If you are not finding the codes mentioned in second point, there is an alternate approach drupal.stackexchange.com/questions/16151/… – arpitr Mar 15 '14 at 21:42
  • if I went with your code approach, where would I put it? – Lukas Pleva Mar 17 '14 at 6:11
0

Entity Reference Prepopulate is the best way to do this without custom code like arpitr described. You would rewrite the links to look like

/node/add/article?og_group_ref=123

These links could be on the group's page or you may find a token to populate the gid automatically based on user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.