Solved

default_value not working for select element in hook_form_alter

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Read about how to approach blogging and about ways to do it right. Stand out from the crowd and let your knowledge be consumed by a large audience. This article aims to explain how your blog should look like,  the most important things to do while b…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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