Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bnecreative opened this issue Mar 27, 2017 · 13 comments
Closed

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

bnecreative opened this issue Mar 27, 2017 · 13 comments
Labels

Comments

@bnecreative
Copy link

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: mail@example.com',
			) );
		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 );
@bnecreative bnecreative changed the title Custom fields that are repeatable are not shown properly. Custom field Types that are repeatable are not shown properly. Mar 31, 2017
@bradp bradp added the bug label Apr 3, 2017
@RubenMartins
Copy link
Contributor

Any update on this?
I'm also experiencing this with 4 custom fields and only the 4th is added to a new line of repeatable field.

@tw2113
Copy link
Contributor

tw2113 commented Apr 12, 2017

No word that I've seen, but I also missed seeing this one.

@RubenMartins are you also do your own custom field type like the issue opener is?

@tw2113
Copy link
Contributor

tw2113 commented Apr 13, 2017

git bisect shows that this commit is what broke the code above in the original posting: 410cee4

@tw2113
Copy link
Contributor

tw2113 commented Apr 13, 2017

Based on further testing, the __call() method is hit twice, and houses this line 410cee4#diff-32fc356ab519c81b4a672288be3a1e46R61

For the first hit, $object returns null, and doesn't execute render. However the second one does, with the $object be of type CMB2_Type_Textarea

@jtsternberg Any light on this one? You're more aware of the change involved than I am.

@RubenMartins
Copy link
Contributor

RubenMartins commented Apr 13, 2017

@tw2113 yes I created an custom field with cmb2_render_ function witch is repeatable. And prior to the version 2.2.4 this works fine but with this version this bug appeared.

@RubenMartins
Copy link
Contributor

RubenMartins commented May 2, 2017

Any update on this @jtsternberg ?

@tw2113
Copy link
Contributor

tw2113 commented May 2, 2017

Nothing I've personally seen. @jtsternberg Re-pinging you for some possible feedback.

@desrosj
Copy link
Contributor

desrosj commented May 5, 2017

I am also encountering this. A custom field added in the same manner described (cmb2_render_{$field_type}) is not properly adding a new row.

@desrosj
Copy link
Contributor

desrosj commented May 8, 2017

@bnecreative I noticed in the code that you provided, you have three filters. I believe that your cmb2_render_bne_people_details_repeater hook should be an action, not a filter.

I have opened a pull request that fixes the issue as I saw it happening. Any testing is appreciated!

@tw2113
Copy link
Contributor

tw2113 commented May 8, 2017

PR mentioned: #969

@bnecreative
Copy link
Author

@desrosj add_filter was used in the initial wiki example of making an address field which is why I used it; regardless, both works. This PR, changing the return to false, does works on my end using trunk. I'm not sure if there is a side effect to that change for other scenarios.

@desrosj
Copy link
Contributor

desrosj commented May 10, 2017

@bnecreative Gotcha! Was worth double checking. I am somewhat new to CMB2, so some older code examples may not stand out as such to me.

Thanks for confirming that the PR fixes the issue for you. I am going to work on some unit tests for the pull request to prove it does what it's supposed to without any ill effects and hopefully we can get this fixed.

@sakutasanen
Copy link

I also encountered this bug. I would appreciate if this would be fixed soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants