Skip to content

Commit 2b851e7

Browse files
committed
Add example for overriding data source
1 parent e64dcf8 commit 2b851e7

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Demonstrates using an alternate data source for storing/retrieving data for a CMB2 box.
4+
* In this case, we're simply storing/retrieving/removing from an option in the options table,
5+
* but this can be extended to handle data storage to/from custom tables, Redis, REST APIs, etc.
6+
*/
7+
8+
add_action( 'cmb2_init', 'yourprefix_register_yourprefix_group_alt_data_metabox' );
9+
/**
10+
* Hook in and add a metabox to demonstrate repeatable grouped fields
11+
*/
12+
function yourprefix_register_yourprefix_group_alt_data_metabox() {
13+
14+
$default_values = get_option( 'yourprefix_group_alt_data_demo' );
15+
if ( ! is_array( $default_values ) ) {
16+
add_option( 'yourprefix_group_alt_data_demo', array(
17+
// Default group 1.
18+
array(
19+
'title' => 'Hey, this is your first entry',
20+
'description' => 'Go ahead and delete this entry, or update its contents',
21+
),
22+
// Default group 2.
23+
array(
24+
'title' => 'Hey, this is your 2nd entry',
25+
'description' => '#2',
26+
),
27+
) );
28+
}
29+
30+
$cmb_group = new_cmb2_box( array(
31+
'id' => 'yourprefix_group_alt_data_metabox',
32+
'title' => __( 'Repeating Field Group', 'cmb2' ),
33+
'object_types' => array( 'post', ),
34+
'show_on' => array( 'id' => array( 1000, ) ),
35+
'show_in_rest' => WP_REST_Server::ALLMETHODS,
36+
) );
37+
38+
// $group_field_id is the field id string, so in this case: '_yourprefix_group_demo'
39+
$group_field_id = $cmb_group->add_field( array(
40+
'id' => 'yourprefix_group_alt_data_demo',
41+
'type' => 'group',
42+
'options' => array(
43+
'group_title' => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
44+
'add_button' => __( 'Add Another Entry', 'cmb2' ),
45+
'remove_button' => __( 'Remove Entry', 'cmb2' ),
46+
'sortable' => true, // beta
47+
),
48+
) );
49+
50+
/**
51+
* Group fields works the same, except ids only need
52+
* to be unique to the group. Prefix is not needed.
53+
*
54+
* The parent field's id needs to be passed as the second argument.
55+
*/
56+
$cmb_group->add_group_field( $group_field_id, array(
57+
'name' => 'Entry Title',
58+
'id' => 'title',
59+
'type' => 'text',
60+
) );
61+
62+
$cmb_group->add_group_field( $group_field_id, array(
63+
'name' => 'Description',
64+
'description' => 'Write a short description for this entry',
65+
'id' => 'description',
66+
'type' => 'wysiwyg',
67+
'options' => array(
68+
'textarea_rows' => 3,
69+
),
70+
) );
71+
};
72+
73+
add_filter( 'cmb2_override_yourprefix_group_alt_data_demo_meta_value', 'yourprefix_group_alt_data_demo_override_meta_value', 10, 4 );
74+
function yourprefix_group_alt_data_demo_override_meta_value( $data, $object_id, $args, $field ) {
75+
76+
// Here, we're pulling from the options table, but you can query from any data source here.
77+
// If from a custom table, you can use the $object_id to query against.
78+
return get_option( 'yourprefix_group_alt_data_demo', array() );
79+
}
80+
81+
add_filter( 'cmb2_override_yourprefix_group_alt_data_demo_meta_save', 'yourprefix_group_alt_data_demo_override_meta_save', 10, 4 );
82+
function yourprefix_group_alt_data_demo_override_meta_save( $override, $args, $field_args, $field ) {
83+
84+
// Here, we're storing the data to the options table, but you can store to any data source here.
85+
// If to a custom table, you can use the $args['id'] as the reference id.
86+
$updated = update_option( 'yourprefix_group_alt_data_demo', $args['value'] );
87+
return !! $updated;
88+
}
89+
90+
add_filter( 'cmb2_override_yourprefix_group_alt_data_demo_meta_remove', 'yourprefix_group_alt_data_demo_override_meta_remove', 10, 4 );
91+
function yourprefix_group_alt_data_demo_override_meta_remove( $override, $args, $field_args, $field ) {
92+
// Here, we're removing from the options table, but you can query to remove from any data source here.
93+
// If from a custom table, you can use the $args['id'] to query against.
94+
// (If we do "delete_option", then our default value will be re-applied, which isn't desired.)
95+
$updated = update_option( 'yourprefix_group_alt_data_demo', array() );
96+
return !! $updated;
97+
}

0 commit comments

Comments
 (0)