As you can see the array key [0] is used, so if you use that function to generate an $options array for '#options' => $options, the first array item will always be selected as the form api says, the first array key should not be used. Maybe this function could be optimized, so you can use it with #type 'checkboxes' too.

/**
 * Returns an array of possible categories, suitable for inclusion in FAPI.
 */
function userpoints_get_categories($account = NULL) {
  $cache = drupal_static(__FUNCTION__, array());
  $key = $account ? $account->uid : 0;
  if (!isset($cache[$key])) {
    // Create the "Uncategorized" category.
    $options = array();
    $options[0] = t('!Uncategorized', userpoints_translation());
    if (module_exists('taxonomy')) {
      $vid = userpoints_get_vid();
      if ($vid) {
        // If an account is passed, load the terms directly from the database.
        if ($account) {
          $query = db_select('taxonomy_term_data', 't')
            ->fields('t', array('tid', 'name'))
            ->condition('t.vid', userpoints_get_vid())
            ->groupBy('t.tid')
            ->groupBy('t.name')
            ->orderBy('t.weight');
          $query->join('userpoints_txn', 'p', 't.tid = p.tid AND p.uid = :uid', array(':uid' => $account->uid));
          $terms = $query->execute();
        }
        else {
          $terms = taxonomy_get_tree($vid);
        }
        foreach ($terms as $term) {
          $options[$term->tid] = $term->name;
        }
      }
    }
    $cache[$key] = $options;
  }
  return $cache[$key];
}

Comments

manuel.adan’s picture

Status: Active » Closed (outdated)

Closing old issues as outdated. Feel free to reopen if needed.