Trying to set a default value on an entity reference field.

$unitid = node_load($unit);
$form['field_choose_a_service']['und']['#default_value'] = $unitid;

Warning: Illegal offset type in form_type_checkboxes_value() (line 2390 of /homepages/43/d503288075/htdocs/example.co.uk/includes/form.inc).

Notice: Object of class stdClass could not be converted to int in form_process_checkboxes() (line 3316 of /homepages/43/d503288075/htdocs/example.co.uk/includes/form.inc).

And targeting the nid from that loaded node...

$unitid = node_load($unit);
$form['field_choose_a_service']['und']['#default_value'] = $unitid->nid;

Warning: Invalid argument supplied for foreach() in form_type_checkboxes_value() (line 2389 of /homepages/43/d503288075/htdocs/example.co.uk/includes/form.inc).

The first one actually works, but kicks up the errors. What am I doing wrong?

Essentially I want to check if the nid im passing in the URL as a _GET query matches an option in the entity reference checkbox options, and make that option selected if it does.


1 Answer 1


Try this one:

$unitid = node_load($unit);
$form['field_choose_a_service']['und']['#default_value'] = array($unitid->nid);

And please note that node_load returns an object but not a ID. I see you use $unitid as the variable name. It can make people confused.

  • 1
    I supposed that $unitid should be a node as it is the value from node_load()....
    – Jimmy Ko
    Aug 5, 2016 at 9:48
  • Yep, that worked. I'm guessing that worked because the default value is expecting an array, even if there is only one? Thanks!
    – Collins
    Aug 5, 2016 at 10:33
  • @Collins I think field uses checkboxes rather than checkbox in all cases. And checkboxes only accept array as value. And the error happen on form_type_checkboxes_value() can prove it.
    – Jimmy Ko
    Aug 5, 2016 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.