Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

insert_model_entry does not accept true for boolean fields #607

Open
nickcernis opened this issue Jul 26, 2022 · 0 comments
Open

insert_model_entry does not accept true for boolean fields #607

nickcernis opened this issue Jul 26, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@nickcernis
Copy link
Member

nickcernis commented Jul 26, 2022

insert_model_entry only updates boolean values to true if 'on' is passed in place of true.

To reproduce

  1. Create a model called 'rabbit' with a boolean field named 'bool'.
  2. Run this code:
    $model_id   = 'rabbit';
    $field_data = [
  	  'bool'  => true,						
    ];	

    $post_id = insert_model_entry( $model_id, $field_data );

    var_dump( $post_id );     
  1. Grab the post meta for the new post ID via wp post meta list [id-of-post]
  2. The bool value will be false/'off' instead of the expected true/'on'.

Repeat the same with a value of 'on' and post meta should save correctly.

Expected behavior

Boolean values passed for boolean fields should save.

Version information

  • Plugin version: 0.19.2
  • WordPress version: 6.0.1

Additional context

From the report in Slack:

l believe there's a bug in how boolean values are being evaluated in the /plugins/atlas-content-modeler/includes/publisher/lib/field-functions.php file. It should be checking for true/false on line 327. Instead it checks for "on" and booleans are never set as true in the model.

Relevant lines (perhaps check for true or 'on' instead of just 'on', or migrate to real boolean values):

case 'boolean':
return $value === 'on' ? 'on' : 'off';

Workaround

Boolean field values have to be cast as 'on' or 'off':

	$model_id   = 'rabbit';
	$boolean_value = true;
	$field_data = [		
		'bool' => $boolean_value ? 'on' : 'off',
	];	

	$post_id = insert_model_entry( $model_id, $field_data );
@nickcernis nickcernis added the bug Something isn't working label Jul 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant