Skip to content

Custom field Types that are repeatable are not shown properly. #901

@bnecreative

Description

@bnecreative

Prior to v2.2.4, a custom field that has more than one field would output normally in a repeatable set. The new custom field is built similarly to the address field that is in the CMB2 code library. It comprises of two fields - 1) text 2) textarea.

Expected Behavior:

Prior to v2.2.4, a custom field that has more than one field would output normally in a repeatable set.

Image: https://www.dropbox.com/s/2qqgssl9w1cwin0/people-before224.png?dl=0

Actual Behavior:

Since v2.2.4, only the second field of the set is added in a repeatable set after clicking the add row button. In addition, the surrounding html markup of the 2 fields are not included in the new row.

Image: https://www.dropbox.com/s/b3ofkh2sninevi2/people224.png?dl=0

Steps to reproduce (I have confirmed I can reproduce this issue on the trunk branch):

  1. Add a normal cmb2 box with a custom field
  2. Add needed functions to build out the field

CMB2 Field Registration Code:

add_action( 'cmb2_admin_init', 'yourprefix_register_demo_metabox' );
function yourprefix_register_demo_metabox() {

	$cmb = new_cmb2_box( array(
		'id'            =>	'bne_people_details',
		'title'         =>  __( 'Profile Details', 'bne' ),
		'object_types'  =>  array( 'post-type-name' ),
		'classes'		=>	'bne-cmb-wrapper'
	) );


	// Field - Extra Content
	$cmb->add_field( array(
		'name'    		=>	__( 'name', 'bne' ),
		'desc'    		=>	__( 'description', 'bne' ),
		'id'      		=>	'details',
		'type'    		=>	'bne_people_details_repeater',
		'repeatable'	=>	true,
		'text'			=>	array(
			'add_row_text'	=>	__( 'Add Detail', 'bne' )
		)
	) );
}
function bne_people_details_field( $field, $value, $object_id, $object_type, $field_type ) {

	// Make sure we specify each part of the value we need.
	$value = wp_parse_args( $value, array(
		'label'		=>	'',
		'content'	=>	'',
	) );

	// Label
	echo '<div class="col-4">';
		echo '<div class="bne-field-option clearfix">';
			echo '<label>'.__( 'Label', 'bne' ).'</label>';
			echo $field_type->input( array(
				'name'    		=>	$field_type->_name( '[label]' ),
				'id'      		=>	$field_type->_id( '_label' ),
				'value' 		=> 	$value['label'],
				'desc'    		=> 	'',
				'placeholder'	=>	'ex: Email',
			) );
		echo '</div>';
	echo '</div>';

	// Content
	echo '<div class="col-8">';
		echo '<div class="bne-field-option clearfix">';
			echo '<label>'.__( 'Content', 'bne' ).'</label>';
			echo $field_type->textarea( array(
				'name'    		=>	$field_type->_name( '[content]' ),
				'id'      		=>	$field_type->_id( '_content' ),
				'value' 		=> 	$value['content'],
				'desc'    		=> 	'',
				'rows'			=>	'1',
				'placeholder'	=>	'ex: [email protected]',
			) );
		echo '</div>';
	echo '</div>';

	// Field Description
	echo $field_type->_desc( true );

}
add_filter( 'cmb2_render_bne_people_details_repeater', 'bne_people_details_field', 10, 5 );

/*
 * 	The following snippets are required for allowing the fields above
 * 	to work as a repeatable field, or in a repeatable group in CMB2.
 *
*/
function bne_people_cmb2_sanitize_field( $check, $meta_value, $object_id, $field_args, $sanitize_object ) {
	// if not repeatable, bail out.
	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 );
}
add_filter( 'cmb2_sanitize_bne_people_details_repeater', 'bne_people_cmb2_sanitize_field', 10, 5 );

function bne_people_cmb2_types_esc_field( $check, $meta_value, $field_args, $field_object ) {
	// if not repeatable, bail out.
	if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) {
		return $check;
	}
	foreach ( $meta_value as $key => $val ) {
		if( $val )
		$meta_value[ $key ] = array_filter( array_map( 'esc_attr', $val ) );
	}
	return array_filter( $meta_value );
}
add_filter( 'cmb2_types_esc_bne_people_details_repeater', 'bne_people_cmb2_types_esc_field', 10, 4 );

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions