• Resolved martnick

    (@martnick)


    Hello, I have been using your plugin for quite some time now. And started making some advanced things with it. I have a complex field with selects, that hide and show different fields dependent on the selected value. But in the DB it saves even the hidden fields as rows.

    Some code:
    Creating a theme option container

    $roles = carbon_get_theme_option('soscenter_roles','complex');
    
    $dynamic_roles_container = Container::make('theme_options','Plugin Roles Options')->set_page_parent('Plugin Options');

    Going through roles

    for($i = 0; $i < count($roles); $i++) {
    	$dynamic_roles_container->add_fields(generate_fields_array($roles[$i]['soscenter_role_name'], $roles[$i]['soscenter_display_name']));
    }
    function generate_fields_array($role_code, $role_display) {
    	$field_types = array(
    		'text' => 'Text Field',
    		'select' => 'Select');
    
    	/*
    	 * Make a field with role name as seperator text
    	 * Add complex field to which we will add fields later
    	 * Adding select with field options to it
    	 */
    	$seperator = Field::make('separator',$role_code . 'role_separator',$role_display);
    	$complex_part = Field::make('complex',$role_code . '_add_field','Role Fields');
    	$complex_part_fields_array = array();
    	$types_select = Field::make('select',$role_code . '_field_type','Field Type')->add_options($field_types);
    
    	$complex_part_fields_array[] = $types_select;
    
    	/*
    	 * Going through field options
    	 */
    	foreach($field_types as $key => $value) {
    		$temp_field_array = array();
    
    		/*
    		 * Creating different fields to handle a specific role field
    		 */
    		switch($key) {
    			case 'text':
    				$field_code = $role_code . '_' . $key . '_';
    				$temp_field_array[] = Field::make('text',$field_code . 'label','Label');
    				$temp_field_array[] = Field::make('text',$field_code . 'code','Code Name')->set_required(true);
    				$temp_field_array[] = Field::make('text',$field_code . 'placeholder','Placeholder Value');
    				$temp_field_array[] = Field::make('text',$field_code . 'default_value','Default Value');
    			break;
    			case 'select':
    				$field_code = $role_code . '_' . $key . '_';
    				$temp_field_array[] = Field::make('text',$field_code . 'label','Label');
    				$temp_field_array[] = Field::make('text',$field_code . 'code','Code Name')->set_required(true);
    				$temp_field_array[] = Field::make('complex',$field_code .'options')->add_fields(array(
    					Field::make('text',$field_code . 'option_key', 'Key')->set_width(50)->set_required(true),
    					Field::make('text',$field_code . 'option_value', 'Value')->set_width(50)->set_required(true),
    				));
    			break;
    		}
    
    		/*
    		 * Making fields conditions to be displayed only on the specific select value
    		 */
    		$relations_array = array(
    			'relation' => 'AND',
    			array(
    				'field' 	=> $role_code . '_field_type',
    				'value' 	=> $key,
    				'compare' 	=> '=',
    			)
    		);
    
    		foreach($temp_field_array as $field_to_be_added) {
    			$field_to_be_added->set_conditional_logic($relations_array);
    			$complex_part_fields_array[] = $field_to_be_added;
    		}
    	}
    
    	/*
    	 * Returns the specific field generated for role
    	 */
    	return array(
    		$seperator,
    		$complex_part->add_fields($complex_part_fields_array));
    }

    And for a test role in the db it is added like this:
    test_add_field_-test_field_type_0
    test_add_field_-test_text_label_0
    test_add_field_-test_text_code_0
    test_add_field_-test_text_placeholder_0
    test_add_field_-test_text_default_value_0
    test_add_field_-test_select_label_0
    test_add_field_-test_select_code_0

    https://ww.wp.xz.cn/plugins/carbon-fields/

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

    (@htmlburger)

    Hi @martnick,

    This behavior is by design. The Conditional functionality is meant to be used only to improve User Experience.

    Imagine the following use-case.

    You need to add Credentials options for a service, which has both “Sandbox” and “Live” environments, e.g. PayPal. For both of these environments, you will have two options “Client ID” and “Secret Key”. In addition to these, you have another option “Choose Environment”.

    function crb_environment_conditional_logic( $environment ) {
        return array(
            array(
                'field' => 'paypal_environment',
                'value' => $environment,
            ),
        ),
    }
    
    Container::make( 'theme_options', 'PayPal Settings' )
        ->add_fields( array(
            Field::make( 'select', 'paypal_environment', 'Choose Environment' )
                ->add_options( array(
                    'live'    => 'Live',
                    'sandbox' => 'Sandbox',
                ) ),
    
            Field::make( 'text', 'paypal_live_clientid', 'Live Client ID' )
                ->set_conditional_logic( crb_environment_conditional_logic( 'live' ) ),
            Field::make( 'text', 'paypal_live_secret_key', 'Live Secret Key' )
                ->set_conditional_logic( crb_environment_conditional_logic( 'live' ) ),
    
            Field::make( 'text', 'paypal_sandbox_clientid', 'Sandbox Client ID' )
                ->set_conditional_logic( crb_environment_conditional_logic( 'sandbox' ) ),
            Field::make( 'text', 'paypal_sandbox_secret_key', 'Sandbox Secret Key' )
                ->set_conditional_logic( crb_environment_conditional_logic( 'sandbox' ) ),
        ) );

    So, basically if you change the Environment, only the associated Credentials options will be displayed. However, if the Conditional Logic stores only the visible fields in Database, the Administrator will have to update the fields every time He wants to test in Sandbox and again when switching to Live, which will be inconvenient.

    In addition, you will still have to perform a check in your code to get the correct Environment Credentials. Not storing the invisible options it Database won’t affect your code.

    —-

    However, given your example, it makes more sense to use two Complex Groups instead of conditional logic. This will look like this:

    Container::make( 'theme_options', 'Plugin Roles Options' )
        ->set_page_parent('Plugin Options');
        ->add_fields( array(
            Field::make( 'complex', 'role_add_field', 'Role Fields' )
                ->add_fields( 'Text', array(
                    Field::make('text', $field_code . 'label', 'Label'),
                    Field::make('text', $field_code . 'code', 'Code Name')->set_required(true),
                    Field::make('text', $field_code . 'placeholder', 'Placeholder Value'),
                    Field::make('text', $field_code . 'default_value', 'Default Value'),
                ) )
                ->add_fields( 'Select', array(
                    Field::make('text', $field_code . 'label', 'Label'),
                    Field::make('text', $field_code . 'code', 'Code Name')->set_required(true),
                    Field::make('complex', $field_code .'options')->add_fields(array(
                        Field::make('text', $field_code . 'option_key', 'Key')->set_width(50)->set_required(true),
                        Field::make('text', $field_code . 'option_value', 'Value')->set_width(50)->set_required(true),
                ) ),
        ) )

    For more information about Multiple Complex Groups, please head to the Documentation – https://carbonfields.net/docs/complex-field-multiple-groups/

    Let us know if this solves your issue.

    Thread Starter martnick

    (@martnick)

    Thanks for the fast response, I will play around with this, and will contact you as soon as possible on my progress. Cheers.

    Thread Starter martnick

    (@martnick)

    Hey guys, sorry for the late response. Everything is great now. Thank you once again!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Complex field with relations, adding extra rows to db’ is closed to new replies.