Complex field with relations, adding extra rows to db
-
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
The topic ‘Complex field with relations, adding extra rows to db’ is closed to new replies.