I created a url field on users that is not required and just has one field that is not exposed on the registration form.
The default value is something like http://www.example.dev/
I'm also using user_save to create accounts something like:
$new_user = array(
'name' => $values['name'],
'pass' => user_password(),
'mail' => $values['email'],
'init' => $values['email'],
'status' => 1,
'access' => REQUEST_TIME,
'roles' => array(),
);
$account = user_save(NULL, $new_user);
When I do that I get this error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1: INSERT INTO {field_data_field_google_plus_profile} (entity_type, entity_id, revision_id, bundle, delta, language, field_google_plus_profile_value, field_google_plus_profile_title, field_google_plus_profile_attributes) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, ); Array ( [:db_insert_placeholder_0] => user [:db_insert_placeholder_1] => 30496 [:db_insert_placeholder_2] => 30496 [:db_insert_placeholder_3] => user [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => http://www.example.dev/this-doesnt-exist [:db_insert_placeholder_7] => ) in field_sql_storage_field_storage_write() (line 448 of /home/greggles/workspace/cap/modules/field/modules/field_sql_storage/field_sql_storage.module).
It looks like there are 9 columns listed in the INSERT statement but only 8 values in the Array of placeholders - the missing one appears to be the attributes.
This issue did not cause the form at admin/people/create to fail, so it seems that manually doing a user_save is key to repeating the issue.
I tried to debug url_field_presave a bit and found that the $items was empty when using user_save. So, if we default it then everything works.
Comment | File | Size | Author |
---|---|---|---|
#6 | 1866936-default-value-workaround.patch | 1.66 KB | Dave Reid |
default_url_if_items_empty.patch | 609 bytes | greggles | |
Comments
Comment #1
Dave ReidThat is really really strange. Field modules shouldn't have to load in their default values in the hook_field_presave() at all. It should be handled automatically.
Comment #2
Dave ReidI think this is more of an issue is the 'attributes' key is missing in the default value array itself.
Comment #3
gregglesThat certainly makes sense to me. I don't really know how to fix that.
Comment #4
gregglesOne note: if you decide my proposed patch is the right way to fix this then it also needs work to avoid a notice for cases where there is no default.
Comment #5
Dave ReidI have found the issue. It is a core field API bug, yay!
#1899498: Field default values do not get hook_field_presave() run on them
Comment #6
Dave ReidHere's a patch I think will work for now until 1899498 is fixed. Please test (by going to your field settings and re-saving the default value) and see if this fixes the issue for you.
Comment #7
Dave ReidTested and committed #6 to 7.x-1.x. http://drupalcode.org/project/url.git/commit/2859780
Tempted to leave this open to write an update function which fixes existing URL fields.
Comment #8
Dave ReidAdded an update hook to correct any existing field instances in http://drupalcode.org/project/url.git/commit/32f6b67. Considering this complete now.