Custom repeatable field type missing data when update
-
I have a custom cmb2 field called as pb_info, which is consist of 4 different field abbreviation (select), course code(text), pe point( number), pe hour (number). and i want to use it inside a repeatable group as a group field. my code is work fine until i am inserting 2 rows of data, but it miss after 2nd row that means from 3rd row it missing data. and when i inspect it i saw from 3rd row the id of input field is not changing.
Here is my code :
registering field behaviour :
function cmb2_render_pb_info_field_callback( $field, $value, $object_id, $object_type, $field_type ) { $value = wp_parse_args( $value, array( 'pb_abbreviation' => '', 'pb_crs_cd' => '', 'pe_pnt' => '', 'pe_hr' => '', ) ); ?> <div class="alignleft"><p><label for="<?php echo $field_type->_id( '_pb_abbreviation' ); ?>'">PB Abbreviation</label></p> <?php echo $field_type->select( array( 'name' => $field_type->_name( '[pb_abbreviation]' ), 'id' => $field_type->_id( '_pb_abbreviation' ), 'options' => cmb2_get_abbr_options( $value['pb_abbreviation'] ), 'desc' => '', ) ); ?> </div> <div class="alignleft use_maxlength_validator"><p><label for="<?php echo $field_type->_id( '_pb_crs_cd' ); ?>'">PB Course Code</label></p> <?php echo $field_type->input( array( 'class' => 'cmb_text_small', 'name' => $field_type->_name( '[pb_crs_cd]' ), 'id' => $field_type->_id( '_pb_crs_cd' ), 'value' => $value['pb_crs_cd'], 'maxlength' => 20, 'desc' => '', ) ); ?> </div> <div class="alignleft pb_credit"><p><label for="<?php echo $field_type->_id( '_pe_pnt' ); ?>'">PE Point</label></p> <?php echo $field_type->input( array( 'class' => 'cmb_text_small', 'name' => $field_type->_name( '[pe_pnt]' ), 'id' => $field_type->_id( '_pe_pnt' ), 'value' => $value['pe_pnt'], 'desc' => '', ) ); ?> </div> <div class="alignleft pb_hour"><p><label for="<?php echo $field_type->_id( '_pe_hr' ); ?>'">PE Hour</label></p> <?php echo $field_type->input( array( 'class' => 'cmb_text_small', 'name' => $field_type->_name( '[pe_hr]' ), 'id' => $field_type->_id( '_pe_hr' ), 'value' => $value['pe_hr'], 'desc' => '', ) ); ?> </div> <br class="clear"> <?php echo $field_type->_desc( true ); } add_filter( 'cmb2_render_pb_info', 'cmb2_render_pb_info_field_callback', 10, 5 ); add_filter( 'cmb2_sanitize_pb_info', 'maybe_save_split_pb_values', 12, 4 ); /** * The following snippets are required for allowing the pb_info field * to work as a repeatable field, or in a repeatable group */ add_filter( 'cmb2_sanitize_pb_info','pb_sanitize', 10, 5 ); add_filter( 'cmb2_types_esc_pb_info', 'pb_escape', 10, 4 ); /** * Optionally save the Pb Info values into separate fields */ function pb_sanitize( $check, $meta_value, $object_id, $field_args, $sanitize_object ) { if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { return $check; } foreach ( $meta_value as $key => $val ) { $meta_value[ $key ] = array_filter( array_map( 'sanitize_text_field', $val ) ); } return array_filter($meta_value); } function maybe_save_split_pb_values( $override_value, $value, $object_id, $field_args ) { foreach ($override_value as $key => $data) { if($data['pb_crs_cd'] == '' && $data['pe_hr'] == '' && $data['pe_pnt'] == '' ){ unset($override_value[$key]); } } if ( ! isset( $field_args['split_values'] ) || ! $field_args['split_values'] ) { // Don't do the override return $override_value; } foreach ($value as $key => $data) { if($data['pb_crs_cd'] == '' && $data['pe_hr'] == '' && $data['pe_pnt'] == '' ){ unset($value[$key]); } } $pb_info_keys = array( 'pb_abbreviation', 'pb_crs_cd', 'pe_pnt', 'pe_hr' ); foreach ( $pb_info_keys as $key ) { if ( ! empty( $value[ $key ] ) ) { update_post_meta( $object_id, $field_args['id'] . 'addr_'. $key, sanitize_text_field( $value[ $key ] ) ); } } remove_filter( 'cmb2_sanitize__pb_info', 'pb_sanitize', 10, 5 ); return true; } function pb_escape( $check, $meta_value, $field_args, $field_object ) { if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) { return $check; } foreach ( $meta_value as $key => $val ) { $meta_value[ $key ] = array_filter( array_map( 'esc_attr', $val ) ); } return array_filter($meta_value); } function cmb2_get_abbr_options( $value = false ) { $terms = get_terms( 'professional-body', array( 'hide_empty' => false, ) ); $pb_abbreviation = ''; $meta_key = "abbreviation"; foreach ( $terms as $term ) { $pb_abbreviation .= '<option value="'. $term->term_id .'" '. selected( $value, $term->term_id, false ) .'>'. get_pb_meta( $term->term_id, $meta_key) .'</option>'; } return $pb_abbreviation; } And the field initialization is:$lu = new_cmb2_box( array(
‘id’ => $prefix.’mcptr{#}’,
‘title’ => _(‘Round Informations’),
‘object_types’ => array(‘mcpt’),
‘context’ => ‘normal’,
‘priority’ => ‘high’,
‘show_names’ => true,
));
$group_field_id = $lu->add_field( array(
‘id’ => $prefix.’mcptre’,
‘class’ => $prefix.’mcptgrp’,
‘type’ => ‘group’,
‘repeatable’ => true,
‘options’ => array(
‘group_title’ => __( ‘Rounds {#}’, ‘cmb2’ ),
‘closed’ => false,
‘add_button’ => __( ‘Another Round’, ‘cmb2’ ),
‘remove_button’ => __( ‘Remove Round’, ‘cmb2’ ),
‘sortable’ => true,
),
) );$lu->add_group_field( $group_field_id, array(
‘name’ => ‘PB Info’,
‘id’ => $prefix.’pb_info_field’,
‘type’ => ‘pb_info’,
‘repeatable’ => true,
‘classes’ => ‘pb_lu_info’
) );
`
So how can i fix the problem, i have never mention to do something special at row number 2 or 3, but somehow it take 2 rows of value and after 3rd row it stops changing id.
The topic ‘Custom repeatable field type missing data when update’ is closed to new replies.