Support » Plugin: Carbon Fields » Activate fields conditionally

  • Resolved alexadark

    (@alexadark)


    Hi,
    i am doing a page builder with complex fields groups, each group is a layout.
    i want to put options to activate certain layouts or not, as i will use this plugin to build different themes.
    so i have made theme options with set

    ontainer::make( 'theme_options', 'Theme Options' )
             ->add_fields( array(
    	         Field::make( 'set', 'crb_active_layouts' )
    	              ->add_options( array(
    		              'text_area'       => 'Text Area',
    		              'slider'          => 'Slider',
    		              'slideshow_panel' => 'Slideshow Panel',
    		              'parallax'        => 'Parallax Area',
    		              'panel_switcher'  => 'Panel Switcher'
    	              ) )
             ) );

    then retrieve the array like that
    $active_layouts = carbon_get_theme_option('crb_active_layouts');
    then i suppose i can make the condition like that

    if(in_array(‘slider’,$active_layouts){
    //Field registration
    }

    i think my logic is good, but not the syntax, perhaps because i am bad with oop

    https://wordpress.org/plugins/carbon-fields/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author htmlBurger

    (@htmlburger)

    Hi @alexadark,

    Your approach is great! In my opinion – don’t complicate things with OOP, its a simple problem that needs a simple solution.

    If you have further questions, don’t hesitate to contact us.

    Thread Starter alexadark

    (@alexadark)

    Hi,
    yes but i don’t know how to do it…
    basically i have this structure
    `Container::make( ‘post_meta’, ‘layouts’ )
    ->show_on_post_type( ‘page’ )
    ->show_on_template( ‘builder-page.php’ )
    ->add_fields( array(
    Field::make( ‘complex’, ‘crb_block_layouts’ )
    /**
    * TEXT AREA
    */

    ->add_fields( ‘text_area’, array(
    //fields for text area
    ) )
    /**
    * SLIDER
    */
    ->add_fields( ‘slider’, array(
    //fields fore slider
    ))`

    then i have the functions to displays them in another files

    if for exemple i don’t want to activate the slider how can i write it ?
    because if i write a conditional arround it gives me a php error

    `Container::make( ‘post_meta’, ‘layouts’ )
    ->show_on_post_type( ‘page’ )
    ->show_on_template( ‘builder-page.php’ )
    ->add_fields( array(
    Field::make( ‘complex’, ‘crb_block_layouts’ )
    /**
    * TEXT AREA
    */

    ->add_fields( ‘text_area’, array(
    //fields for text area
    ) )
    if(//condition){
    /**
    * SLIDER
    */
    ->add_fields( ‘slider’, array(
    //fields fore slider
    ))
    }`

    but i don’t know how to write my condition, as i say it is a syntax problem

    Plugin Author htmlBurger

    (@htmlburger)

    @alexadark, you can make a field builder function that populates the complex field with the needed groups. Maybe something like:

    Container::make( 'post_meta', 'layouts' )
    	->show_on_post_type( 'page' )
    	->show_on_template( 'builder-page.php' )
    	->add_fields( array(
    		crb_get_layouts_complex_field( 'crb_block_layouts' )
    	) );
    
    function crb_get_layouts_complex_field( $name ) {
    	$active_layouts = (array) carbon_get_theme_option('crb_active_layouts');
    
    	$complex = Field::make( 'complex', $name );
    
    	$fields = array(
    		'text_area' => array(
    			// fields for text area
    		),
    
    		'slider' => array(
    			// fields for slider
    		),
    	);
    
    	foreach ($active_layouts as $layout) {
    		if ( empty( $fields[$layout] ) ) {
    			continue;
    		}
    
    		$complex->add_fields( $layout, $fields[$layout] );
    	}
    
    	return $complex;
    }
    Thread Starter alexadark

    (@alexadark)

    Thank you !
    i am going to try !

    Thread Starter alexadark

    (@alexadark)

    It works !

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Activate fields conditionally’ is closed to new replies.