-
Notifications
You must be signed in to change notification settings - Fork 566
Description
I have a field with the attributes set to readonly and disabled:
$allow_ad_sync->add_field( array(
'name' => __( 'Last Modified', 'cmb2' ),
'desc' => __( 'Time last modified in LDAP', 'cmb2' ),
'id' => $prefix . 'last_modified',
'type' => 'text_date_timestamp',
'date_format' => 'm-d-Y @ h:i:s',
'on_front' => false, // Optionally designate a field to wp-admin only
'show_on_cb' => array( $this, 'upd_cbm2_is_user_admin' ), // function should return a bool value,
'attributes' => array(
'readonly' => 'readonly',
'disabled' => 'disabled',
),
) );I populate the field value through a separate script and just wish to display it to users without giving them the ability to update it ( readonly and disabled ).
This issue here is that because it isn't passed along in the $_POST data ( I believe because it is marked as disabled in the DOM ) the value is set to null in CMB2_Field.php on line 429:
https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L429
/**
* Process $_POST data to save this field's value
* @since 2.0.3
* @param array $data_to_save $_POST data to check
* @return bool Result of save
*/
public function save_field_from_data( $data_to_save ) {
$meta_value = isset( $data_to_save[ $this->id( true ) ] )
? $data_to_save[ $this->id( true ) ]
: null;
return $this->save_field( $meta_value );
}Setting it to null is then set as the value and wipes my field's value. Instead there probably should be a check for disabled fields and not apply any update. The benefit of adding a conditional here would disallow tampering with the DOM (although there are probably other checks for that already).
As a workaround I've added a hook to the override filter here:
https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L290
for my specific field_id on the filter (I may do one more generic for disabled fields in general, but right now I only have the one field):
add_filter( 'cmb2_override_{_my_prefix_and_field_id}_meta_remove', array($this, 'upd_dont_remove_disabled_field'), 10, 4 );
public function upd_dont_remove_disabled_field($override, $args, $field_args, $field_object) {
$override = true;
return $override;
}This override filter above fixes the issue but disabled fields probably shouldn't be updated at all or require a filter like this.