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

Add option to store values in individual rows instead of serialized array #262

Merged
merged 1 commit into from Apr 4, 2015

Conversation

JohnyGoerend
Copy link

In previous versions, multicheck fields were stored using multiple key/value entries. This was recently changed (maybe due to a bug) so now those fields got saved as serialized arrays. Unfortunately, that change made it impossible to use the "meta_key" and "meta_value" arguments with the get_posts function.
This patch leaves the possbility to the user with an additional "multiple" option, to store the values as individual rows again.
Fixes #183

(Re-done to correct #225)

@mweimerskirch
Copy link

👍

@natebeaty
Copy link

Just dug around trying to figure out why this wasn't working after upgrading from CMB. +1 for merging this from someone that uses this functionality on a few sites.

@@ -299,8 +299,9 @@ function yourprefix_register_demo_metabox() {
$cmb_demo->add_field( array(
'name' => __( 'Test Multi Checkbox', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'multicheckbox',
'id' => $prefix . 'multicheckbox2',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to rename this

@jtsternberg
Copy link
Member

@JohnyGoerend @mweimerskirch @natebeaty If you all test this patch and confirm it works with and without the multiple option, I'll be able to get this merged much quicker.

@natebeaty
Copy link

I can confirm it as working for me.

I've applied the patch and when I add 'multiple' => true it stores the array of meta values as separate entries properly in the database, and when I comment it out, it goes back to the single entry method (e.g. a:2:{i:0;s:3:"378";i:1;s:2:"25";}).

@mweimerskirch
Copy link

Works perfectly for me as well, just as it used to with CMB1.

@JohnyGoerend
Copy link
Author

@jtsternberg Fixed.

I've already used it on a website which is online since last week.

jtsternberg added a commit that referenced this pull request Apr 4, 2015
Add option to store values in individual rows instead of serialized array. Props @JohnyGoerend
@jtsternberg jtsternberg merged commit 7accb45 into CMB2:trunk Apr 4, 2015
@natebeaty
Copy link

Just curious, should this fix be showing up in 2.0.5 if using WPackagist? I'm including it via Composer in a Bedrock/Sage project, and the multiple code in question is commented out. I also tried using "wpackagist-plugin/cmb2": "dev-trunk" and same thing, the code is commented out.

I can move to including it locally in the theme to get this fix, but I liked having CMB2 easily updatable as a plugin controlled via Composer.

@jtsternberg
Copy link
Member

@natebeaty no, we haven't merged to master yet. I expect to soon.

@natebeaty
Copy link

@jtsternberg ok, thanks for the quick reply. I'll just move CMB2 into my theme for now.

pluginmirror-worker pushed a commit to wp-plugins/cmb2 that referenced this pull request Apr 30, 2015
* New metabox/form parameter, `show_on_cb`, allows you to conditionally display a cmb metabox/form via a callback. The `$cmb` object gets passed as a parameter to the callback. This complements the `'show_on_cb'` parameter that already exists for individual fields. Using this callback is similar to using the `'cmb2_show_on'` filter, but only applies to that specific metabox and it is recommended to use this callback instead as it minimizes th risk that your filter will affect other metaboxes.
* Taxonomy types no longer save a value. The value getting saved was causing confusion and is not meant to be used. To use the saved taxonomy data, you need to use the WordPress term api, `get_the_terms `, `get_the_term_list`, etc.
* Add `'multiple'` field parameter to store values in individual rows instead of serialized array. Will only work if field is not repeatable or a repeatable group. Props [JohnyGoerend](https://github.com/JohnyGoerend). ([#262](CMB2/CMB2#262), [#206](CMB2/CMB2#206), [#45](CMB2/CMB2#45)).
* Portuguese (Brazil) translation provided by [@lucascdsilva](https://github.com/lucascdsilva) - [#293](CMB2/CMB2#293).
* Spanish (Spain) translation updated by [@yivi](https://github.com/yivi) - [#272](CMB2/CMB2#272).
* Added group field callback parameters, `'before_group'`, `'before_group_row'`, `'after_group_row'`, `'after_group'` to complement the `'before_row'`, `'before'`, `'after'`, `'after_row'` field parameters.
* Better styling for `title` fields and `title` descriptions on options pages.
* Add a `sanitization_cb` field parameter check for the `group` field type.
* Better function/file doc-blocks to provide better documentation for automated documentation tools. See: [cmb2.io/api](http://cmb2.io/api/).
* `cmb2_print_metabox_form`, `cmb2_metabox_form`, and `cmb2_get_metabox_form` helper functions now accept two new parameters:
	* an `'object_type'` parameter to explictly set that in the `$cmb` object.
	* an `'enqueue_js'` parameter to explicitly disable the CMB JS enqueue. This is handy if you're not planning on using any of the fields which require JS (like color/date pickers, wysiwyg, file, etc).

### Bug Fixes

* Fix issue with oembed fields in repeatable groups where changing video changed it for all fields in a group.
* Fix empty arrays (like in the group field) saving as a value.
* Move `'cmb2_override_meta_value'` and `"cmb2_override_{$field_id}_meta_value"` filters to the `CMB2_Field::get_data()` method so that the filters are applied every time the data is requested. **THIS IS A BREAKING CHANGE:** The parameters for those filters have changed a bit. Previously, the filters accepted 5 arguments, `$value`, `$object_id`, `$field_args`, `$object_type`, `$field`. They have changed to accept 4 arguments instead, `$value`, `$object_id`, `$args`, `$field`, where `$args` is an array that contains the following:
	* @type string $type     The current object type
	* @type int    $id       The current object ID
	* @type string $field_id The ID of the field being requested
	* @type bool   $repeat   Whether current field is repeatable
	* @type bool   $single   Whether current field is a single database row

git-svn-id: https://plugins.svn.wordpress.org/cmb2/trunk@1150489 b8457f37-d9ea-0310-8a92-e5e31aec5664
@pieroit
Copy link

pieroit commented Sep 28, 2015

Hi guys,
I need this feature and was happy to see it was already addressed. My problem is that I can't get the option multiple to work with the option repeatable.

The meta has two values, sotred into the DB as two different records.
image

If I set multiple => true and repeatable => true, only the first value is shown in an <input> box and below there is the "Add Row" button to insert more. The second one disappear (despite being present in the DB).
image

If I set multiple => false and repeatable => true, same as above.

If I set multiple => true and repeatable => false, both values get passed to the field but they are printed together into one <input>, in which I read Array. (see Notice)
image

What do you think? I asked for help before diving into the code because I already have in place a functioning metabox system, and the switch may require too much time. Hope to fix this soon to join the community :)

Thanks,
Piero

@pieroit
Copy link

pieroit commented Sep 30, 2015

Found a way to send the meta values correctly to the repeated field.
This line
https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L232
in the following block:

        // If no override, get value normally
        if ( 'cmb2_field_no_override_val' === $data ) {
            $data = 'options-page' === $a['type']
                ? cmb2_options( $a['id'] )->get( $a['field_id'] )
                : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) );
        }

calls get_metadata with final parameter set to $a['single'] || $a['repeat'].
This causes the metabox to load only one value even when you explicitly asked for repeated in combination with multiple. The problem is fixed by changing to this:
: get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || !$a['repeat'] ) );

But I don't know if I broke something in this way. Also, the values are still serialized when inserted into the DB. Can anybody help?

Thank you,
Piero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants