humanmade/Custom-Meta-Boxes

Using Mulitple on post select shows multiple selections on edit

Opened this issue · 11 comments

I have a post select field on one of my pages that uses the multiple option.

array(
    'id' => 'home_featured_pages',
    'name' => __('Featured Pages', 'edit2017'),
    'type' => 'post_select',
    'use_ajax' => true,
    'multiple' => true,
    'query' => array(
      'post_type' => 'page'
    )
)

When I edit the page to begin with it works as it should letting me select as many pages as I want.

After saving the page and coming back to edit it I get each page in its own select2 box.

image

All the boxes have the same id/name and only the first one gets saved.

I'm using the latest dev-master code.

@Arcath thanks for taking the time to submit a ticket!

Can I ask why you want multiple multi-select boxes in an edit screen? I'm kind of thinking that we might want to just disable this option for multi-select since it inherently has this option, but if there's a good use case I'm open to finding ways around this bug.

Also, do you mean develop when you say dev-master?

@mikeselander I only want the one select box.

It works as expected with no data letting me add X pages before saving the page.

image

Once I save and come back I get the issue.

If I take out multiple from the field I can't select more than one.

Ah right., I misunderstood you - apologies. I know what's causing this but will take a little bit of time to dig into and find a fix that doesn't mess something else up.

@mikeselander For now would I better using single selections with repeatable? Just thinking then I can deliver the product

@Arcath how soon do you need to deliver this? I might be able to carve out some time later this week since it sounds more urgent to you.

@mikeselander I have plenty of time. We aren't looking at launching for a while yet.

@mikeselander there is also an issue in saving that post_select multiple field. When adding multiple selection and save that post page, it displays each selection in each line like that. image

On updating this post page again, only first single option is selected and the rest options disabled. Is there any quick solution to fix this issue?

Many thanks

@HeartBreakKid58 I don't have an easy off the top of my head answer unfortunately. I just need to get around to fixing this. Thanks for the report!

If anyone's stumbling upon this, I solved it by extending CMB_Post_Select and tweaking $this->values / $this->value. Here's an example:

class PostSelectField extends \CMB_Post_Select {
	/**
	 * Print out a field.
	 */
	public function display() {
		if ( $this->args['use_ajax'] && $this->args['multiple'] ) {
			$this->values = [ implode( ',', $this->values ) ];
		}

		parent::display();
	}

	/**
	 * Output inline scripts to support field.
	 */
	public function output_script() {
		$this->value = explode( ',', $this->value );

		parent::output_script();
	}
}

It works fine for our use case. Perhaps it needs some adjustment for yours.

@swissspidy How do you tell CMB to use this class over CMB_Post_Select?

@Arcath Use something like this:

add_filter( 'cmb_field_types', function( array $fields ) {
    $fields['post_select'] = PostSelectField::class;

    return $fields;
}