Container Conditions
Carbon Fields allows you to define your own container conditions as well. Here’s how to define and use a "Foobar_Condition" class for theme options containers:
Foobar_Condition.php
class Foobar_Condition extends Condition {
/**
* Check if the condition is fulfilled
*
* @param array $environment
* @return bool
*/
public function is_fulfilled( $environment ) {
return $this->compare(
'foobar', // Only return true when the passed value is 'foobar'
$this->get_comparison_operator(),
$this->get_value()
);
}
}
functions.php
or another suitable place
\Carbon_Fields\Carbon_Fields::extend( Foobar_Condition::class, function( $container ) {
// create an instance of our new class
$condition = new Foobar_Condition();
$condition->set_comparers( \Carbon_Fields\Carbon_Fields::resolve( 'generic', 'container_condition_comparer_collections' ) );
return $condition;
} );
add_filter( 'carbon_fields_theme_options_container_static_condition_types', function( $condition_types, $container_type, $container ) {
// append our new condition type to allowed theme options conditions
return array_merge(
$condition_types,
array( 'foobar' )
);
}, 10, 3 );
Container definitions
Container::make( 'theme_options', __( 'Foobar Container', 'crb' ) )
->where( 'foobar', '=', 'foobar' ); // this will always return true
// ->where( 'foobar', '=', 'lorem' ); // this will always return false