How to automatically fill the group audience field in Drupal Organic Groups.

Field Groups audience must be populated via URL

This one little phrase has ruined my Sunday. A private client of mine requested that I set up Drupal so that a user with an Editor role could select users with the role Contributor for the Editor's particular section of a website. So...I had a million ideas run through my head, until I had an epiphany: Organic Groups. I've never used organic groups before. I've only heard about it's existence and experienced a fully set up version using Open Atrium (which is possibly the finest open-source project management system there is!).

Little did I know the work that went into setting this beast up. Skipping all of the learning curve items, here's what you have to know when creating content inside a content type that is marked as "Group Content" and has the og_group_ref field set to read the OG context: you should probably click the "Add new content" link from within the group itself, rather than relying on the Content>Add New functionality once you've set up OG. This will allow OG to pass along the proper context via the URL and save you the headache of trying to figure out how to manually construct the URL. However, if you're hard headed (and I am), and need to set up the URL on your own, here's the syntax (as of Drupal 7.22 and OG 7.x-2.1):

node/add/[groups-controlled-content-type-machine-name]?[group_reference_field_name]=[group_nid]&destination=node/[group_nid]

So… let's assume the following:

  1. The machine name of the content type your editing is "blog",
  2. The group reference field name is "og_group_ref" (I don't know that you can change this, but...), and
  3. The group nid is "45"

Your final manually-constructed add node link would have the URL:

node/add/blog?og_group_ref=45&destination=node/45
organic groups
og