Skip to content

Commit

Permalink
Introduce the 'save_field' boolean field property. Closes #346, Closes
Browse files Browse the repository at this point in the history
 #500

For disabling the saving of a field. Useful if you want to display the
value of another field, or use a disabled/read-only field.

See example in example-functions.php
  • Loading branch information
jtsternberg committed Jun 28, 2016
1 parent e2fb78f commit f54c262
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Enhancements

* Small string improvement, move a period inside the translatable string. Props [@pedro-mendonca](https://github.com/pedro-mendonca) ([#672](https://github.com/WebDevStudios/CMB2/pull/672)).
* Introduce the `'save_field'` boolean field property for disabling the saving of a field. Useful if you want to display the value of another field, or use a disabled/read-only field. ([#346](https://github.com/WebDevStudios/CMB2/issues/346), [#500](https://github.com/WebDevStudios/CMB2/issues/500))

## 2.2.2.1 - 2016-06-27

Expand Down
14 changes: 13 additions & 1 deletion example-functions.php
Expand Up @@ -157,7 +157,19 @@ function yourprefix_register_demo_metabox() {
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'textmedium',
'type' => 'text_medium',
// 'repeatable' => true,
) );

$cmb_demo->add_field( array(
'name' => __( 'Read-only Disabled Field', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'readonly',
'type' => 'text_medium',
'default' => __( 'Hey there, I\'m a read-only field', 'cmb2' ),
'save_field' => false, // Disables the saving of this field.
'attributes' => array(
'disabled' => 'disabled',
'readonly' => 'readonly',
),
) );

$cmb_demo->add_field( array(
Expand Down
13 changes: 9 additions & 4 deletions includes/CMB2_Field.php
Expand Up @@ -472,12 +472,16 @@ public function save_field_from_data( array $data_to_save ) {
*/
public function save_field( $meta_value ) {

$new_value = $this->sanitization_cb( $meta_value );
$old = $this->get_data();
$updated = false;
$action = '';
$new_value = $this->sanitization_cb( $meta_value );

if ( ! $this->args( 'save_field' ) ) {

// Nothing to see here.
$action = 'disabled';

if ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) {
} elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) {

$this->remove_data();
$count = 0;
Expand All @@ -493,7 +497,7 @@ public function save_field( $meta_value ) {
$updated = $count ? $count : false;
$action = 'repeatable';

} elseif ( ! cmb2_utils()->isempty( $new_value ) && $new_value !== $old ) {
} elseif ( ! cmb2_utils()->isempty( $new_value ) && $new_value !== $this->get_data() ) {
$updated = $this->update_data( $new_value );
$action = 'updated';
} elseif ( cmb2_utils()->isempty( $new_value ) ) {
Expand Down Expand Up @@ -1043,6 +1047,7 @@ public function _set_field_defaults( $args, $blah ) {
'inline' => false,
'on_front' => true,
'show_names' => true,
'save_field' => true, // Will not save if false
'date_format' => 'm\/d\/Y',
'time_format' => 'h:i A',
'description' => isset( $args['desc'] ) ? $args['desc'] : '',
Expand Down
4 changes: 2 additions & 2 deletions tests/test-cmb-field.php
Expand Up @@ -86,13 +86,13 @@ public function test_cmb2_label_cb_field_callback() {
public function test_cmb2_row_classes_field_callback_with_array() {
// Add row classes dynamically with a callback that returns an array
$classes = $this->field->row_classes();
$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout type name desc before after options options_cb text text_cb attributes protocols default default_cb select_all_button multiple repeatable inline on_front show_names date_format time_format description preview_size render_row_cb display_cb label_cb column id before_field after_field row_classes _id _name has_supporting_data', $classes );
$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout type name desc before after options options_cb text text_cb attributes protocols default default_cb select_all_button multiple repeatable inline on_front show_names save_field date_format time_format description preview_size render_row_cb display_cb label_cb column id before_field after_field row_classes _id _name has_supporting_data', $classes );
}

public function test_cmb2_default_field_callback_with_array() {
// Add row classes dynamically with a callback that returns an array
$default = $this->field->args( 'default' );
$this->assertEquals( 'type, name, desc, before, after, options, options_cb, text, text_cb, attributes, protocols, default, default_cb, select_all_button, multiple, repeatable, inline, on_front, show_names, date_format, time_format, description, preview_size, render_row_cb, display_cb, label_cb, column, id, before_field, after_field, row_classes, _id, _name, has_supporting_data', $default );
$this->assertEquals( 'type, name, desc, before, after, options, options_cb, text, text_cb, attributes, protocols, default, default_cb, select_all_button, multiple, repeatable, inline, on_front, show_names, save_field, date_format, time_format, description, preview_size, render_row_cb, display_cb, label_cb, column, id, before_field, after_field, row_classes, _id, _name, has_supporting_data', $default );
}

public function test_cmb2_row_classes_field_callback_with_string() {
Expand Down

0 comments on commit f54c262

Please sign in to comment.