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

Using Mulitple on post select shows multiple selections on edit #428

Open
Arcath opened this issue Mar 27, 2017 · 11 comments
Open

Using Mulitple on post select shows multiple selections on edit #428

Arcath opened this issue Mar 27, 2017 · 11 comments
Labels
Milestone

Comments

@Arcath
Copy link

Arcath commented Mar 27, 2017

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.

@mikeselander
Copy link
Contributor

mikeselander commented Mar 27, 2017

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

@Arcath
Copy link
Author

Arcath commented Mar 27, 2017

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

@mikeselander
Copy link
Contributor

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 mikeselander self-assigned this Mar 27, 2017
@mikeselander mikeselander added this to the 1.1 milestone Mar 27, 2017
@Arcath
Copy link
Author

Arcath commented Mar 27, 2017

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

@mikeselander
Copy link
Contributor

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

@Arcath
Copy link
Author

Arcath commented Mar 28, 2017

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

@johnnny-bravoo
Copy link

@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

@mikeselander
Copy link
Contributor

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

@swissspidy
Copy link
Contributor

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.

@Arcath
Copy link
Author

Arcath commented Feb 5, 2018

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

@swissspidy
Copy link
Contributor

@Arcath Use something like this:

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

    return $fields;
}

@mikeselander mikeselander removed their assignment Dec 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants