mustardBees / cmb-field-select2

Select2 field type for Custom Metaboxes and Fields for WordPress
93 stars 46 forks source link

Select Box no longer working with CMB 2.2.2.1 #33

Closed Shaatarn closed 8 years ago

Shaatarn commented 8 years ago

Hi Phil,

Yesterday I updated CMB2 to version 2.2.2.1 and it broke the pw_select drop down. I am doing two different callbacks to populate these drop-downs and neither of them are now working. The first grabs a list of all published posts and the second grabs a list from an array in a PHP settings file. I have pasted the code below. I also updated this plugin to 3.0.0, but no joy. Can you take a look and let me know a fix for his please.

Get the post list:

`

    /**
     * Callback to populate the list of articles in the related artciles drop downs
     *
     * @since     1.0.0
     */
    function get_articles_for_select() {

        $args = array(
            'post_type' => 'post',
            'posts_per_page'    => -1,
            'order' => 'ASC',
            'orderby'   => 'title',
        );

        $posts = get_posts( $args );

        $post_options = array();
        if ( $posts ) {
            foreach ( $posts as $post ) {
                $post_options[ $post->ID ] = $post->post_title;
            }
        }

        return $post_options;

    }

}

`

and to get the array from the settings file:

`

       $cmb_mobilebanner->add_field( array(
        'name'       => __( 'Mobile Banner to Display', 'cmb2' ),
        'desc'       => __( 'Show this Mobile Banner in the article with the [mobilebanner] shortcode', 'cmb2' ),
        'id'         => $this->prefix . 'mb_banner_name',
        'type'       => 'pw_select',
        'options_cb' => 'get_mobile_banners_for_select',
        'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
    ) );

    function get_mobile_banners_for_select() {

        $mbSettings = new Uplift_Banner_Ad_Admin_Settings ();
        return $mbSettings->get_mobile_banner ('', true);

    }

`

which calls:

`

               $this->mobileBanners = array (
        'Yoga Day Sumit 2016' => array (
                'show' => true,
                'click_url' => 'http://yogadaysummit.com/',
                'click_new_window' => true,
                'desktop_image_url' => 'http://upliftconnect.com/wp-content/uploads/2016/06/YDSbannerArticlesDesktopAFTER.jpg',
                'image_url' => 'http://upliftconnect.com/wp-content/uploads/2016/06/YogaSummitBannerInArticleAFTER-1.jpg',
                'show_text' => false,
                'text' => '',
                'text_size' => 16,
                'text_colour' => '#fff',
                'back_colour' => '#75872b', // #003284
        ),
        'Water is Sacred' => array (
                'show' => true,
                'click_url' => 'http://blessthewater.com/bless-the-water-video/',
                'click_new_window' => true,
                'desktop_image_url' => 'http://upliftconnect.com/wp-content/uploads/2016/05/waterIsSacredMobileBanner.png',
                'image_url' => 'http://upliftconnect.com/wp-content/uploads/2016/05/waterIsSacredMobileBanner.png',
                'show_text' => false,
                'text' => '',
                'text_size' => 16,
                'text_colour' => '#fff',
                'back_colour' => '#003284',
        ),
    );

public function get_mobile_banner ($bannerKey = '', $keysOnly = false) {

    if ($bannerKey !== '') {

        return $this->mobileBanners[$bannerKey];

    } else {

        if ($keysOnly) {

            $dropDownKeys = [];
            foreach ($this->mobileBanners as $key=>$value) {
                $dropDownKeys[$key] = $key;
            }
            return $dropDownKeys;

        } else {

            return $this->mobileBanners;

        }

    }

}

`

I have downgraded to CMB2 v2.2.1 and Select2 v2.0.4 and the above code works fine.

Love the plugin by the way, it has been working super well for ages now, so I am not sure what has changed in CMB2 to break it.

Thanks in advance! Shaa.

alfredomirick commented 8 years ago

Hi,

I had the same issue, fixed it with a little trick :


add_action('cmb2_init', 'foo_register_metaboxes');
function foo_register_metaboxes()
{
    $select2_obj = new PW_CMB2_Field_Select2();
    $select2_obj->setup_admin_scripts();

    $cmb = new_cmb2_box([
        'id' => 'foo-metabox',
        'title' => 'foo',
        'object_types' => ['post'],
        'context' => 'normal',
        'priority' => 'high',
        'show_names' => true,
    ]);

    $cmb->add_field([
        'name' => 'Foo Field',
        'id' =>  'foo_field',
        'type' => 'select',
        'repeatable' => true,
        'attributes' => ['class' => 'pw_select2 pw_select'],
        'options' => ['','x','y','z']
    ]);

    // ...... blah, blah, blah
}

Although it's not the solution we are looking for! I hope the problem be solved in the next versions.

Thank you all A. Mirick

pablo-sg-pacheco commented 8 years ago

Same problem here. Version 2.0.4 works without problems

jondcampbell commented 8 years ago

Same problem. Not sure how to apply the fix from @alfredomirick to the multi-select field.

jondcampbell commented 8 years ago

Figured out getting it to work on a multiselect

$cmb_box->add_field( array(
        'name'    => 'Featured Authors(Users)',
        'id'      => $prefix . 'featured_authors',
        'desc'    => 'Select users. Drag to reorder.',
        'type'    => 'select',
        'attributes' => [
            'class'     => 'pw_select2 pw_multiselect',
            'multiple'  => 'multiple',
            'style'     => 'width: 99%',
        ],
        'options_cb' => 'custom_get_users_cb',
    ) );

Sadly it seems like the drag and drop reordering was removed from select2 version 4.* Guess I should just use the older version of this field type because I need reordering.

boneus commented 8 years ago

To get this to work you need to add this lines to functions "render_pw_select" and "render_pw_multiselect" in cmb-field-select2.php file after $this->setup_admin_scripts():

    if ( version_compare( CMB2_VERSION, '2.2.2', '>=' ) ) {
        $field_type_object->type = new CMB2_Type_Select( $field_type_object );
    }
gyrus commented 8 years ago

Yep, completely broken with CMB2 v2.2.2.1 + Select2 v3.0.0. None of the hacks work for me - any news on a proper fix? Have downgraded for now.

mustardBees commented 8 years ago

Can you test the field with version 3.0.1 and let me know if you have any further trouble.

gyrus commented 8 years ago

That seems to work, thanks. Maybe an issue with the width but I think that's my custom theming. Functionally good.

mustardBees commented 8 years ago

@gyrus Where are you using the field? I noticed the width was off when used on the user profile editor. For now you can try passing through a min-width like so:

$team_meta_box->add_field( array(
    'name'       => __( 'Team', 'iweb' ),
    'id'         => $prefix . 'team',
    'type'       => 'pw_select',
    'options'    => iweb_get_cmb_options_array( array( 'post_type' => 'team' ) ),
    'attributes' => array(
        'style' => 'min-width: 400px;',
    ),
) );
gyrus commented 8 years ago

It's on a post edit screen. Having to manually pass an inline style each time seems a bit sub-optimal, as they say. Actually I guess the width is technically the width of the longest term, but the 'x' isn't accounted for. You can read all the terms when you see the drop-down, but the selected term isn't completely readable if it's the longest (see screenshot).

screenshot

Maybe this is more about simply allowing a bit more space there?

mustardBees commented 8 years ago

I see what you mean. This feels like it needs raising upstream with Select2. In the meantime, if this can be fudged with CSS, there is already a CSS file included with this field which overrides a few Select2 properties. I'd be happy to accept a PR if you fancy working on this.

gyrus commented 8 years ago

Cool - obviously not critical so I'll keep it pending and hopefully have a look soon :)

Shaatarn commented 8 years ago

Awesome, thanks for sorting this issue out Phil.