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

How to assign columns to repeatable groups #27

Open
ShariqKhan2012 opened this issue Jul 18, 2016 · 1 comment
Open

How to assign columns to repeatable groups #27

ShariqKhan2012 opened this issue Jul 18, 2016 · 1 comment

Comments

@ShariqKhan2012
Copy link

I was just wondering, is it possible to assign columns to repeatable groups ?

I did something like this:

            $prefix      = '_group_columns_test';
            $cmb_group   = new_cmb2_box( array(
                'id'             => $prefix . 'metabox',
                'title'          => __( 'Repeating Field Group using a Grid', 'cmb2' ),
                'object_types'   => array( 'page' ),
            ) );
            // $group_field_id is the field id string, so in this case: $prefix . 'demo'
            $group_field_id  = $cmb_group->add_field( array(
                'id'         => $prefix . 'demo',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number.
                    'add_button'     => __( 'Add Another Entry', 'cmb2' ),
                    'remove_button'  => __( 'Remove Entry', 'cmb2' ),
                    'sortable'       => true,
                ),
            ) );
            $gField1         = $cmb_group->add_group_field( $group_field_id, array(
                'name'   => __( 'Entry Title', 'cmb2' ),
                'id'     => 'title',
                'type'   => 'text',
            ) );
            $gField2         = $cmb_group->add_group_field( $group_field_id, array(
                'name'           => __( 'Description', 'cmb2' ),
                'description'    => __( 'Write a short description for this entry', 'cmb2' ),
                'id'             => 'description',
                'type'           => 'textarea_small',
            ));

            $cmb2Grid = new \Cmb2Grid\Grid\Cmb2Grid( $cmb_group );
            // Create now a Grid of group fields.
            $cmb2GroupGrid   = $cmb2Grid->addCmb2GroupGrid( $group_field_id );

            $row = $cmb2Grid->addRow();
            $row->addColumns( array(
                $cmb2GroupGrid
            ) );

This way the group comes up fine, but it takes up full width.

If I change the last line to :

            $row->addColumns( array(
                array( $cmb2GroupGrid, 'class' => 'col-md-6' ),
            ) );

then it gives me fatal error:

Fatal error: Cannot use object of type Cmb2Grid\Grid\Group\Cmb2GroupGrid as array

Now if I add another group to make my code look like this:

            $prefix      = '_group_columns_test';
            $cmb_group   = new_cmb2_box( array(
                'id'             => $prefix . 'metabox',
                'title'          => __( 'Repeating Field Group using a Grid', 'cmb2' ),
                'object_types'   => array( 'page' ),
            ) );
            // $group_field_id is the field id string, so in this case: $prefix . 'demo'
            $group_field_id  = $cmb_group->add_field( array(
                'id'         => $prefix . 'demo',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number.
                    'add_button'     => __( 'Add Another Entry', 'cmb2' ),
                    'remove_button'  => __( 'Remove Entry', 'cmb2' ),
                    'sortable'       => true,
                ),
            ) );
            $gField1         = $cmb_group->add_group_field( $group_field_id, array(
                'name'   => __( 'Entry Title', 'cmb2' ),
                'id'     => 'title',
                'type'   => 'text',
            ) );
            $gField2         = $cmb_group->add_group_field( $group_field_id, array(
                'name'           => __( 'Description', 'cmb2' ),
                'description'    => __( 'Write a short description for this entry', 'cmb2' ),
                'id'             => 'description',
                'type'           => 'textarea_small',
            ));



            $group_field_id2     = $cmb_group->add_field( array(
                'id'         => $prefix . 'demo2',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __( 'DEntry {#}', 'cmb2' ), // {#} gets replaced by row number.
                    'add_button'     => __( 'Add Another DEntry', 'cmb2' ),
                    'remove_button'  => __( 'Remove Entry', 'cmb2' ),
                    'sortable'       => true,
                ),
            ) );
            $gField12        = $cmb_group->add_group_field( $group_field_id2, array(
                'name'   => __( 'DEntry Title', 'cmb2' ),
                'id'     => 'title2',
                'type'   => 'text',
            ) );
            $gField22        = $cmb_group->add_group_field( $group_field_id2, array(
                'name'           => __( 'Description', 'cmb2' ),
                'description'    => __( 'Write a short description for this entry', 'cmb2' ),
                'id'             => 'description2',
                'type'           => 'textarea_small',
            ));


            if ( ! is_admin() ) {
                return;
            }
            // Create a default grid.
            $cmb2Grid = new \Cmb2Grid\Grid\Cmb2Grid( $cmb_group );
            // Create now a Grid of group fields.
            $cmb2GroupGrid   = $cmb2Grid->addCmb2GroupGrid( $group_field_id );      

            $cmb2GroupGrid2  = $cmb2Grid->addCmb2GroupGrid( $group_field_id2 );

            $row = $cmb2Grid->addRow();
            $row->addColumns( array(
                $cmb2GroupGrid, // Can be $group_field_id also.
                $cmb2GroupGrid2
                //array( $cmb2GroupGrid, 'class' => 'col-md-8' ),
                //array( $cmb2GroupGrid2, 'class' => 'col-md-4' )
            ) );

then the groups come fine in 2 columns.
However, the problem is that if I click on add button to create one more type of the group. then the new instance comes just below it, it does not come to the next available column.

Perhaps a screenshot would explain better:

gridcolumns

@sabbiseaan
Copy link

NO solution here???

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

No branches or pull requests

2 participants