Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

default_value not working for select element in hook_form_alter

Posted on 2010-09-20
2
Medium Priority
?
465 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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