You are here

function og_vocab_form_alter in OG Vocabulary 6

Same name and namespace in other branches
  1. 5 og_vocab.module \og_vocab_form_alter()
  2. 7 og_vocab.module \og_vocab_form_alter()

Implementation of hook_form_alter().

File

./og_vocab.module, line 174
Give each group its own system controlled vocabularies.

Code

function og_vocab_form_alter(&$form, $form_state, $form_id) {
  if (strpos($form_id, '_node_form') && !empty($form['taxonomy'])) {

    // Remove from node form those vocabs that belong to groups other than
    // us (if we even have a group).
    $gids = array();

    // Check if we edit an existing group post.
    if (!empty($form['og_initial_groups'])) {
      $gids = $form['og_initial_groups']['#value'];
    }

    // Check if usre clicked on 'Preview' while selecting group(s).
    if (!empty($form_state['values']['og_groups'])) {
      $gids = array_filter($form_state['values']['og_groups']);
    }
    elseif ($groupnode = og_get_group_context()) {
      $gids[] = $groupnode->nid;
    }

    // If user is a member of only a single group and audience is required
    // then we pass the single group.
    if (!$gids && variable_get('og_audience_required', 0) == 1) {
      global $user;
      if (count($user->og_groups) == 1) {
        $gids[] = current($user->og_groups);
      }
    }
    if ($gids) {

      // Remove og_vocab vocabularies that aren't in the context.
      $placeholders = implode(', ', array_fill(0, count($gids), '%d'));
      $where = 'ov.nid NOT IN (' . $placeholders . ')';
    }
    else {

      // Remove all og_vocab vocabularies.
      $placeholders = '';
      $where = 'ov.vid = v.vid';
    }
    $sql = "SELECT v.vid, v.tags FROM {vocabulary} v LEFT JOIN {og_vocab} ov ON v.vid = ov.vid WHERE {$where}";
    $result = db_query($sql, $gids);
    while ($row = db_fetch_object($result)) {
      if ($row->tags) {
        unset($form['taxonomy']['tags'][$row->vid]);
      }
      else {
        unset($form['taxonomy'][$row->vid]);
      }
    }

    // Remove categories fieldset if no vocabularies remain.
    // First, unset tags if needed.
    if (!count($form['taxonomy']['tags'])) {
      unset($form['taxonomy']['tags']);
    }
    if (!count(element_children($form['taxonomy']))) {
      unset($form['taxonomy']);
    }
    else {

      // Make themeing the fieldst easier.
      $form['taxonomy']['#attributes'] = array(
        'class' => 'og-vocab-taxonomy',
      );
    }
  }
  elseif ($form_id == 'search_form') {

    // We can't use hook_form_FORM-ID_alter, as the 'advanced' part is called
    // later.
    og_vocab_form_search_alter($form);
  }
}