jcchavezs / cmb2-conditionals

Plugin to relate fields in a CMB2 metabox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with

mac2net opened this issue · comments

Hi

I added this to a plugin.
No problem using this with a normal CPT, but the conditionals don't seem to work when added to the options-and-settings-pages snippet.

Any ideas?
Thanks
Mike

If you look at the code you referred to, you'll see that it enqueues the CMB2 CSS. This is not done automatically for options pages.

The same needs to be done for the CMB2 conditionals javascript. There currently isn't a great way to do this other than enqueueing the script yourself (mind the path!):

wp_enqueue_script('cmb2-conditionals', plugins_url('/cmb2-conditionals.js', 'path/to/CMB2_conditionals/file'), array('jquery'), '1.0.2', true);

FYI: I have a PR in the works which will make this a lot easier.

Thanks!
No problem doing this.
I already added tabs to the option page and fiddled with some CSS to get the same spacing.
Since I am all in on CMB2, I guess I'll print out the code and read i over a ☕ verkeerd.

;-)

Unfortunately I am pretty sure it still doesn't work.
To reduce variables I used just the sample conditional metaboxes and loaded the JS w/o restrictions.
I just had to make one change using the existing ID w/o the prefix and also the options page action.

In the source, the conditional boxes all had the proper attributes. The CSS and JS were loaded properly
And on the page, everything displayed regardless of conditions.

Now it's working!
I am going to have to read the code.
In my plugin I enqueued the JS and gave it a different ID from the from the enqueue in cmb2-conditionals.php. Even though it wasn't loading from cmb2-conditionals.php it was still using that ID.
So when I changed my enqueue to that ID it worked.
I had previous cleared the browser cache so that's not the source of the problem.
Transients perhaps.

I am going to have to read the code.

You may want to have a look at PR #17 as it contains some pretty big changes for CMB2-conditionals. Also, once that's merged, I'll address #16, so the code is going to be undergoing some large changes in the near future ;-)

BTW I added the CPT label to the multicheck display. I also added tabs and fiddled a bit with the CSS to get that "genuine WordPress options page look and feel".

But, frankly, the first code I wrote came out on punch cards and my PHP kind of tells the same story.

But, frankly, the first code I wrote came out on punch cards and my PHP kind of tells the same story.

Not sure what you mean - can you explain ?

It works but it doesn't look pretty.
Beautiful Code

Once it works, making it pretty is generally not that hard ;-)

It also has to be correct!
This is the multicheck code for CPTs w/labels.
I have only just started with the Settings API.
Most of what I have been doing is working with CPTs (which is why I noticed it wasn't using the label).

add_action( 'cmb2_render_multicheck_posttype', 'ds_cmb_render_multicheck_posttype', 10, 5 );

    function ds_cmb_render_multicheck_posttype( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
        $my_post_types = get_post_types( array (
        'show_ui' => true,
        'show_in_menu' => true,
        ), 'objects' );
        unset( $my_post_types[ 'attachment' ] );
        $cpts = apply_filters( 'multicheck_posttype_' . $field->args[ '_id' ], $my_post_types );
        $options = '';
        $i = 1;
        $values = (array) $escaped_value;

        if ( $cpts ) {
            foreach ( $cpts as $cpt ) {
                $args = array(
                    'value' => $cpt->name,
                    'label' => $cpt->label,
                    'type' => 'checkbox',
                    'name' => $field->args['_name'] . '[]', //wasn't sure if this is redundant with 'value'??
                );

                if ( in_array( $cpt->name, $values ) ) {
                    $args[ 'checked' ] = 'checked';
                }
                $options .= $field_type_object->list_input( $args, $i );
                $i++;
            }
        }

        $classes = false === $field->args( 'select_all_button' ) ? 'cmb2-checkbox-list no-select-all cmb2-list' : 'cmb2-checkbox-list cmb2-list';
        echo $field_type_object->radio( array( 'class' => $classes, 'options' => $options ), 'multicheck_posttype' );
    }

`

Looks like you based the code of https://github.com/WebDevStudios/CMB2-Snippet-Library/blob/master/custom-field-types/multicheck_posttype-field_type.php ?
You could send in a PR to fix it in that repo. Could be helpful to others.

Other than that, this is more a CMB2 issue now than a CMB2-conditionals one, so I suggest closing this issue as the original question was answered & you got that working.