Solved

default_value not working for select element in hook_form_alter

Posted on 2010-09-20
2
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

In this blog, I will share you some basic tips for content marketing and to rank your website on Google.
A big percent of today’s marketing activity is performed through the online environment. The marketing strategies that have existed a decade ago no longer relate to what’s happening today. We’re currently facing a revolutionary era, called the digit…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

617 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