You are here

function og_subgroups_add_group_select_form in Subgroups for Organic groups 6

Add form components to select group parents on the group node form

1 call to og_subgroups_add_group_select_form()
og_subgroups_form_alter in ./og_subgroups.module
Implenentation of hook_form_alter().

File

includes/form.inc, line 6

Code

function og_subgroups_add_group_select_form(&$form, $node) {

  // Check access
  if (!og_subgroups_can_edit_hierarchy($node)) {
    return;
  }

  // Determine the default parent ID to be used on the form
  if (!$node->nid) {

    // See if a default parent was included in the URL
    $pid = isset($_GET['og_parent']) && is_numeric($_GET['og_parent']) ? $_GET['og_parent'] : 0;
    if ($pid) {

      // Attempt to load the parent
      if ($parent = node_load($pid)) {

        // Check that it's a group
        if (og_is_group_type($parent->type)) {

          // Check that the user is a member of this grou
          if (og_is_group_member($parent->nid)) {

            // Set this parent as the current context
            og_set_group_context($parent);

            // Use this parent as the default parent in the form
            $parent_id = $parent->nid;
          }
        }
      }
    }
  }
  else {

    // Use the already chosen parent for this group
    $parent_id = isset($node->og_parent->nid) ? $node->og_parent->nid : 0;
  }

  // The subgroup selection title
  $title = t('Parent');

  // The subgroup selection description
  $description = t('The parent group for this group node.');
  $form['og_subgroups'] = array(
    '#type' => 'fieldset',
    '#title' => $title,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  if (module_exists('og_subgroups_hs')) {
    $form['og_subgroups']['og_parent'] = array(
      '#type' => 'hierarchical_select',
      '#title' => $title,
      '#size' => 1,
      '#config' => array(
        'module' => 'og_subgroups_hs',
        'params' => array(
          'nid' => isset($node->nid) ? $node->nid : NULL,
          'optional' => 0,
        ),
        'save_lineage' => 0,
        'enforce_deepest' => 0,
        'entity_count' => 0,
        'resizable' => 0,
      ),
      '#default_value' => $parent_id,
      '#description' => $description,
    );
  }
  else {
    $form['og_subgroups']['og_parent'] = array(
      '#type' => 'select',
      '#title' => $title,
      '#default_value' => $parent_id,
      '#options' => og_subgroups_group_select_options(TRUE),
      '#description' => $description,
    );
  }

  // Add our validator to the form
  array_unshift($form['#validate'], 'og_subgroups_node_form_validate');
}