I have a question about what will be considered the best practice in adding the "og_audience" widget (the input element that allows one to associate a group content to a group).

Currently I have used hook_element_info() to add an og_audience type however in the light of #639428: Remove #process pattern from text field I wonder if it's really the best way to go.

One case that I'm not sure how to tackle is in og_register module - where a registrating user may select groups to be subscribed to.


/**
 * Implement hook_form_FORM_ID_alter().
 *
 * Get all the groups that should appear in the user registration, and show them.
 */
function og_register_form_user_register_form_alter(&$form, $form_state) {
  if ($groups = og_register_get_groups()) {
    $form['og_register'] = array(
      '#type' => 'fieldset',
      '#title' => t('Groups'),
    );
    $form['og_register']['og_register_groups'] = array(
      '#type' => 'og_audience',
      '#title' => t('Subscribe to group'),
      '#includes' => array('content groups' => $groups),
      '#description' => t('Select the groups you wish to subscribe to.'),
    );

    // Add own submit handler.
    $form['#submit'][] = 'og_register_register_submit';
  }
}


/**
 * Submit handler for the registration form.
 */
function og_register_register_submit($form, &$form_state) {
  if (!empty($form_state['values']['og_audience']) && ($nids = array_filter($form_state['values']['og_audience']))) {
    $groups = array();
    foreach ($nids as $nid) {
      $groups[] = array('value' => $nid);
    }
    og_subscribe_user($groups);
  }
}

Not sure, maybe I can replace the above code with field_attach_form() -- although the $object (i.e. the user) doesn't exist yet.

And even If I'm able to, can anyone think about a secnario where the FAPI element will be needed and the field widget can't be used?

Comments

amitaibu’s picture

Status: Active » Patch (to be ported)

Talked with Moshe on IRC - and indeed moved logic to hook_widget().

amitaibu’s picture

Status: Patch (to be ported) » Fixed

Already in 7.x

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.