Support » Plugin: CMB2 » Save group fields data separately and repeatable groups styling questions

  • Resolved Alex

    (@flyjam)


    Hi! The more I learn CMB2 the more I like it. Thank you, guys for what you’re doing. I’ve got several questions.

    1) Is it possible to save fields data inside a non-repeatable group separately per each field (not serialized)?
    I’ve tried to use 'multiple' => true but it seems not work for the group.

    And the second question is somehow interralated with the first one.

    2) Is there any way to just visually group the fields inside one metabox? Some sort of a group, but only for visual organization of fields’ blocks.

    3) Can I change repeatable groups styling without css overriding? For example, customize the repeatable group markup or by adding custom classes (globally, not for individual metabox). And probably to change position and style of ‘remove’/’add’ buttons.
    What I’d like is to make more lightweight styling like ACF table layout. If I need to repeat just a group of 2 or 3 fields, that would look organic ^)

    • This topic was modified 6 years, 3 months ago by Alex.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just out of curiosity, have you seen either of these two links yet?

    https://github.com/WebDevStudios/CMB2/wiki

    https://github.com/WebDevStudios/CMB2-Snippet-Library

    Some things in them may help out on a number of areas, but feel free to respond here in case none seem to. We can reply in more depth at that time.

    Thread Starter Alex

    (@flyjam)

    Hi Michael,

    Sure, I’ve been at both links and looked into the issues as well. My questions are more specific to the group fields.
    Can’t find answers for #1

    For #2:
    I thought I could use groups for just visually sorting the fields, but if I can’t choose how to save data (separetely or serialized), this is probably not suitable.

    As per my question #3:
    I found that group rendered by render_group_row function in CMB.php. Unfortunetly I can’t find any filter I can use to change the group’s output.
    With render_row_cb as far as I see I can’t control it.

    Only the way – a custom field. Yes, I used this solution to combine some fields. But I thought there might be more easy way to choose and control group field, that would speed things up out of the box. Some ideas on that have been discussed in the topic at https://github.com/WebDevStudios/CMB2/issues/364.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you share your current CMB2 config code? Would love to tinker from the same base that you’re working from to see if I can figure anything out that’d help here.

    Thread Starter Alex

    (@flyjam)

    Non-repeatable group:

    
    ...
              $prefix = '_ao_post';
    
              $group_pro = $cmb->add_field( array(
    		'id'          => $prefix . 'pro_info',
    		'type'        => 'group',
    		'options'     => array(
    			'group_title'   => __( 'Professional details', 'cmb2' ), 
    		),
                    'repeatable' => false,
    	) );
            $cmb->add_group_field($group_pro, array(
    	    //'before_field' => __( 'Headline', 'cmb2' ),
    	    'name'             => 'Headline',	  
    	    'id'   => $prefix . 'headline',
    	    'type' => 'text',
    	) );
    
    	$cmb->add_group_field($group_pro, array(
    	    'name' => 'Position',
    	    'id'   => $prefix . 'position',
    	    'type' => 'text'
    	) );
       ...
          /* Other group fields goes  below */
    

    1) Is it possible to save fields within a group separetely? Now they all saved as one meta key in serialized array.
    2) Can I anyway change a group markup/layout (including remove and add buttons)? (it’s not about field/row markup)

    Plugin Author Justin Sternberg

    (@jtsternberg)

    If you’re just looking to add a wrap around certain fields, you can use the before/after callback field parameters. Here is a working example that will wrap the 3 fields in a metabox-like wrap:

    
    $advanced_open = '
    <div class="advanced-toggle advanced-toggle-wrap postbox closed">
    <button type="button" class="handlediv button-link" aria-expanded="true"><span class="screen-reader-text">Click to toggle</span><span class="toggle-indicator" aria-hidden="true"></span></button>
    <h3 class="hndle ui-sortable-handle"><span>Toggle Advanced Options</span></h3>
    <div class="inside">
    ';
    $advanced_close = '</div></div>';
    
    $cmb->add_field( array(
    	'before_row' => $advanced_open,
    	'name' => esc_html__( 'Test Text', 'cmb2' ),
    	'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
    	'id'   => $prefix . 'text',
    	'type' => 'text',
    ) );
    
    $cmb->add_field( array(
    	'name' => esc_html__( 'Test Text Small', 'cmb2' ),
    	'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
    	'id'   => $prefix . 'textsmall',
    	'type' => 'text_small',
    ) );
    
    $cmb->add_field( array(
    	'name' => esc_html__( 'Test Text Medium', 'cmb2' ),
    	'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
    	'id'   => $prefix . 'textmedium',
    	'type' => 'text_medium',
    	'after_row' => $advanced_close,
    ) );
    
    Plugin Author Justin Sternberg

    (@jtsternberg)

    Thread Starter Alex

    (@flyjam)

    Cool. That’s one I needed. Thank you!

    And as far as I know only way for now to change Repeatable Groups Layout – only with css which is a limit over design or with custom fields (like address field in examples).
    The goal is to have more lightweight look like ACF has (table layout) http://uploadpie.com/2BWfx (screenshot).

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    For what it’s worth at the moment, you can add any CSS you want to the admin area and have it override CMB2’s styling. If you want to do it via admin_enqueue_scripts hook, you could use one of these as a dependency so yours is loaded last:

    cmb2-styles
    cmb2-display-styles
    

    Otherwise, a <style> tag inside admin_head hook would work as well.

    Just trying to offer some ideas of how to approach this, since you’re wanting to add your own custom styling.

    Thread Starter Alex

    (@flyjam)

    I’ve added my custom styles, though can’t get full control over layout.. Went this way.
    Ok. Thanks for helping.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Let us know if you need anything else, we’ll do the best to provide direction.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Yah, to get that style, you’re probably going to have to do some hacky things, or be comfortable with diving into the CMB2 internals to see how it handles outputting things (and replacing with your own).

    Thread Starter Alex

    (@flyjam)

    Yup, I’m going to this and learn more about cmb2, for now just need a easy-go solution.
    Thank you, guys!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Save group fields data separately and repeatable groups styling questions’ is closed to new replies.