I'm having a problem with duplicate ID's for a custom field widget.

The scenario: I have an order form, that contains 2 field collections "pick-up" & "delivery". Both these collections contain the same field "field_delivery_profile" which is a customer profile reference field with a custom field widget. In the hook_field_widget_form regardless of whether I assign the element an id or not, the ID is the same for both occurrences of the field in the form.

One thing is the breaking W3C standard but the most crucial one for me right now, is that I need to check both fields value change through JS and have to be able at least predict the IDs. Is there maybe a way to get the parent field-collection (that's containing the field) information for assigning a better id?



  • Use drupal_html_id, that will guarantee uniqueness
    – Clive
    Mar 3, 2016 at 10:33

1 Answer 1


Drupal has a helper function for that:


Prepares a string for use as a valid HTML ID and guarantees uniqueness.

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.