Forums Forums Search Search Results for 'sf_input_object_pre function label'

Viewing 10 results - 1 through 10 (of 62 total)
  • Author
    Search Results
  • #276739

    Anonymous
    Inactive

    @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 🙂

    #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

    Anonymous
    Inactive

    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?

    #273326

    Trevor
    Participant

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

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

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