You are here

function og_form_group_manager_validate in Organic groups 7.2

Validate handler; Make sure a group can be created.

We check if the group manager has a matching group-audience field for the OG membership to be created in.

1 string reference to 'og_form_group_manager_validate'
og_field_attach_form in ./og.module
Implements hook_field_attach_form().

File

./og.module, line 789
Enable users to create and manage groups with roles and permissions.

Code

function og_form_group_manager_validate($form, &$form_state) {
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  if (empty($form_state[$entity_type])) {

    // We are inside field settings page.
    return;
  }
  $entity = $form_state[$entity_type];
  $langcode = isset($form_state['values']['language']) ? $form_state['values']['language'] : LANGUAGE_NONE;
  if (!isset($form_state['values']['uid']) || !isset($entity->uid)) {

    // There is no user ID property on the entity.
    return;
  }
  if (isset($form_state['values'][OG_GROUP_FIELD]) && empty($form_state['values'][OG_GROUP_FIELD][$langcode][0]['value'])) {

    // Not a group.
    return;
  }
  if (!isset($form_state['values'][OG_GROUP_FIELD])) {

    // Field doesn't appear in the form, so it is probably hidden by
    // hook_field_access(). So check the default value of the field.
    $field = field_info_field(OG_GROUP_FIELD);
    $instance = field_info_instance($entity_type, OG_GROUP_FIELD, $bundle);
    $items = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
    if (empty($items[0]['value'])) {

      // Default value is not a group.
      return;
    }
  }
  if ($entity_type == 'node') {

    // A user might assign the node author by entering a user name in the
    // node form, which we then need to translate to a user ID.
    // However, this happens later on, in node_submit(), so we do a special
    // check for the node entity.
    if (!($account = user_load_by_name($form_state['values']['name']))) {

      // Invalid username.
      return;
    }
  }
  else {
    $account = user_load($form_state['values']['uid']);
  }
  list($id) = entity_extract_ids($entity_type, $entity);
  if ($id && $entity->uid == $account->uid) {

    // The entity's user ID hasn't changed.
    return;
  }
  if ($access = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) {

    // Matching group audience field found.
    return;
  }
  form_error($form, t("Can't save entity as group, because user @name can't be subscribed to group and become a manager.", array(
    '@name' => format_username($account),
  )));
}