Similar to http://drupal.org/node/516684 I'm looking for some help using hook form altar.

I created my own module, called views_price, using the code in the link that I modified for my own use. I called this: views_price.module

<?php
// $Id: views_price.module,v 1.22 2011/04/02 19:53:27 sun Exp $

/**
* Alter the form to display dropdowns for price range
*/
function mainland_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form' :
      $form['submit']['#value'] = t('Search');
      $from_options = array(
        '' => t('Any'),
     '5000' => t('5,000RMB'),
      '10000' => t('10,000 RMB'),
      '15000' => t('15,000 RMB'),
      '20000' => t('20,000 RMB'),
      '30000' => t('30,000 RMB'),
      '40000' => t('40,000 RMB'),
      '50000' => t('50,000 RMB'),
      '100000' => t('100,000 RMB'),
      );
    $to_options = array(
        '' => t('Any'),
     '5000' => t('5,000RMB'),
      '10000' => t('10,000 RMB'),
      '15000' => t('15,000 RMB'),
      '20000' => t('20,000 RMB'),
      '30000' => t('30,000 RMB'),
      '40000' => t('40,000 RMB'),
      '50000' => t('50,000 RMB'),
      '100000' => t('100,000 RMB'),
       '100000' => t('100,000 RMB plus'),
      );
      $field_propprice_value_min = array (
        '#type' => 'select',
        '#multiple' => false,
        '#required' => false,
        '#options' => $from_options,
        '#default_value' => 'Any',
      );      
      $field_propprice_value_max = array (
        '#type' => 'select',
        '#multiple' => false,
        '#required' => false,
        '#options' => $to_options,
        '#default_value' => '$1,000,000 plus',
      );    
      $form['field_propprice_value']['min'] = $field_propprice_value_min;
      $form['field_propprice_value']['max'] = $field_propprice_value_max;
      $form['field_propprice_value']['min']['#title'] = t('from');
      $form['field_propprice_value']['max']['#title'] = t('to');
      break;
  }

}

http://terascape.net/images/min.jpg - views settings (max is same, except "less than" & _max
http://terascape.net/images/search.jpg the search box
http://d20010.u35.terascape.net the dev site

Trouble is, it won't show, and I don't see what I'm missing. This is my FIRST attempt at this, so could use a little feedback. And once I get this down, I need to copy all this to a new views_bed.module for number of bedrooms.

Comments

Seneschall’s picture

http://drupal.org/node/1113842 Or is a views THEME a better way to approach this?

msathesh’s picture

Hey skyline5k.. did you make it work? I can make the form with max and min to display (I think you don't need to have the max and min filters in your views filters section). But after I press the search button it's actually posting the values such as this: '/?&min=all&max=all'. Do you know where to post the values actually and get the results?

msathesh’s picture

@skyline5k sorry about my last post. I am facing the same issue. If I expose the price field in filters, the dynamic select elements (min & max) gets vanished for some reason. If I remove the exposed price field in filters I can get the select elements in the form (but it doesn't actually do any filtering work since it's not connected to the view filter itself!). Please do let me know if you solve the issue and I will let you know if I find a solution to the problem. Thanks.

msathesh’s picture

@skyline5k

I have just changed the $form variable from $form['field_propprice_value']['min'] and $form['field_propprice_value']['max']to $form['field_price_range_min'] and $form['field_price_range_max'] respectively and it worked. Otherwise, everything are same i.e. exposed price field filter withing the views for min and max. Then this code with just a minor change. And it works very well. But how did you handle the max price value in your views for '100,000 RMB plus'?

See the following code to see the difference:

    case 'views_exposed_form' :
      $form['submit']['#value'] = t('Search');
      $from_options = array(
     '0' => t('Min. Price'),
     '100000' => t('$100,000'),
	 '150000' => t('$150,000'),
	 '200000' => t('$200,000'),
	 '250000' => t('$250,000'),
	 '300000' => t('$300,000'),
	 '350000' => t('$350,000'),
	 '400000' => t('$400,000'),
	 '450000' => t('$450,000'),
	 '500000' => t('$500,000'),
      );
    $to_options = array(
	 '150000' => t('$150,000'),
	 '200000' => t('$200,000'),
	 '250000' => t('$250,000'),
	 '300000' => t('$300,000'),
	 '350000' => t('$350,000'),
	 '400000' => t('$400,000'),
	 '450000' => t('$450,000'),
	 '500000' => t('$500,000'),
	 '550000' => t('$550,000'),
	 '600000' => t('$600,000'),
      );
      $form['field_price_range_min'] = array (
        '#title' => 'From',
        '#type' => 'select',
        '#multiple' => false,
        '#required' => true,
        '#options' => $from_options,
        '#default_value' => '0',
      );     
      $form['field_price_range_max'] = array (
        '#title' => 'To',
        '#type' => 'select',
        '#multiple' => false,
        '#required' => true,
        '#options' => $to_options,
        '#default_value' => '10000000',
      ); 

Seneschall’s picture

You got farther than I did. I'm still not getting any dropdowns at all. What's your view setting for your price field? Also, is your code in a custom module, or did you add that somewhere to the actual views module or a views template?

my price field is set up like this:
http://www.terascape.net/images/propprice.jpg

msathesh’s picture

StatusFileSize
new142.75 KB
new143.1 KB

Yeah I have the code in my custom module only. You could have a look at the screenshots of my setting with max and min fields. And here is my complete code. Hope it helps. Let me know how it goes.

function mymodule_form_alter(&$form, &$form_state, $form_id) {

	if ($form_state['view']->name == 'searchfilter' && $form_state['view']->current_display == 'page_1') {
		
      $form['submit']['#value'] = t('Search');
      $from_options = array(
     '0' => t('Min. Price'),
     '100000' => t('$100,000'),
	 '150000' => t('$150,000'),
	 '200000' => t('$200,000'),
	 '250000' => t('$250,000'),
	 '300000' => t('$300,000'),
	 '350000' => t('$350,000'),
	 '400000' => t('$400,000'),
	 '450000' => t('$450,000'),
	 '500000' => t('$500,000'),
      );
    $to_options = array(
     '1000000000' => t('Max. Price'),
	 '150000' => t('$150,000'),
	 '200000' => t('$200,000'),
	 '250000' => t('$250,000'),
	 '300000' => t('$300,000'),
	 '350000' => t('$350,000'),
	 '400000' => t('$400,000'),
	 '450000' => t('$450,000'),
	 '500000' => t('$500,000'),
	 '550000' => t('$550,000'),
	 '600000' => t('$600,000'),
	 '100000000' => t('$600,000+'),
      );
      $form['price_min'] = array (
        '#title' => 'Price Range',
        '#type' => 'select',
        '#multiple' => false,
        '#required' => false,
        '#options' => $from_options,
        '#default_value' => '0',
      );     
      $form['price_max'] = array (
        '#type' => 'select',
        '#multiple' => false,
        '#required' => false,
        '#options' => $to_options,
        '#default_value' => '1000000000',
      );     
  }
}
Seneschall’s picture

Still no dropdown. This site is driving me nuts! Perhaps I'm missing an extra module somewhere.

msathesh’s picture

@skyline5k I don't think you need any extra modules to work. check your 'filter identifier names' within exposed filters matches with the php code in your module. Can you post the latest code with screenshots of your exposed filters?

Seneschall’s picture

StatusFileSize
new133.45 KB
new133.47 KB

I matched yours the best I could. original field name is still field_propprice

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {

    if ($form_state['view']->name == 'searchfilter' && $form_state['view']->current_display == 'page_1') {
        
      $form['submit']['#value'] = t('Search');
      $from_options = array(
     '0' => t('Min. Price'),
     '5000' => t('5000'),
     '10000' => t('10,000'),
     '15000' => t('15,000'),
     '20000' => t('20,000'),
     '30000' => t('30,000'),
     '35000' => t('35,000'),
     '40000' => t('40,000'),
     '45000' => t('45,000'),
     '50000' => t('50,000'),
      );
    $to_options = array(
     '1000000000' => t('Max. Price'),
      '10000' => t('10,000'),
     '15000' => t('15,000'),
     '20000' => t('20,000'),
     '30000' => t('30,000'),
     '35000' => t('35,000'),
     '40000' => t('40,000'),
     '45000' => t('45,000'),
     '50000' => t('50,000'),
     '60000' => t('60,000'),
     '60001' => t('60,000+'),
      );
      $form['price_min'] = array (
        '#title' => 'Price Range',
        '#type' => 'select',
        '#multiple' => false,
        '#required' => false,
        '#options' => $from_options,
        '#default_value' => '0',
      );     
      $form['price_max'] = array (
        '#type' => 'select',
        '#multiple' => false,
        '#required' => false,
        '#options' => $to_options,
        '#default_value' => '10000',
      );     
  }
}
?>
msathesh’s picture

In the if statement did you change your page's current display? And change

i.e. 'page_1' in [code]$form_state['view']->current_display == 'page_1'[/code] should be changed to suit your code.
and please change the 'searchfilter' in [code]$form_state['view']->name == 'searchfilter'[/code] to your views' name.
Can you post the page display setting screenshot here?

zdean’s picture

I tried #5 and everything works fine, except that I get an "An illegal choice has been detected. Please contact the site administrator." error.

Here is the code I used in my module:

function viewstweaks_form_alter(&$form, &$form_state, $form_id) {

	if ($form_state['view']->name == 'home_page_art_search' && $form_state['view']->current_display == 'page_2') {

	$form['submit']['#value'] = t('Search');
	$from_options = array(
	'0' => t('Min Height'),
	'10' => t('10 inches'),
	'20' => t('20 inches'),
	'30' => t('30 inches'),
	'40' => t('40 inches'),
	'50' => t('50 inches'),
	'60' => t('60 inches'),
	'70' => t('70 inches'),
	'80' => t('80 inches'),
	'90' => t('90 inches'),
	'100' => t('100 inches'),
		);
	$to_options = array(
	'10' => t('10 inches'),
	'20' => t('20 inches'),
	'30' => t('30 inches'),
	'40' => t('40 inches'),
	'50' => t('50 inches'),
	'60' => t('60 inches'),
	'70' => t('70 inches'),
	'80' => t('80 inches'),
	'90' => t('90 inches'),
	'100' => t('Max Height'),
		);
		$form['height_min'] = array (
		'#title' => 'Height',
		'#type' => 'select',
		'#multiple' => false,
		'#required' => false,
		'#options' => $from_options,
		'#default_value' => '0',
	);
	$form['height_max'] = array (
	'#type' => 'select',
	'#multiple' => false,
	'#required' => false,
	'#options' => $to_options,
	'#default_value' => '100',
		);
	}
}

Any suggestions on how to overcome the error?

Thanks in advance!!

zdean’s picture

I think I figured it out. For the options array, I needed to include:

'' => t(''),

So,


    $from_options = array(
    '0' => t('Min Height'),
    '10' => t('10 inches'),
    '20' => t('20 inches'),
    '30' => t('30 inches'),
    '40' => t('40 inches'),
    '50' => t('50 inches'),
    '60' => t('60 inches'),
    '70' => t('70 inches'),
    '80' => t('80 inches'),
    '90' => t('90 inches'),
    '100' => t('100 inches'),
        );

should be:



    $from_options = array(
     '' => t(''),
    '0' => t('Min Height'),
    '10' => t('10 inches'),
    '20' => t('20 inches'),
    '30' => t('30 inches'),
    '40' => t('40 inches'),
    '50' => t('50 inches'),
    '60' => t('60 inches'),
    '70' => t('70 inches'),
    '80' => t('80 inches'),
    '90' => t('90 inches'),
    '100' => t('100 inches'),
        );
lennyaspen’s picture

thanks guys this topic really helped me,

@zdean your solution rocked for "An illegal choice has been detected. Please contact the site administrator."
although wouldn`t it be better like this '' => t('Min Height'), even if it does not contain any value?

zdean’s picture

I think if anything it would have to be '' => t('Any')

lennyaspen’s picture

yes you are totally right :)

kars-t’s picture

Status: Active » Fixed

Hi

I am closing this issue to clean up the issue queue. Feel free to reopen the issue if there is new information and the problem still resides. If not please make sure you close your issues that you don't need any more.

Maybe you can get support from the local user group. Please take a look at this list at groups.drupal.org.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.