How to attach a date field from another entity to a new form I am attaching a few fields from the user entity to a custom form with the following:

$account = user_load($user-&gt;uid);
foreach ($fields_to_collect as $field) {
  field_attach_form('user', $account, $form, $form_state, NULL, [
    'field_name' =&gt; $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.
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.