I have a select list that I am populating $form['field_my_field']['und']['#options'] in a hook_form_alter.

I was wondering what a good way to set a data attribute per each option would be. I've thought about adding some inline javascript to add it after it's rendered, but I want to do this with PHP and preferably within the hook_form_alter.

Is this possible?

1 Answer 1


Based on theme_select which is the one that constructs and styles the Drupal Form API's select type. It uses the form_select_options function do create the <option> tags.

So you would need to override theme_select and replace the form_select_options function with your own.

Basically, copy the functions and it's content, rename them and do your modifications.

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.