Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search Search Results for 'sf_input_object_pre'

Viewing 10 results - 11 through 20 (of 150 total)
  • Author
    Search Results
  • #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
    Moderator

    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).

    #270642

    Jessica Spata
    Participant

    The full code is below. They both work as dropdowns, but neither work correctly as checkboxes. They output some options, but not all of them. It might be to do with how I removed them from the array, is there a better way to do it?

    function filter_input_object($input_object, $sfid) {
    	if( !in_array( $input_object['name'], array('_sft_age_group','_sft_health_problem') ) )
    		return $input_object;
    
    	if( $input_object['name'] == '_sft_age_group' ) {
    		foreach($input_object['options'] as $key => $option) {
    			if ($option->value == '') {
    			} elseif ($option->value == 'prenatal') {
    				$option->label = 'Early Childhood (<5 year)';
    				$option->value = 'prenatal+infant+pre-school';
    			} elseif ($option->value == 'primary-school') {
    				$option->label = 'Primary School (5-12 years)';
    			} elseif ($option->value == 'early-secondary-school') {
    				$option->label = 'Secondary School (13-17 years)';
    				$option->value = 'early-secondary-school+late-secondary-school';
    			} elseif ($option->value == 'early-adulthood') {
    				$option->label = 'Young Adulthood (18+ years)';
    			} else {
    				unset($input_object['options'][$key]);
    			}
    		}
    		return $input_object;
    	}
    
    	if( $input_object['name'] == '_sft_health_problem' ) {
    		foreach($input_object['options'] as $key => $option) {
    			if ( !in_array( $option->value, array('option-a','option-b','option-c') ) ) {
    				unset($input_object['options'][$key]);
    			}
    		}
    		return $input_object;
    	}
    }
    add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
    #269630

    Trevor
    Moderator

    You will likely need to recreate the field terms array, using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #269204

    Trevor
    Moderator

    I can see it is in the raw data and HTML, so it is there in your database like that. You could change the labels in the Select box, using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    You would need to use PHP to remove the unwanted characters and change the label text as you need.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+label/

    #268437

    Warren O’Donoghue
    Participant

    Thanks Trevor, that works perfectly — you’re a star!

    Here’s the full working version for anyone else who’s interested:

    function update_field_options($input_object, $sfid){
    	
    	// ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag'
    	if( $input_object['name'] != '_sfm__price' ) {
    		return $input_object;
    	}
    		
    	// 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;
    	}
    	
    	// this is an array of all the options, we can remove them all, recreate them, etc
    	// var_dump( $input_object['options'] );
    	
    	// we know the option you want to replace is the 7th one in the list (including the default), so we can do :
    	if ( isset ( $input_object['options'][5] ) ) {
    		$new_option = new StdClass();
    		$new_option->value = "40000+10000000";
    		$new_option->label = "40000 plus";
    		$input_object['options'][5] = $new_option;
    	}
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);

    And finally I adjusted the UI options to Max value 40000 so no more options are displayed in the popup selector.

    #268419

    Trevor
    Moderator

    This line is wrong:

    if( $input_object['name'] != '_price' ) {

    It should be:

    if( $input_object['name'] != '_sfm__price' ) {

    I think. You also need to limit the number of entries in the dropdown to just 7. You would have to slice the array. Maybe like this at the end:

    $input_object = array_slice( $input_object , 0 , 7, true );
    return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);
    #268340

    Warren O’Donoghue
    Participant

    Thanks Trevor,

    I tried the following but it hasn’t worked sadly:

    function update_field_options($input_object, $sfid){
    	
    	// ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag'
    	if( $input_object['name'] != '_price' ) {
    		return $input_object;
    	}
    		
    	// 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;
    	}
    	
    	// this is an array of all the options, we can remove them all, recreate them, etc
    	// var_dump( $input_object['options'] );
    	
    	// we know the option you want to replace is the 4th one in the list (including the default), so we can do :
    	if ( isset ( $input_object['options'][4] ) ) {
    		$new_option = new StdClass();
    		$new_option->value = "50000+10000000";
    		$new_option->label = "50000 plus";
    		$input_object['options'][4] = $new_option;
    	}
    
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);

    Do you have any ideas?

    Many thanks
    Warren

    #267669

    In reply to: Select: Option order


    Trevor
    Moderator

    You would have to do this using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    To re-order the terms in the field.

    I am not sure if it will help, but if there are many snippets already in the forum, this forum search should find them:

    https://support.searchandfilter.com/forums/search/sf_input_object_pre+function+order/

    #267028

    Mark van den Ing
    Participant

    I got it figured out for a bit. The only thing that is not working now is filtering the childs of the models. The code now rules out every choice. Any ideas about that?

    
    function remove_obsolete_dom_elements_from_filter($input_object, $sfid) {
    
    /* check what taxonomy page we are on */
    
    $taxonomy = get_query_var('taxonomy');
    
    // Business rules :
    //
    // On brand taxonomy page (data-current-taxonomy-archive=brand) 
    // - remove brands
    // - remove non-selected brand related models (count?)  
    //
    // On model taxonomy page  
    // - remove brands
    // - remove all parent models - NOT WORKING YET - removes every thing - want to show current parent and underlying childs
    // 	
    
    //make sure we affect the '_sft_brand' field only, and only in form ID 44286
    
    	if ($taxonomy == "brand") {
    		if(($input_object['name']!='_sft_brand')||($input_object['type']!='select')) {
     		return $input_object;
    		}
      
    		if(!isset($input_object['options'])) {
    		return $input_object;
    		}
    		
    		if( $sfid == 44286 && $input_object['name']=='_sft_brand' )	{
    		unset($input_object['options']);
    		return $input_object;
    		}
    	}
    	
    	elseif ($taxonomy == "model") {
    		
    		if(($input_object['name']!='_sft_model')||($input_object['type']!='select')) {
     		return $input_object;
    		}
      
    		if(!isset($input_object['options'])) {
    		return $input_object;
    		}
    		/*
    		if( $sfid == 44286 && $input_object['name']=='_sft_brand' )	{
    		unset($input_object['options']);
    		return $input_object;
    		} */
    		if( $sfid == 44286 && $input_object['name']=='_sft_model' )	{
    		unset($input_object['options']); // only unset not selected "parents" - how to do that?
    		return $input_object;
    		}
    	}
    	
    	else {
    		return $input_object;
    	}
    }
    
    add_filter('sf_input_object_pre', 'remove_obsolete_dom_elements_from_filter', 10, 2);
    
Viewing 10 results - 11 through 20 (of 150 total)