Custom-Meta-Boxes icon indicating copy to clipboard operation
Custom-Meta-Boxes copied to clipboard

Using Mulitple on post select shows multiple selections on edit

Open Arcath opened this issue 7 years ago • 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 avatar Mar 27 '17 15:03 Arcath

@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 avatar Mar 27 '17 15:03 mikeselander

@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.

Arcath avatar Mar 27 '17 15:03 Arcath

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 avatar Mar 27 '17 16:03 mikeselander

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

Arcath avatar Mar 27 '17 20:03 Arcath

@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 avatar Mar 27 '17 20:03 mikeselander

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

Arcath avatar Mar 28 '17 07:03 Arcath

@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

johnnny-bravoo avatar May 10 '17 09:05 johnnny-bravoo

@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!

mikeselander avatar May 15 '17 17:05 mikeselander

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 avatar Nov 28 '17 12:11 swissspidy

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

Arcath avatar Feb 05 '18 12:02 Arcath

@Arcath Use something like this:

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

    return $fields;
}

swissspidy avatar Feb 06 '18 14:02 swissspidy