0

I am attaching a few fields from the user entity to a custom form with the following:

$account = user_load($user->uid);
foreach ($fields_to_collect as $field) {
  field_attach_form('user', $account, $form, $form_state, NULL, [
    'field_name' => $field
  ]);
}

This works fine except for date fields (I am using the date_popup widget but I haven't yet narrowed that down as responsible). Attaching a date field results in the following error: Warning: implode(): Invalid arguments passed in date_limit_format() (line 2262 of sites/all/modules/date/date_api/date_api.module). The date field is just shown as a text field with the text Format shown as a description.

The error is due to $granularity being NULL. On, for example, the user account edit page which has this field, $granularity is something like:

[
  'month',
  'day',
  'year'
]

Thanks for any help/suggestions.

1 Answer 1

1

Unsurprisingly, it was my error.

Further up in my function I was building $form_state with form_state_defaults(). Once I provided a proper form_state everything worked as expected.

1
  • In my case, I forgot to pass $form_state as a reference in the form function definition -- &$form_state solved the error
    – argiepiano
    Commented Sep 22, 2018 at 16:04

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.