Search Results for 'sf_input_object_pre range'

Forums › Forums › Search › Search Results for 'sf_input_object_pre range'

Viewing 10 results – 1 through 10 (of 14 total)
  • Author
    Search Results
  • #271160

    Trevor
    Participant

    OK, and for anybody who has the same conditions that the previous user has, here is a demo form:

    https://sandfdev.cdnwebservices.net/shop/

    There are 5 ‘posts’, each has one ‘date’, each in a different year (2016-2020 inclusive).

    The ‘Year’ field is based on a custom field named first_date_from, and this is an ACF date field with a single date saved in the format YYYYMMDD.

    The code below is pasted in to the child theme functions.php file:

    function update_date_field_options($input_object, $sfid){
    	// ensure we are only filtering the correct field name in the correct form - in this case the field we want to filter has the name '_sfm_first_date_from', based on the field name 'first_date_from'
    	if( $input_object['name'] == '_sfm_first_date_from' && $sfid == 42 ) {
    		
    		// if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
    		if( ! isset( $input_object['options'] ) ) {
    			return $input_object;
    		}
    		//udpate this field before rendering
    		//all the options are stored in $input_object['options'] as an array
    		$new_options = array();
    
    		//create a new "default" option
    		$new_option = new StdClass();
    		$new_option->value = "";
    		$new_option->label = "All Years";
    		array_push($new_options, $new_option);	
    	
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "20160101+20161231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2016"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		$new_option = new StdClass();
    		$new_option->value = "20170101+20171231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2017"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		$new_option = new StdClass();
    		$new_option->value = "20180101+20181231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2018"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		$new_option = new StdClass();
    		$new_option->value = "20190101+20191231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2019"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		$new_option = new StdClass();
    		$new_option->value = "20200101+20201231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "2020"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		//now replace the options with our own custom options:
    		$input_object['options'] = $new_options;
    	}
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_date_field_options', 10, 2);

    I hope you can see how it works by actually sending a date range of 1 Jan to 31 Dec of each year to the form. So, if you want more years, add them as you need.

    This is how the field is set:

    https://www.screencast.com/t/R2hhmi9WePv

    #270963

    Ross
    Keymaster

    Hi Jessica

    I think I see what is happening!

    So I just tried your code (albeit changed it for my setup) and the strangest thing was happening, I was getting some checkboxes with labels missing, and some working just fine.

    I then realised (and didn’t know) that at least for checkboxes, the array of options must be in order.

    So unsetting specific values leaves “holes” in the array, that for some reason our checkbox code can’t work around for now.

    Instead I did this (push the wanted options, into a new array):

    function filter_input_object($input_object, $sfid) {
    	if( !in_array( $input_object['name'], array('_sft_pa_color') ) ) {
    		return $input_object;
    	}
    
    	if( $input_object['name'] == '_sft_pa_color' ) {
    		$new_options = array();
    		foreach($input_object['options'] as $key => $option) {
    			if ( in_array( $option->value, array( 'gray', 'green') ) ) {
    				$new_options[] = $option;
    			}
    		}
    		$input_object['options'] = $new_options;
    	}
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);

    Another way would be to keep your code exactly the way it was, but before returning $input_object do:

    $input_object['options'] = array_values( $input_object['options'] );

    This will effectively reset the indexes too.

    Let me know how you get on!

    Thanks

    #270878

    Trevor
    Participant

    With a date field, there are only two ways I can think to do this.

    1. Have an additional custom field in the posts where one of those 3 labels is the choice.
    2. Use code. How this would work is:

    # Set the form UI field to a number range field, like this (in this case I chose a radio field, but a dropdown would also work, I think):

    https://www.screencast.com/t/rpY6G9KLnX

    Ignore my field name! Use what you have as a field name.

    Then add code like this to your child theme functions.php file (change the field name, form ID number and dates to work for you):

    function update_field_options($input_object, $sfid){
    	
    	// ensure we are only filtering the correct field name in the correct form - in this case the field we want to filter has the name '_sfm_first_date_to', based on the field name 'first_date_to'
    	if( $input_object['name'] == '_sfm_first_date_to' && $sfid == 42 ) {
    		
    		// if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
    		if( ! isset( $input_object['options'] ) ) {
    			return $input_object;
    		}
    		//udpate this field before rendering
    		//all the options are stored in $input_object['options'] as an array
    		$new_options = array();
    
    		//create a new "default" option
    		$new_option = new StdClass();
    		$new_option->value = "";
    		$new_option->label = "All Ages";
    		array_push($new_options, $new_option);	
    	
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "19900101+29991231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "Latest"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    
    		$new_option = new StdClass();
    		$new_option->value = "0+19561231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "Golden Age"; //the label can be anything
    		array_push($new_options, $new_option);
    
    		$new_option = new StdClass();
    		$new_option->value = "19570101+19891231"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "Silver Age"; //the label can be anything
    		array_push($new_options, $new_option);
    
    		//now replace the options with our own custom options:
    		$input_object['options'] = $new_options;
    	}
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);

    You can see what it looks like on this demo I made (it is not functional as the data does not match!!):

    https://sandfdev.cdnwebservices.net/shop/

    I hope that makes sense!

    It assumes that the date data is correctly stored in ACF/SQL date format(YYYYMMDD).

    #262072

    In reply to: Multipel Tags fields


    Trevor
    Participant

    I think it should be this:

    //Search & filters hook
    
    function update_range_field_mileage($input_object, $sfid)
    {
    //make sure we affect the '_sfm_standby_power_esp_kvs_nr' field only
    if($input_object['name']=='_sfm_standby_power_esp_kvs_nr' && $sfid==1893)
    {
    //udpate this field before rendering
    //all the options are stored in $input_object['options'] as an array
    $new_options = array();
    
    //create a new "default" option
    $new_option = new StdClass();
    $new_option->value = "";
    $new_option->label = "kVA";
    array_push($new_options, $new_option);
    
    //create a new range option we want to add
    $new_option = new StdClass();
    $new_option->value = "10+50"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "10 – 50"; //the label can be anything
    array_push($new_options, $new_option);//create a new range option we want to add
    
    $new_option = new StdClass();
    $new_option->value = "51+200"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "51 – 200"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "201+400"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "201 – 400"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "401+700"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "401 – 700"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "701+1500"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "701 – 1500"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "1501+2500"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "1501 – 2500"; //the label can be anything
    array_push($new_options, $new_option);
    
    //now replace the options with our own custom options:
    $input_object['options'] = $new_options;
    }
    
    return $input_object;
    }
    add_filter('sf_input_object_pre', 'update_range_field_mileage', 10, 2);
    #262056

    In reply to: Multipel Tags fields


    Anonymous
    Inactive

    Hi Trevor

    Thanks.

    Can you just check if it is done correctly with form id if($sfid==1893)

    Thanks

    //Search & filters hook
    
    function update_range_field_mileage($input_object, $sfid)
    {
    //make sure we affect the '_sfm_standby_power_esp_kvs_nr' field only
    if($input_object['name']=='_sfm_standby_power_esp_kvs_nr')
    
    if($sfid==1893)
    {
    //udpate this field before rendering
    //all the options are stored in $input_object['options'] as an array
    $new_options = array();
    
    //create a new "default" option
    $new_option = new StdClass();
    $new_option->value = "";
    $new_option->label = "kVA";
    array_push($new_options, $new_option);
    
    //create a new range option we want to add
    $new_option = new StdClass();
    $new_option->value = "10+50"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "10 – 50"; //the label can be anything
    array_push($new_options, $new_option);//create a new range option we want to add
    
    $new_option = new StdClass();
    $new_option->value = "51+200"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "51 – 200"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "201+400"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "201 – 400"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "401+700"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "401 – 700"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "701+1500"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "701 – 1500"; //the label can be anything
    array_push($new_options, $new_option);
    
    $new_option = new StdClass();
    $new_option->value = "1501+2500"; //this is the value that goes in the URL, which affects the query
    $new_option->label = "1501 – 2500"; //the label can be anything
    array_push($new_options, $new_option);
    
    //now replace the options with our own custom options:
    $input_object['options'] = $new_options;
    }
    
    return $input_object;
    }
    add_filter('sf_input_object_pre', 'update_range_field_mileage', 10, 2);
    #262027

    In reply to: Multipel Tags fields


    Trevor
    Participant

    Hi

    Great to speak with you Sasha. The link for the main function was this:

    https://support.searchandfilter.com/forums/topic/range-overlap/#post-231607

    Testing the form id would make it look like this:

    function update_range_field_mileage($input_object, $sfid)
    {
    	//make sure we affect the '_sfm_mileage' field only, and only in form ID 12345
    	if( $sfid == 12345 && $input_object['name']=='_sfm_mileage' )
    	{
    		//udpate this field before rendering
    		//all the options are stored in <code>$input_object['options']</code> as an array
    		$new_options = array();
    		
    		//create a new "default" option
    		$new_option = new StdClass();
    		$new_option->value = "";
    		$new_option->label = "KM Stand";
    		array_push($new_options, $new_option);
    		
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "0+49999"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "0 - 49999"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    		
    		$new_option = new StdClass();
    		$new_option->value = "50000+99999"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "50000 - 99999"; //the label can be anything
    		array_push($new_options, $new_option);
    		
    		$new_option = new StdClass();
    		$new_option->value = "100000+150000"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "100000 - 150000"; //the label can be anything
    		array_push($new_options, $new_option);
    		
    		
    		//now replace the options with our own custom options:
    		$input_object['options'] = $new_options;
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'update_range_field_mileage', 10, 2);

    Do let me know how you get on?

    #234883

    Anonymous
    Inactive

    Hello,

    I’m using a combobox to display / filter regions on a real estate website. How can I add a new region to the combobox via my functions.php file? Basically, I want to combine a few towns into a single option.

    I’ve used the code below to add options to a normal dropdown in the search but couldn’t get something similar to work for the combobox:

    function edit_rent_range($input_object, $sfid) {
    	if(($input_object['name']!='_sfm_rent_max')||($input_object['type']!='select'))
    	{
    		return $input_object;
    	}
    	
    	if(!isset($input_object['options']))
    	{
    		return $input_object;
    	}
    	
    	//create a new option we want to add
    	$new_last_option = new StdClass();
    	$new_last_option->value = "3000000";
    	$new_last_option->label = "$35,000+";
    	
    	//add a brand new option to the bottom
    	array_push($input_object['options'], $new_last_option);
    		
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'edit_rent_range', 10, 2);

    Any help will be appreciated.

    #231607

    In reply to: Range overlap


    Ross
    Keymaster

    Hi Patrick

    It’s not possible within the UI, but you can use a filter to manually update a fields options (and values):
    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    I’ve done a test, and here is the code you would need to affect your mileage field (copy to functions.php in your child theme):

    function update_range_field_mileage($input_object, $sfid)
    {
    	//make sure we affect the '_sfm_mileage' field only
    	if($input_object['name']=='_sfm_mileage')
    	{
    		//udpate this field before rendering
    		//all the options are stored in <code>$input_object['options']</code> as an array
    		$new_options = array();
    		
    		//create a new "default" option
    		$new_option = new StdClass();
    		$new_option->value = "";
    		$new_option->label = "KM Stand";
    		array_push($new_options, $new_option);
    		
    		//create a new range option we want to add
    		$new_option = new StdClass();
    		$new_option->value = "0+49999"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "0 - 49999"; //the label can be anything
    		array_push($new_options, $new_option);//create a new range option we want to add
    		
    		$new_option = new StdClass();
    		$new_option->value = "50000+99999"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "50000 - 99999"; //the label can be anything
    		array_push($new_options, $new_option);
    		
    		$new_option = new StdClass();
    		$new_option->value = "100000+150000"; //this is the value that goes in the URL, which affects the query
    		$new_option->label = "100000 - 150000"; //the label can be anything
    		array_push($new_options, $new_option);
    		
    		
    		//now replace the options with our own custom options:
    		$input_object['options'] = $new_options;
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'update_range_field_mileage', 10, 2);

    Let me know how you get on.

    Thanks

    #184484

    Anonymous
    Inactive

    I have a range field with number inputs. I’ve figured out how to empty the default values out using the ‘sf_input_object_pre’ filter. However, when doing this I get 0 search results upon submitting the form. Basically, I’d like for this field to be optional. How can I achieve this?

    #141135

    Anonymous
    Inactive

    Do you mean to open the url

    https://support.searchandfilter.com/forums/search/sf_input_object_pre/
    instead of
    https://support.searchandfilter.com/forums/search/sf_input_object_pre+range/ ?

    I’m afraid i’ll need some more help Trevor, in any case I really appreciate your fast answers…

Viewing 10 results – 1 through 10 (of 14 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.