Question

When people register for my site I want them to either join an existing group or create a new one if it doesn't exist. They can only be a member of one group, and there's only one group type used on the site.

I've got the autocomplete widget set and I want to alter it's functionality so instead of throwing an error message it creates a group. I presume altering og.module is not a good idea as it wouldn't be easy to make my particular changes generic enough to work for lots of use cases so am I right in thinking I would create a custom module to hook into the pre-save functionality of the user entity and do my checking to see if exists, if not then create group and create membership there or is there another way?

I tried using taxonomy as my group content and using a term reference autocomplete field on my user profile, but og doesn't seem to work fully with taxonomy at the moment, and I had other problems with attached field data not saving on taxonomy so gone back to using a node type as my group content type.

Was it helpful?

Solution 2

hook_user_insert() triggers too late in the process. I used hook_form_FORM_ID_alter() to change the autocomplete validate callback to use my custom function.

  function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  $form['group_audience']['und'][0]['gid']['#element_validate'][0] = 'MYMODULE_field_audience_autocomplete_validate';
}

MYMODULE_field_audience_autocomplete_validate() is same as the original function, except that, instead of throwing an error, it creates a group.

OTHER TIPS

I think your general premise is sound. I'd be tempted to use hook_user_insert() instead, which is called after the user has been created, rather than before, since:

  • Your module can't actually fail to create a group or insert a new one.
  • hook_user_presave is really for modules that want to serialize (simple) data to be stored in the users table itself, which isn't what you're doing.

The necessary hooks in OG7 are all there for checking and creating groups and inserting users.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top