Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant