• Resolved kblairspark

    (@kblairspark)


    I’m using a CMB2’s repeatable fields to keep track of materials I need to run an activity at an event.

    I want to be able to add 3 items to the repeatable field automatically if the post I’m working on has a specific value as a custom field. I was trying to do it using the save_post hook, but the function I added to it fires before CMB updates the meta values.

    Is there a way I can add a set of default values to a repeatable group field? I tried adding a callback funtion to default on the GROUP field, but I’m not sure if group fields actually do anything with the default field parameter (it didn’t seem to).

    Now I’m investigating doing it with the updated_post_meta / added_post_meta hook, is that probably the best way?

    https://ww.wp.xz.cn/plugins/cmb2/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    If you want assistance, it’s always best if you provide your code, even if it’s not working. Especially your cmb box/field registration code. Alternatively, i’ll use a dummy example and you’ll have to make it work w/ your actual ids/fields/etc.

    You can hook into the after_save hooks: https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2.php#L606-L640

    Assuming your cmb/field registration looks like:

    function yourprefix_register_repeatable_group_field_metabox() {
    	/**
    	 * Repeatable Field Groups
    	 */
    	$cmb = new_cmb2_box( array(
    		'id'           => 'yourprefix_group_metabox',
    		'title'        => __( 'Repeating Field Group', 'cmb2' ),
    		'object_types' => array( 'page', ),
    	) );
    
    	$cmb->add_field( array(
    		'name'             => __( 'Radio', 'cmb2' ),
    		'id'               => 'yourprefix_group_radio',
    		'type'             => 'radio',
    		'options' => array(
    			'standard' => __( 'Option One', 'cmb2' ),
    			'custom'   => __( 'Option Two', 'cmb2' ),
    			'none'     => __( 'Option Three', 'cmb2' ),
    		),
    	) );
    
    	$group_field_id = $cmb->add_field( array(
    		'id'          => 'yourprefix_group_demo',
    		'type'        => 'group',
    		'description' => __( 'Generates reusable form entries', 'cmb2' ),
    		'options'     => array(
    			'group_title'    => __( 'Entry {#}', 'cmb2' ),
    			'add_button'     => __( 'Add Another Entry', 'cmb2' ),
    			'remove_button'  => __( 'Remove Entry', 'cmb2' ),
    			'sortable'       => true,
    		),
    	) );
    
    	$cmb->add_group_field( $group_field_id, array(
    		'name' => 'Entry Title',
    		'id'   => 'title',
    		'type' => 'text',
    	) );
    
    	$cmb->add_group_field( $group_field_id, array(
    		'name'        => 'Description',
    		'description' => 'Write a short description for this entry',
    		'id'          => 'description',
    		'type'        => 'textarea_small',
    	) );
    
    }
    add_action( 'cmb2_init', 'yourprefix_register_repeatable_group_field_metabox' );

    You could hook in to the save hook and do something like:

    function hook_in_and_add_default_group_value( $post_id, $updated, $cmb ) {
    	// If 'yourprefix_group_radio' was updated, then proceed w/ your stuff.
    	if ( in_array( 'yourprefix_group_radio', $updated ) ) {
    		if ( 'standard' === get_post_meta( $post_id, 'yourprefix_group_radio', 1 ) ) {
    			// do stuff
    			update_post_meta( $post_id, 'yourprefix_group_demo', array(
    				array(
    					'title'       => 'Title of group 1',
    					'description' => 'Description of group 1',
    				)
    			) );
    		}
    	}
    }
    add_action( 'cmb2_save_post_fields_yourprefix_group_metabox', 'hook_in_and_add_default_group_value', 10, 3 );

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Thread Starter kblairspark

    (@kblairspark)

    Thank you so much! I was mostly asking for an approach, rather than code, which is why I didn’t post mine. Especially because right now it’s not really even trying to do what I want, and I will be changing it based on this (not the metabox, but all the code updating the values).

    This example is super helpful, I will try it out, and i will come back and post code if it doesn’t work as expected.

    Is there anywhere the hooks for CMB2 are documented, outside of the code? I couldn’t find it in the wiki.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This should provide at least a consolidated list of available hooks, with code references http://cmb2.io/api/hook-docs.html

    Thread Starter kblairspark

    (@kblairspark)

    Thanks! This worked great. I ended up using updated_post_meta to hook on to, since the trigger custom field was not a CMB2 field.

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

The topic ‘Repeatable Field – default values’ is closed to new replies.