Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danieljwonder/9384b50d5fa050fa7b7fea4fb654928c to your computer and use it in GitHub Desktop.
Save danieljwonder/9384b50d5fa050fa7b7fea4fb654928c to your computer and use it in GitHub Desktop.

Revisions

  1. @rmorse rmorse revised this gist Sep 15, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sf-pro-filter-input-object.php
    Original file line number Diff line number Diff line change
    @@ -66,7 +66,7 @@ function filter_input_object($input_object, $sfid)

    //create a new option we want to add
    //options must have a value and label
    $new_last_option = new StdClass();
    $new_last_option = new stdClass();
    $new_last_option->value = "my_custom_value";
    $new_last_option->label = "This is a New Option";

  2. @rmorse rmorse revised this gist Jan 7, 2016. 1 changed file with 30 additions and 17 deletions.
    47 changes: 30 additions & 17 deletions sf-pro-filter-input-object.php
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,36 @@
    <?php
    function filter_input_object($input_object, $sfid)
    {
    var_dump($input_object);
    //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;
    }

    //we want to filter the options generated, so make sure the options variable actually exists before proceeding
    //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;
    @@ -25,6 +46,10 @@ function filter_input_object($input_object, $sfid)
    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";
    }
    }

    @@ -45,12 +70,11 @@ function filter_input_object($input_object, $sfid)
    $new_last_option->value = "my_custom_value";
    $new_last_option->label = "This is a New Option";

    //attributes are optional - must be assoc arrays - these are mapped to the html output
    //attributes on options are optional - must be assoc arrays - these are mapped to the html output
    $new_last_option->attributes = array(
    'class' => 'custom_class another_custom_class',
    'class' => 'custom_option_class another_custom_option_class',
    'title' => 'My Custom Title',
    'style' => 'background-color: #aa6666;',
    'onclick' => 'alert("this is horrible JS")'
    'style' => 'background-color: #46a064;color:#ffffff'
    );

    //add a brand new option to the bottom
    @@ -66,17 +90,6 @@ function filter_input_object($input_object, $sfid)
    //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) );

    //add/override prefix & postfix to the field
    $input_object['prefix'] = "Prefix";
    $input_object['postfix'] = "Postfix";

    //manually set the values shown in the field - must be array even if single value
    //this should really only occur under specific conditions, otherwise you field will be permanently set to this value
    $input_object['defaults'] = array("black");

    //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:";

    return $input_object;
    }

  3. @rmorse rmorse revised this gist Jan 7, 2016. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions sf-pro-filter-input-object.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    <?php
    function filter_input_object($input_object, $sfid)
    {
    var_dump($input_object);
    //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'))
    @@ -65,6 +66,17 @@ function filter_input_object($input_object, $sfid)
    //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) );

    //add/override prefix & postfix to the field
    $input_object['prefix'] = "Prefix";
    $input_object['postfix'] = "Postfix";

    //manually set the values shown in the field - must be array even if single value
    //this should really only occur under specific conditions, otherwise you field will be permanently set to this value
    $input_object['defaults'] = array("black");

    //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:";

    return $input_object;
    }

  4. @rmorse rmorse created this gist Jan 7, 2016.
    72 changes: 72 additions & 0 deletions sf-pro-filter-input-object.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    <?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;
    }

    //we want to filter the options generated, so make sure the options variable actually exists before proceeding
    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.")";
    }
    }

    //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 are optional - must be assoc arrays - these are mapped to the html output
    $new_last_option->attributes = array(
    'class' => 'custom_class another_custom_class',
    'title' => 'My Custom Title',
    'style' => 'background-color: #aa6666;',
    'onclick' => 'alert("this is horrible JS")'
    );

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