0

How would I go about adding a dropdown list to the user registration form? Also, is there a limit as to how many dropdown items you can have?

3 Answers 3

2

You can do this via the UI by going to (admin/config/people/accounts/fields) and then adding your new field. There is no limit to how many fields you can have.

1
  • 1
    There is a performance limit to the number of fields you can have, but unless you expect to have massive amounts of traffic this isn't something you need to worry about. Jun 13, 2013 at 5:27
0

One way of adding a dropdown list has already been stated. However, if you wish to add a dropdown of taxonomy terms, you can incorporate the following piece of code:

//get the list of locations from taxonomy to use in the dropdown
$dropdown_source = taxonomy_get_tree(2);
$dropdown_array = array('0' => '--none--');
foreach ($dropdown_source as $item) {
$key = $item->tid;
$value = $item->name;
$dropdown_array[$key] = $value;
}

//location filter dropdown
$form['filterset']['locationfilter'] = array(
  '#weight' => '1',
  '#key_type' => 'associative',
  '#multiple_toggle' => '1',
  '#type' => 'select',
  '#options' => $dropdown_array,
  '#title' => 'Filter by location',
);

unset($dropdown_array);

It will also rule out problems on encountering non-relevant taxonomy terms.

You may also check out taxonomy_form

1
  • Rakhi, would you mind telling where to incorporate that code?
    – Mołot
    Jun 13, 2013 at 7:44
0

//Here is the function for getting list dynamically from database(here i uses taxonomy). In this function '$vocabularyName' as parameter is taxonomy_machine_name, which we created for our project then query for getting the id from taxonomy_term_data (drupal default table) of above name, and fetching the data from drupal default table taxonomy_vocabulary.

    function getList($vocabularyName) {
      $query = db_select('taxonomy_term_data', 'f');
      $query->innerjoin('taxonomy_vocabulary', 'b', 'b.vid = f.vid');
      $query->fields('f', array('name', 'tid'))
            ->condition('b.machine_name', $vocabularyName);
      $result = $query->execute()->fetchAll();

      $keyarray = array();

      foreach ($result as $key => $value) {
      $keyarray[$value->tid] = $value->name;
     }

     $options = $keyarray;
     return $options;
   }

//code for hook_form()

$form['city'] = array(
'#type' => 'select',
'#title' => t('City'),
'#options' => getList('taxomomy_machine_name'),
);

Note : Answer with the consideration of drupal7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.