Lately I've been building a Drupal 7 module with the Entity API but I've run in to a problem. When debugging an edit form I've noticed that the load method is called twice which is causing an error.
Recoverable fatal error: Object of class stdClass could not be converted to string in DatabaseStatementBase->execute() (regel 2039 van /drupal7/includes/database/database.inc).
This is caused by FooController::load being executed twice.
Underneath is the code I've been using.
<?php
function foo_menu() {
$items = array();
...
$items['admin/foo/%foo/edit'] = array(
'title' => 'Edit foo',
'page callback' => 'foo_edit',
'page arguments' => array(2),
'access arguments' => array('administer content'),
'type' => MENU_CALLBACK,
);
...
return $items;
}
function foo_edit($foo) {
return drupal_get_form('foo_edit_form', $foo);
}
function foo_edit_form($form, &$form_state, $foo) {
$form['#foo'] = $foo;
$form_state['values'] = $foo;
$form['id'] = array(
'#type' => 'hidden',
'#value' => $foo->id,
);
...
$form['picture'] = array(
'#type' => 'file',
'#title' => t('Picture'),
'#default_value' => $foo->picture->filename,
);
...
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}