Forums Forums Search Search Results for 'sf_input_object_pre'

Viewing 10 results - 11 through 20 (of 137 total)
  • Author
    Search Results
  • #270642

    Anonymous
    Inactive

    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
    Participant

    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
    Participant

    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

    Anonymous
    Inactive

    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
    Participant

    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

    Anonymous
    Inactive

    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
    Participant

    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

    Anonymous
    Inactive

    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);
    
    #266986

    Trevor
    Participant

    Hi

    The code goes in the child theme functions.php file.

    There you can check which page you are on before making changes. Tis forum search will provide other snippets, but you need to experiment with the code and see what you wrote does:

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

    #266721

    Ross
    Keymaster

    Hi Boris

    So I had a little play with this and got it half working – it will be for you to tidy up.

    Basically, there is a PHP function for sorting arrays by properties – usort (in this case we want to sort the array by count) :

    
    function cmp_reorder_desc($a, $b) {
        return $a->count < $b->count;
    }
    
    function reorder_options_count( $input_object, $sfid ) {
    
    	// change _sft_level for your field name
    	if($input_object['name']!=='_sft_level') {
    		return $input_object;
    	}
    	
    	if ( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	if ( ! is_array( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// $input_object['options'] // this is an array of options
    	// An option has 3 properties (and a few more):
    	//$option->label;
    	//$option->value;
    	//$option->count;
    	
    	//this doesn't take into consideration the first option "All items" - you might want to remove it (it is the first option: )
    	// $input_object['options'][0] and re-add to the array, after the sorting
    	
    	// we can use the PHP function to sort an array by "count": 
    	echo usort( $input_object['options'], "cmp_reorder_desc");
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'reorder_options_count', 10, 2);

    I added some notes in for you, remember to change the field name at the beginning to your own.

    Thanks

Viewing 10 results - 11 through 20 (of 137 total)