Solved

default_value not working for select element in hook_form_alter

Posted on 2010-09-20
2
444 Views
Last Modified: 2012-05-10
Hi, I'm working on a project and in it, there's a custom module that modifies a form with hook_form_alter.  It changes a textbox into a select field.  That works well ...

The problem is once a value is selected in the list, it reloads the page, showing only the content matching the value selected but the chosen value is not selected in the list ... it's blank.  I want the chosen value to be selected when the page reloads ...

The $chosenNeighbourhood variable contains the right value (example 355) when the page reloads but apparently the '#default_value' property doesn't work ... so I read a bit and stumbled upon this : http://drupal.org/node/726282

I tried that in the hook_after_build but it's still not working ... I must have a syntax error.  Does anyone have an idea where I'm wrong ?

Thanks !
function sfl_form_alter (&$form, $form_state, $form_id)
{
    if (arg (3) == NULL)
    {
	$chosenNeighbourhood = $_GET['nid'];
    }
    else 
    {
	$chosenNeighbourhood = "";
    }
...
    // the $neighbourhoods variable is an array containing (354 => "blabla", 355 => "blabla", etc ...).
    unset ($form['nid']);
    // Override the existing textfield form with a dropdown box
    $form['nid']= array(
        '#type' => 'select',
	'#options' => $neighbourhoods,
	'#title' => t("Filter by neighbourhood"),
	'#attributes' => array('onchange' => 'this.form.submit();'),
	'#default_value' => $chosenNeighbourhood,
	'#weight' => 2);
}

function sfl_after_build($form, &$form_state)
{
	if ($form['#id'] == 'views-exposed-form-quartier-search-block-page-1')
	{
		if (arg (3) == NULL)
		{
			$chosenNeighbourhood = $_GET['nid'];
		}
		else 
		{
			$chosenNeighbourhood = "";
		}
		
		//var_dump($form);
		
		$form['nid'][0]['#default_value']['value'] = $chosenNeighbourhood;
		$form['nid'][0]['value']['#default_value'] = $chosenNeighbourhood;
		$form['nid'][0]['value']['#value'] = $chosenNeighbourhood;
  		$form_state['values']['nid'][0]['value'] = $chosenNeighbourhood;
		
		return $form;
		
	}
}

Open in new window

0
Comment
Question by:a_godin
2 Comments
 
LVL 17

Expert Comment

by:Thomas4019
ID: 33721278
I can't see what's going on here, but I know that using the Devel module and it's dpm() function will make debugging easier. The dpm function's parameters are the same as var_dump.
0
 

Accepted Solution

by:
a_godin earned 0 total points
ID: 33726777
Problem solved ... thank you for suggesting the Devel module.  I had it installed but not activated.

Here's what was happening.  Once an item was selected in the list and I was redirected to the right page, the content of dpm($form) would appear twice ... the first time, #default_value had the correct value but not the second time.

So I replaced $chosenNeighbourhood with variable_set('chosen_variable', $_GET['nid']) because I thought the value of $chosenNeighbourhood was erased when the page was redirected.

After I did that, each time the content of dpm($form) would appear, #default_value had the correct value but the list still didn't keep the selected value when the page reloaded.

I had read somewhere that for a 'select' element, it was the '#value' property that represented the selected value ... I tried it and it worked.

Here's a portion of my working code.
function sfl_form_alter (&$form, $form_state, $form_id)
{
    if ($form['#id'] == 'views-exposed-form-quartier-search-block-page-1')
    {
        if (arg (3) == NULL)
        {
	    variable_set('chosen_neighbourhood', $_GET['nid']);
	}

        unset ($form['nid']);
	// Override the existing textfield form with a dropdown box
	$form['nid']= array(
		'#type' => 'select',
		'#options' => $neighbourhoods,
		'#title' => t("Filter by neighbourhood'),
		'#attributes' => array('onchange' => 'this.form.submit();'),
		'#default_value' => variable_get('chosen_neighbourhood', ""),
		'#value' => variable_get('chosen_neighbourhood', ""),
		'#weight' => 2);
    }
}

function sfl_after_build($form, &$form_state)
{
	if ($form['#id'] == 'views-exposed-form-quartier-search-block-page-1')
	{
		$form['nid'][0]['#default_value']['value'] = variable_get('chosen_neighbourhood', "");
		$form['nid'][0]['value']['#default_value'] = variable_get('chosen_neighbourhood', "");
		$form['nid'][0]['#value']['value'] = variable_get('chosen_neighbourhood', "");	
		$form['nid'][0]['value']['#value'] = variable_get('chosen_neighbourhood', "");
  		$form_state['values']['nid'][0]['value'] = variable_get('chosen_neighbourhood', "");
		
		return $form;
		
	}
}

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Documentum 6.7 - Password Encryption 5 319
Drupal 7: Include a referenz to webfont CSS in HTML head 7 156
Create a simple login Form in Drupal 1 368
Change size 15 41
RTL (right to left) web applications aiming for audiences speaking languages like Hebrew or Arabic, are generally more complicated than the same applications aiming for audiences speaking Latin based languages. The main difference lies of course …
In this article, we will look at our content management system as a whole, to get an idea of how we will start to manage these content items.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now