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

Adding a group row duplicates the previous row's content #257

Closed
austinjreilly opened this issue Mar 18, 2015 · 10 comments
Closed

Adding a group row duplicates the previous row's content #257

austinjreilly opened this issue Mar 18, 2015 · 10 comments

Comments

@austinjreilly
Copy link

It's entirely possible that I'm doing something that isn't smart, so if that's the case please let me know. In any case, here's what I've got. The relevant portions of my code are below.

I am using CMB2 to create an internal "Notes" field that will add the current user id as the note author, along with the content of the note. Then on refresh, the note author will display above the note (via the get_note_author callback). This works with the code I have below, except that now when I click the "Add a note" button, the new note contains the old note's content and author ID.

It looks to my untrained eye like something that I'm doing is preventing the newRowHousekeeping JavaScript function from firing, but I'm definitely not sure.

Anyone have any ideas?

//Create notes metabox
add_filter('cmb2_init', array($this, 'notes_metabox'));

//Renders note author id
add_filter( 'cmb2_render_note_author', array($this,'cmb2_render_callback_for_note_author'), 10, 1 );

//Sanitizes note author id field
add_filter( 'cmb2_sanitize_note_author', array($this,'cmb2_sanitize_note_author_callback'),10, 1);
public function notes_metabox() {

    $prefix = '_journal_notes_';

    $journal_notes_metabox = new_cmb2_box(array(
        'id' => $prefix . 'metabox',
        'title' => 'Notes',
        'object_types' => array('journal'),
        'context' => 'side',
        'priority' => 'default',
        'show_names' => false,
        'sortable' => false,
        //'closed' => true,
        'fields' => array(
            array(
                'id' => $prefix . 'repeat_group',
                'type' => 'group',
                'options' => array(
                                'group_title' => 'Note #{#}',
                                'add_button' => 'Add a note',
                                'remove_button' => 'Remove this note',
                             ),
                'fields' => array(
                                array(
                                    'name' => 'Note',
                                    'desc' => '',
                                    'id' => 'content',
                                    'before_row' => array($this,'get_note_author'),
                                    'type' => 'textarea_small',
                                    'default' => false
                                ),
                                array(
                                    'name' => 'Note Author',
                                    'desc' => '',
                                    'id' => 'author',
                                    'type' => 'note_author',
                                    'default' => ''
                                ),
                            ),
            ),
        ),
    ));
}
//Adds hidden note author field
public function cmb2_render_callback_for_note_author($field_type_object){
    echo $field_type_object->input( array( 'class' => 'cmb2_note_author', 'type' => 'hidden' ) );
}

//Don't add an author if there already is one or if there isn't any note content
public function cmb2_sanitize_note_author_callback($value){

    if(empty($value)){
        $notes_repeat_group = $_POST['_journal_notes_repeat_group'];
        foreach ($notes_repeat_group as $note){

            if ( !empty($note['content']) && empty($note['author'])){
                $value = get_current_user_id();
            }
        }
    }
    return $value;
}

public function get_note_author($field_args,$field){
    $count = $field->group->args['count'];
    if (is_array($field->group->value)){
        $note_author_id = $field->group->value[$count]['author'];
    }

    if (!empty($note_author_id)){
        $user_info = get_userdata($note_author_id);
        return '<p>Note Author: '.$user_info->display_name.'</p>';
    } else {
        return false;
    }
}
@christiespeich
Copy link

Oooh, I'm having this problem too! I searched and searched but couldn't find anyone else reporting this, so I assumed it was something I had done. I haven't had a chances to really dig into the code to see what the problem might be.

@quexxon
Copy link

quexxon commented Mar 23, 2015

I believe that your problem is fixed in the latest release (2.0.5). There was an issue with subsequent repeating groups all being populated with values from the first group.

@christiespeich
Copy link

Hmm...I don't know about the original poster, but it appears I am using version 2.0.0.7 ... so a bug introduced in 2.0.3 shouldn't affect me. But upgrading to the latest release might not be a bad idea anyway....

@austinjreilly
Copy link
Author

@quexxon I was/am using 2.0.5, along with WordPress 4.1.1. I should have made that clear in my original post. I'll look into #255 and 8ffbea4 to see if I can gain further insight into what's going on.

@austinjreilly
Copy link
Author

That commit was correcting some problems with the PHP. All of the PHP related functionality seems to be working. From what I can tell, the problem is that (for me at least, for some reason) calling .val on line 366 of cmb2.js isn't actually removing the content from the input that it's supposed to be.

@jtsternberg
Copy link
Member

@austinjreilly Please try converting your metabox registration to using the new API, demonstrated in example-functions.php and the wiki and let me know if you still see issues.

@austinjreilly
Copy link
Author

@jtsternberg I'm still seeing the same issue. My new metabox registration code is below. Thanks for looking into this!

public function notes_metabox() {
    $prefix = '_journal_notes_';

    $journal_notes_metabox = new_cmb2_box(array(
        'id' => $prefix . 'metabox',
        'title' => 'Notes',
        'object_types' => array('journal'),
        'context' => 'side',
        'priority' => 'default',
        'show_names' => false,
        'sortable' => false,
        //'closed' => true,
    ));

    $journal_notes_group = $journal_notes_metabox->add_field(array(
        'id' => $prefix . 'repeat_group',
        'type' => 'group',
        'options' => array(
                        'group_title' => 'Note #{#}',
                        'add_button' => 'Add a note',
                        'remove_button' => 'Remove this note',
                     ),
    ));

    $journal_notes_metabox->add_group_field($journal_notes_group, array(
        'name' => 'Note',
        'desc' => '',
        'id' => 'content',
        'before_row' => array($this,'get_note_author'),
        'type' => 'textarea_small',
    ));

    $journal_notes_metabox->add_group_field($journal_notes_group, array(
        'name' => 'Note Author',
        'desc' => '',
        'id' => 'author',
        'type' => 'note_author',
        'default' => ''
    ));
}

kynetiv pushed a commit to kynetiv/CMB2 that referenced this issue Apr 28, 2015
…a checkbox or radio input.

If the input is a checkbox or radio, the original value is needed or saving will not save the value (subsequent post saves work because inputs  will then have the required value set).
This patch conditionally adds values to radios and inputs on new repeatable and sets the checked attribute only on the default option (radios).
Fixes issue CMB2#257 (this will set reset text inputs as well), and CMB2#246 & CMB2#263(restores radio and checkbox values on new row saves)
@jtsternberg
Copy link
Member

@austinjreilly the get_note_author callback will not work for repeatable groups the way you expect. There is nothing in the JS to update that content on the fly, so if that's an integral feature for you, you'll need to create your own javascript handler. Is that the only issue you are experiencing?

@jtsternberg
Copy link
Member

@austinjreilly it is super-odd that .val('') is not properly clearing out the values, and, for me, seemed to only occur when the 'sortable' attribute was left off (or false). I have included a fix and it should be good.

@austinjreilly
Copy link
Author

@jtsternberg Thanks for looking into this. I've upgraded to trunk. Here's what I can report:

  • Values for text areas are cleared, so I believe that's fixed.
  • Values for radio buttons are repeated, but are not saved. To be more clear, radio buttons (and probably all non-text area content) gets repeated when new rows are added once a repeater field has been saved, although the values themselves are not saved. There is no issue when first loading a repeater field on a post which has never been saved. Does that make sense? I can sign in to Gitter if that would help.
  • I'm getting the following warnings when I turn on define('SCRIPT_DEBUG', true):
JQMIGRATE: jQuery.fn.attr('value') no longer gets properties
console.trace()
migrateWarn @ jquery-migrate.js?ver=1.2.1:43
jQuery.attrHooks.value.get @ jquery-migrate.js?ver=1.2.1:170
m.extend.attr @ jquery.js?ver=1.11.2:4
jQuery.attr @ jquery-migrate.js?ver=1.2.1:159
m.access @ jquery.js?ver=1.11.2:3
m.fn.extend.attr @ jquery.js?ver=1.11.2:4(anonymous function) @ cmb2.js?ver=2.0.6:347
m.extend.each @ jquery.js?ver=1.11.2:2m.fn.m.each @ jquery.js?ver=1.11.2:2
window.CMB2.cmb.cleanRow @ cmb2.js?ver=2.0.6:343
window.CMB2.cmb.addGroupRow @ cmb2.js?ver=2.0.6:506
m.event.dispatch @ jquery.js?ver=1.11.2:3
m.event.add.r.handle @ jquery.js?ver=1.11.2:3

and

JQMIGRATE: jQuery.fn.attr('value', val) no longer sets properties
jquery-migrate.js?ver=1.2.1:43
console.trace()
migrateWarn @ jquery-migrate.js?ver=1.2.1:43
jQuery.attrHooks.value.set @ jquery-migrate.js?ver=1.2.1:182
m.extend.attr @ jquery.js?ver=1.11.2:4jQuery.attr @ jquery-migrate.js?ver=1.2.1:159
m.access @ jquery.js?ver=1.11.2:3
m.access @ jquery.js?ver=1.11.2:3
m.fn.extend.attr @ jquery.js?ver=1.11.2:4(anonymous function) @ cmb2.js?ver=2.0.6:382
m.extend.each @ jquery.js?ver=1.11.2:2
m.fn.m.each @ jquery.js?ver=1.11.2:2
window.CMB2.cmb.cleanRow @ cmb2.js?ver=2.0.6:343
window.CMB2.cmb.addGroupRow @ cmb2.js?ver=2.0.6:506
m.event.dispatch @ jquery.js?ver=1.11.2:3
m.event.add.r.handle @ jquery.js?ver=1.11.2:3

Here is the code for my repeater field:

add_filter('cmb2_init', 'research_question_metabox');
function research_question_metabox() {

    $prefix = '_article_research_questions_';

    $research_questions_metabox = new_cmb2_box(array(
        'id' => $prefix . 'metabox',
        'title' => 'Research Questions',
        'object_types' => array('article'),
        'context' => 'normal',
        'priority' => 'default',
        'show_names' => true,
        'sortable' => false,
    ));

    $research_question_group = $research_questions_metabox->add_field(array(
        'id' => $prefix . 'research_question_group',
        'type' => 'group',
        'options' => array(
            'group_title' => 'Research Question #{#}',
            'add_button' => 'Add a Research Question',
            'remove_button' => 'Remove this Research Question',
        ),
    ));

    $research_questions_metabox->add_group_field($research_question_group, array(
        'name' => 'Is this research question explicit from the manuscript?',
        'desc' => '',
        'id' => 'explicit',
        'type' => 'radio_inline',
        'options' => array('yes' => 'Yes',
            'no' => 'No',
            'combination' => 'Combination',
        ),
    ));

    $research_questions_metabox->add_group_field($research_question_group, array(
        'name' => 'Research Question',
        'desc' => '',
        'id' => 'research_question',
        'type' => 'textarea_small',
    ));
}

pluginmirror-worker pushed a commit to wp-plugins/cmb2 that referenced this issue May 29, 2015
* Ability to use non-repeatable group fields by setting the `'repeatable'` field param to `false` when registering a group field type. Props [marcusbattle](https://github.com/marcusbattle), ([#159](CMB2/CMB2#159)).
* Add and enqeueue a front-end specific CSS file which adds additional styles which are typically covered by wp-admin css. ([#311](CMB2/CMB2#311))
* Better handling of the CMB2 javascript (and CSS) required dependencies array. Dependencies are now only added conditionally based on the field types that are actually visible. ([#136](CMB2/CMB2#136))
* **THIS IS A BREAKING CHANGE:** The `group` field type's `'show_on_cb'` property now receives the `CMB2_Field` object instance as an argument instead of the `CMB2` instance. If you're using the `'show_on_cb'` property for a `group` field, please adjust accordingly. _note: you can still retrieve the `CMB2` instance via the `cmb2_get_metabox` helper function._
* New dynamic hook, `"cmb2_save_{$object_type}_fields_{$this->cmb_id}"`, to complement the existing `"cmb2_save_{$object_type}_fields"` hook.
* New CMB2 property, `enqueue_js`, to disable the enqueueing of the CMB2 Javascript.
* German translation provided by Friedhelm Jost.

### Bug Fixes

* Fix incorrect repeatable group title number. ([#310](CMB2/CMB2#310))
* Fix obscure bug which prevented group field arguments from being passed to the sub-fields (like `show_names` and `context`).
* Fixed occasional issue when adding a group row, the previous row's content would be cloned. ([#257](CMB2/CMB2#257))


git-svn-id: https://plugins.svn.wordpress.org/cmb2/trunk@1169742 b8457f37-d9ea-0310-8a92-e5e31aec5664
pluginmirror-worker pushed a commit to wp-plugins/cmb2 that referenced this issue May 29, 2015
* Ability to use non-repeatable group fields by setting the `'repeatable'` field param to `false` when registering a group field type. Props [marcusbattle](https://github.com/marcusbattle), ([#159](CMB2/CMB2#159)).
* Add and enqeueue a front-end specific CSS file which adds additional styles which are typically covered by wp-admin css. ([#311](CMB2/CMB2#311))
* Better handling of the CMB2 javascript (and CSS) required dependencies array. Dependencies are now only added conditionally based on the field types that are actually visible. ([#136](CMB2/CMB2#136))
* **THIS IS A BREAKING CHANGE:** The `group` field type's `'show_on_cb'` property now receives the `CMB2_Field` object instance as an argument instead of the `CMB2` instance. If you're using the `'show_on_cb'` property for a `group` field, please adjust accordingly. _note: you can still retrieve the `CMB2` instance via the `cmb2_get_metabox` helper function._
* New dynamic hook, `"cmb2_save_{$object_type}_fields_{$this->cmb_id}"`, to complement the existing `"cmb2_save_{$object_type}_fields"` hook.
* New CMB2 property, `enqueue_js`, to disable the enqueueing of the CMB2 Javascript.
* German translation provided by Friedhelm Jost.

### Bug Fixes

* Fix incorrect repeatable group title number. ([#310](CMB2/CMB2#310))
* Fix obscure bug which prevented group field arguments from being passed to the sub-fields (like `show_names` and `context`).
* Fixed occasional issue when adding a group row, the previous row's content would be cloned. ([#257](CMB2/CMB2#257))


git-svn-id: https://plugins.svn.wordpress.org/cmb2/trunk@1169744 b8457f37-d9ea-0310-8a92-e5e31aec5664
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

No branches or pull requests

4 participants