How to get PHP drop downs to work on Drupal site

I’m trying to get a PHP drop down working on my drupal site, but not getting anywhere on Drupal help sites.

So I thought that maybe some guys in here might know the answer?

Might be easier if you look at this post on Drupal:

http://drupal.stackexchange.com/questions/167865/what-hook-form-alter-code-do-i-need-to-change-an-exposed-filter-from-a-text-fiel

[quote]

use if($form_id == “view id” && $form[‘#id’]==‘current view display’ ) and use options array as array(key => value) format… – Abin Aug 6 at 4:37

Thanks Abin, but not quite there yet! what should I put [EXACTLY] into array(key => value) , I tried many combinations and I can’t get it working, like array(1=>1) , array(“1”=>1) , array(“1”=>$1) … I have no idea about PHP…drop down is for ages 18-99 – Bruno Vincent 2 days ago [/quote]

Not sure if this is the bad way to ask on a forum…but I just can’t get any help on Drupal forums…so I thought I would give this one a shot.

I’m just learning PHP, very, very, very beginner.

Thanks

THanks

I’ve never coded for Drupal but in this particular case I guess you should provide options list for your select.
Keys of this array will be used as value attribute for each option and values of array will be used as option text.

If you want it to display ages, maybe it should looks like

$form['your field name']['#options'] = array(
    18 => 18,
    19 => 19,
    // ... 
    99 => 99
);

And of course you don’t have to put all these lines by hand, use loop to generate an array of ages:

for($age = 18; $age <= 99; $age++){ $agesList[$age] = $age; }
$form['your field name']['#options'] = $agesList;

Thanks Megazoid, this code works exept for an error:

“An illegal choice has been detected. Please contact the site administrator.”

How do I put these 2 sets of code together:

   function dropdowninteger_form_alter(&$form, &$form_state, $form_id) {

if ($form_id == “views_exposed_form” ){
$options = array(‘1’ , ‘2’);
$form[‘field_user_age_value’][‘#type’] =‘select’;
$form[‘field_user_age_value’][‘#size’] = null;
$form[‘field_user_age_value’][‘#default_value’] = ‘’;
$form[‘field_user_age_value’][‘#options’] = $options;
}
}

and this one you gave me:

for($age = 18; $age <= 99; $age++){ $agesList[$age] = $age; }
$form[‘your field name’][‘#options’] = $agesList;

replace

$form['field_user_age_value']['#options'] = $options;

with my 2 lines

Yep , the drop down is there, now :wink: But there is still this error:

An illegal choice has been detected. Please contact the site administrator.

Exact code is this:

<?php function dropdowninteger_form_alter(&$form, &$form_state, $form_id) { if ($form_id == "views_exposed_form" ){ $options = array('1' , '2'); $form['field_user_age_value']['#type'] ='select'; $form['field_user_age_value']['#size'] = null; $form['field_user_age_value']['#default_value'] = ''; for($age = 18; $age <= 99; $age++){ $agesList[$age] = $age; } $form['field_user_age_value']['#options'] = $agesList; } }

your field name

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.