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 'function sf_input_object_pre'

Viewing 10 results - 1 through 10 (of 117 total)
  • Author
    Search Results
  • #276865

    Keith White
    Participant
    This reply has been marked as private.
    #276739

    Dennis Jonda
    Participant

    @Jermey Michel and @Trevor

    Maybe this will help:

    function filter_input_labels($input_object, $sfid) {
    if($input_object[‘name’] == ‘_my_field_name’) {

    /* var_dump($input_object); */

    // Fetch the options for this field
    $options = $input_object[‘options’];

    // Loop through the options and update the label for each one
    foreach($options as &$option) {
    $post_id = $option->value;
    $post = get_post($post_id);
    $title = $post->post_title;

    $option->label = $title;
    }

    // Update the options for this field
    $input_object[‘options’] = $options;
    }

    return $input_object;
    }
    add_filter(‘sf_input_object_pre’, ‘filter_input_labels’, 10, 2);

    It worked for me 🙂

    #275328

    Danny Parks
    Participant
    This reply has been marked as private.
    #274982

    Gnist Design
    Participant

    It does not respect the collation, which is set to utf8mb4_danish_ci.

    I had to use a filter to fix it:

    
    function sfp_sort_scandinavian( $input_object, $sfid ) {
    
    	if ( '_sfm_employees' === $input_object['name'] ) {
    
    		usort( $input_object['options'], function( $a, $b ) {
    			return strcmp( $a->value, $b->value );
    		});
    	}
    
    	return $input_object;
    }
    add_filter( 'sf_input_object_pre', 'sfp_sort_scandinavian', 10, 2 );
    
    #274760

    Ross
    Keymaster

    Hi Danny

    Writing something like that is a little out of scope of support (should require custom development) however I’ve gone ahead and bashed something toegether for you anyway (I won’t be able to customise this for you):

    
    function sf_filter_options_sub_terms( $input_object, $sfid ) {
    
    	$filter_name = '_sft_category';
    	$taxonomy_name = 'category';
    	$parent_term_id = 2; // you need to figure out the ID of the parent
    	
    	if ( $input_object['name'] !== $filter_name ) {
    		return $input_object;
    	}
    	
    	if ( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	if ( ! is_array( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// ask WP for the child terms
    	$child_terms = get_terms( $taxonomy_name, array( 'parent' => $parent_term_id, 'orderby' => 'slug', 'hide_empty' => false ) ); 
    	
    	// init the first option
    	$default_option = new StdClass();
    	$default_option->value = '';
    	$default_option->label = 'Choose a Category';
    	
    	// setup the new options array
    	$child_options = array( $default_option );
    	
    	// grab a reference to the search form
    	global $searchandfilter;
    	$search_form = $searchandfilter->get( $sfid );
    	
    	// loop through child terms
    	foreach ( $child_terms as $child_term ) {
    		
    		// add back in the dynamic count
    		$term_count = $search_form->get_count_var( $filter_name, $child_term->slug );
    		
    		//create a new option for the term
    		$new_option = new StdClass();
    		$new_option->value = $child_term->slug;
    		$new_option->label = $child_term->name;
    		$new_option->count = $term_count;
    		
    		array_push( $child_options, $new_option );
    	}
    	
    	// replace the old options with the new ones
    	$input_object['options'] = $child_options;
    	
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'sf_filter_options_sub_terms', 10, 2);

    What you need to do is change the 3 variables at the top to match your own:

    $filter_name = '_sft_category';
    $taxonomy_name = 'category';
    $parent_term_id = 2

    The first $filter_name is the name of your field in our plugin, which will be – _sft_product_cat
    The second is the actual taxonomy internal name product_cat
    And the third is the WP ID of the parent Product category (you can usually figure this out from WP admin by editing the term, and checking the URL)

    Once you replace those, this code will replace the options, with the child options of $parent_term_id.

    Let me know how you get on.

    Thanks

    #274552

    David Perez
    Participant

    Ok! so how could I make a checked option?

    add_filter( ‘sf_input_object_pre’, ‘filter_function_name’, 10, 2 );
    function filter_function_name( $input_object, $sfid )
    {
    if ( $input_object[‘name’]==’_sfm_aff_price_type’ ) {
    //udpate this field before rendering
    }

    return $input_object;
    }

    It returns this object:
    input_object:Array
    (
    [name] => _sfm_aff_price_type
    [defaults] => Array
    (
    [0] =>
    )

    [options] => Array
    (
    [0] => stdClass Object
    (
    [label] => Mensual
    [count] => -1
    [attributes] => Array
    (
    [class] => sf-level-0
    )

    [value] => month
    )

    [1] => stdClass Object
    (
    [label] => Trimestral
    [count] => -1
    [attributes] => Array
    (
    [class] => sf-level-0
    )

    [value] => quaterly
    )

    )

    [attributes] => Array
    (
    [data-operator] => and
    [class] =>
    )

    [accessibility_label] =>
    [type] => checkbox
    [input_class] => sf-input-checkbox
    )

    Which value do I have to change to make month checked in radio?


    Trevor
    Moderator

    Assuming that this is Post Categories, the easiest thing is to hide the All Categories with Custom CSS.

    .sf-field-category .sf-item-0 {
        display: none;
    }

    But, if it is a custom taxonomy, the name would be different, so you would need to substitute the class .sf-field-category with whatever the taxonomy you are using has as a class in our form.

    Is ‘This months featured blogs’ a category already in the list? If it is, you may need to re-order the list of terms. This is not so easy, but would use this filter (there are many other snippets of code using this filter in our forum):

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

    Snippet search:

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

    But, what is not possible would be to pre-select that option on loading the form for the first time. I think that feature will arrive with V3 of our plugin in a few months time.

    #273326

    Trevor
    Moderator

    For Taxonomies, the only way to do that would be to use this filter to change the label:

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

    There are many snippets of code using that filter in the forums:

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

    #272280

    Ross
    Keymaster

    Hi Keith

    We have a filter here for modifying all input fields:
    https://gist.github.com/rmorse/7b59b45a14b1ca179868

    Essentially, the only part you need from that is you need to loop through the options:

    function remove_empty_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 <code>_sfm_attorneys</code>
    	if(($input_object['name']!='_sfm_attorneys')){
    		return $input_object;
    	}
    	
    	if(!isset($input_object['options'])) {
    		return $input_object;
    	}
    	$new_options = array();
    	foreach($input_object['options'] as $option) {
    		if ( trim( $option->label ) !== '' ) {
    			array_push( $new_options, $option );
    		}
    	}
    	$input_object['options'] = $new_options;
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'remove_empty_options', 10, 2);

    I haven’t actually tested this, but it looks to be correct.

    You will need some PHP development skills to modify – that should go in your child themes functions.php.

    Best

    #271160

    Trevor
    Moderator

    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

Viewing 10 results - 1 through 10 (of 117 total)