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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

That 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.

Dave Reid’s picture

I think this is more of an issue is the 'attributes' key is missing in the default value array itself.

greggles’s picture

Status: Needs review » Needs work

That certainly makes sense to me. I don't really know how to fix that.

greggles’s picture

One 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.

-  if (empty($items)) {
+  if (empty($items) && !empty($instance['default_value'])) {
Dave Reid’s picture

Status: Needs work » Active

I 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

Dave Reid’s picture

Assigned: Unassigned » Dave Reid
Priority: Normal » Major
Status: Active » Needs review
FileSize
1.66 KB

Here'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.

Dave Reid’s picture

Status: Needs review » Needs work

Tested 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.

Dave Reid’s picture

Status: Needs work » Fixed

Added an update hook to correct any existing field instances in http://drupalcode.org/project/url.git/commit/32f6b67. Considering this complete now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.