Solved

default_value not working for select element in hook_form_alter

Posted on 2010-09-20
2
439 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

20 Experts available now in Live!

Get 1:1 Help Now