Skip to content

Instantly share code, notes, and snippets.

@rmorse
Last active February 6, 2024 23:01
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rmorse/7b59b45a14b1ca179868 to your computer and use it in GitHub Desktop.
Search & Filter Pro - Filter Input Object
<?php
function filter_input_object($input_object, $sfid)
{
//ensure we are only filtering the correct field name - in this case the field we want to filter has the name `_sfm_colours`
//we also want to make sure its a `select` input type we're filtering
if(($input_object['name']!='_sfm_colours')||($input_object['type']!='select'))
{
return $input_object;
}
//change parameters in the field
//manually set the values shown/selected in the field - must be array even if single value
//this should really only occur under specific conditions, otherwise your field will be permanently set to this value
$input_object['defaults'] = array("black");
//change classes & attributes of the field itself - must be assoc array - you can add any attributes you can think of or would need
//you should never change the field name or replace the attributes array
$input_object['attributes']['class'] = 'custom_class another_custom_class';
$input_object['attributes']['title'] = 'My Custom Title';
$input_object['attributes']['style'] = 'background-color: #c7d8ff;color:#4662a0;';
$input_object['attributes']['onclick'] = 'console.log("this is horrible JS")';
//add/override prefix & postfix to the field
$input_object['prefix'] = "Prefix";
$input_object['postfix'] = "Postfix";
//this will only be used on select fields and text input types - do not use - this variable will be renamed soon to `screen_reader_text`
//$input_object['accessibility_label'] = "Screen Reader Text:";
//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;
}
//now we know there are options we can loop through each one, and change what we need
foreach($input_object['options'] as $option)
{
if($option->value=="")
{//the option with no value is always the "all items" or unselected state
$option->label = "Choose a Colour:";
}
else if($option->value=="black")
{//we want to change the label for the option "black" - we can feed back in the count number to the label for this field type
$option->label = "Jet Black (".$option->count.")";
//yes you can even change the attributes on each option if necessary!
$option->attributes['style'] = "color:#fff;background-color:#000";
$option->attributes['title'] = "This is Jet Black";
}
}
//options are just an array so we can do all the usual things with arrays using PHP
//reverse the options list
$input_object['options'] = array_reverse($input_object['options']);
//put the "all items" option back to the top of the array, which was moved to bottom by `array_reverse`
//S&F requires the first item in an options list to always be the unselected state/option
$last_option = array_pop($input_object['options']); //pop last option
array_unshift($input_object['options'],$last_option); //prepend
//create a new option we want to add
//options must have a value and label
$new_last_option = new stdClass();
$new_last_option->value = "my_custom_value";
$new_last_option->label = "This is a New Option";
//attributes on options are optional - must be assoc arrays - these are mapped to the html output
$new_last_option->attributes = array(
'class' => 'custom_option_class another_custom_option_class',
'title' => 'My Custom Title',
'style' => 'background-color: #46a064;color:#ffffff'
);
//add a brand new option to the bottom
array_push($input_object['options'], $new_last_option);
//create a new option we want to add
//options must have a value and label
$new_first_option = new StdClass();
$new_first_option->value = "another_custom_value";
$new_first_option->label = "New First Option";
//insert option to options array at position 1 (after the default "all items option")
array_splice( $input_object['options'], 1, 0, array($new_first_option) );
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object', 10, 2);
?>
@bahamadigital
Copy link

May I ask how I can loop through checkbox (or radio) inputs, if I want to add a class to each individual checkbox?

In case of checkboxes, this code...

if(($input_object['type']=='checkbox')) { $input_object['attributes']['class'] = 'custom_class another_custom_class'; return $input_object; }

...adds class only to the ul tag that contains all the checkboxes. I assume I need to loop through the checkboxes like you are looping through options, but I can't figure out how. Could you add an example of this in the code please?

@effectiveVision
Copy link

Hi -

Is there any way to keep this filter applied when 'Update the Search Form on user interaction' is enabled?

@rmorse
Copy link
Author

rmorse commented Dec 30, 2022

@bahamadigital + those that are looking to add a class to options, you need to cycle through the options as above:

//now we know there are options we can loop through each one, and change what we need
	foreach($input_object['options'] as $option)
	{
			// Update the class attribute of the option.
			$option->attributes['class'] = "custom-class";
		}
	}

@rmorse
Copy link
Author

rmorse commented Dec 30, 2022

@effectiveVision this filter will always be applied the search form, even after interaction.

@bahamadigital
Copy link

Thank you @rmorse!

@bahamadigital + those that are looking to add a class to options, you need to cycle through the options as above:

@itsViney
Copy link

Is it possible to change the Select2 configuration for a field when the 'combobox' option is ticked? I'm trying to add placeholder text and enable allowClear instead of showing a null at the top of the list.

@drummsb4
Copy link

drummsb4 commented Mar 3, 2023

@rmorse - It does not appear that this filter stays applied when filtering the form with AJAX enabled. Is there something I'm missing that would ensure this is always enabled? It seems after the AJAX call is finished, the form labels reset to the original value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment