Comments (10)
After investigate it, I do not need add any extra check for group fields at widget class
The value is stored correctly, but for some reason is not loaded correctly
from cmb2-snippet-library.
is possible that default_cb is not called on repeatable groups because it returns false instead of null?
from cmb2-snippet-library.
I try to override how CMB2 is getting group values because is trying to retrieve a wrong options index
I dumped the contents of $args in cmb2_override_meta_value and It has type => post instead of options-page (and metabox has correctly configured show_on argument)
I need to investigate It a bit more
from cmb2-snippet-library.
I got some progress about this
I am able to get group field elements values, but for some reason it only return the first element
public function cmb2( $saving = false ) {
// Create a new box in the class
$cmb2 = new CMB2( array(
'id' => $this->option_name .'_box', // Option name is taken from the WP_Widget class.
'hookup' => false,
'show_on' => array(
'key' => 'options-page', // Tells CMB2 to handle this as an option
'value' => array( $this->option_name )
),
), $this->option_name );
foreach ( $this->fields as $field ) {
if ( ! $saving ) {
$field['id'] = $this->get_field_name( $field['id'] );
}
if( $field['type'] == 'group' ) {
// Update group fields default_cb
foreach( $field['fields'] as $group_field_index => $group_field ) {
$group_field['default_cb'] = array( $this, 'default_cb' );
$field['fields'][$group_field_index] = $group_field;
}
}
$field['default_cb'] = array( $this, 'default_cb' );
$cmb2->add_field( $field );
}
return $cmb2;
}
public function default_cb( $field_args, $field ) {
if( $field->group ) {
if( isset( $this->_instance[ $field->group->args( 'id_key' ) ] ) ) {
$data = $this->_instance[ $field->group->args( 'id_key' ) ];
return is_array( $data ) && isset( $data[ $field->group->index ][ $field->args( 'id_key' ) ] )
? $data[ $field->group->index ][ $field->args( 'id_key' ) ]
: null;
} else {
return null;
}
}
return isset( $this->_instance[ $field->args( 'id_key' ) ] )
? $this->_instance[ $field->args( 'id_key' ) ]
: null;
}
from cmb2-snippet-library.
Finally I got it working, I need to add a filter to cmb2_override_meta_value
to retrieve correctly group field value
from cmb2-snippet-library.
@rubengc would you be willing to submit a PR on the widget snippet so we can see what worked and potentially add that code to the snippet library?
from cmb2-snippet-library.
The problem (that i hackily solved) is CMB2 do not call default_cb on group field but default_cb is called in fields inside a group
If CMB2 adds this check to group fields probably my solution will not be needle
from cmb2-snippet-library.
👍
This is really an issue in CMB2, then, rather than the snippet library, but I will leave it open here for context.
from cmb2-snippet-library.
public function form( $instance ) {
add_filter( 'cmb2_override_meta_value', array( $this, 'cmb2_override_meta_value' ), 11, 4 );
// If there are no settings, set up defaults
$this->_instance = wp_parse_args( (array) $instance, self::$defaults );
$cmb2 = $this->cmb2();
$cmb2->object_id( $this->option_name );
CMB2_hookup::enqueue_cmb_css();
CMB2_hookup::enqueue_cmb_js();
$cmb2->show_form();
remove_filter( 'cmb2_override_meta_value', array( $this, 'cmb2_override_meta_value' ) );
}
public function cmb2_override_meta_value( $value, $object_id, $args, $field ) {
if ( $field->group || 'group' === $field->type() ) {
if ( isset( $field->args['id_key'] ) ) {
$id_key = $field->args['id_key'];
if ( isset( $this->_instance[$id_key] ) ) {
$value = $this->_instance[$id_key];
}
}
}
return $value;
}
public function cmb2( $saving = false ) {
// Create a new box in the class
$cmb2 = new CMB2( array(
'id' => $this->option_name .'_box', // Option name is taken from the WP_Widget class.
'hookup' => false,
'show_on' => array(
'key' => 'options-page', // Tells CMB2 to handle this as an option
'value' => array( $this->option_name )
),
), $this->option_name );
foreach ( $this->fields as $field ) {
if ( ! $saving ) {
$field['id'] = $this->get_field_name( $field['id'] );
}
if( $field['type'] == 'group' ) {
// Update group fields default_cb
foreach( $field['fields'] as $group_field_index => $group_field ) {
$group_field['default_cb'] = array( $this, 'default_cb' );
$field['fields'][$group_field_index] = $group_field;
}
}
$field['default_cb'] = array( $this, 'default_cb' );
$cmb2->add_field( $field );
}
return $cmb2;
}
public function default_cb( $field_args, $field ) {
if( $field->group ) {
if( isset( $this->_instance[ $field->group->args( 'id_key' ) ] ) ) {
$data = $this->_instance[ $field->group->args( 'id_key' ) ];
return is_array( $data ) && isset( $data[ $field->group->index ][ $field->args( 'id_key' ) ] )
? $data[ $field->group->index ][ $field->args( 'id_key' ) ]
: null;
} else {
return null;
}
}
return isset( $this->_instance[ $field->args( 'id_key' ) ] )
? $this->_instance[ $field->args( 'id_key' ) ]
: null;
}
(function( window, document, $ ) {
$( document ).on('widget-updated widget-added', function( event, widget ) {
var $metabox = $(widget).find('.cmb2-wrap > .cmb2-metabox'),
cmb = window.CMB2;
$metabox
.on('click', '.cmb-add-group-row', cmb.addGroupRow)
.on('click', '.cmb-add-row-button', cmb.addAjaxRow)
.on('click', '.cmb-remove-group-row', cmb.removeGroupRow)
.on('click', '.cmb-remove-row-button', cmb.removeAjaxRow)
.on( 'click', '.cmbhandle, .cmbhandle + .cmbhandle-title', cmb.toggleHandle );
});
})( window, document, jQuery );
from cmb2-snippet-library.
Note: ColorPicker fields gets destroyed after field save on widgets area a way to fix it:
// Auto Call plugin is class is color-picker
jQuery( document ).ready( function( $ ) {
$( '.color-picker' ).wpColorPicker();
// Initialize on widgets area
$(document).on('widget-updated widget-added', function(e, widget) {
widget.find( '.color-picker, .cmb2-colorpicker' ).wpColorPicker();
});
} );
An explanation on this can be found here
from cmb2-snippet-library.
Related Issues (20)
- [Help needed] Conditionally replacing a field for another HOT 3
- "required" validation not working. HOT 4
- Auto complete not working in third row HOT 11
- How to return programmatic values and selected options for cmb2 fields HOT 2
- Show_on_cb option based on meta-value (Not working?) HOT 7
- The form is as follows: HOT 8
- Option page and submenus example doesn't work HOT 1
- Default values not applicable in theme option page HOT 10
- edit form not saving HOT 2
- Autocomplete doesn't work if field id starts not from underscore symbol HOT 2
- Autocomplete works only on Post type forms HOT 3
- Possible issue with 'Dynamically Changing Group Title From Subfield' (row counts are getting off)
- Snippet "options-pages-with-tabs-and-submenus" : where is CMB2 2.4.0 ? HOT 1
- Title throwing error HOT 3
- Hope to find help for submenu cpt settings/tabs? HOT 3
- [Widget snippet] file (and file_list) field type looks like not working HOT 7
- Overriding WP Core post_status field issue HOT 4
- Overriding Meta save for Taxonomy Terms
- [Help] Custom admin taxonomy page view and cmb2 fields HOT 3
- limit-number-of-repeat-fields in taxonomy page HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cmb2-snippet-library.