Thanks to the patch in http://drupal.org/node/970048#comment-5712492 I am able to programmatically insert an addressfield form element into a custom code using the Form API. However, applying the #states property has no effect on the element - it will always display even when it shouldn't. I know that my code is correct because I have exactly the same lines of code for applying the #states property to another element and that works as expected.

I am happy to write a patch to the patch above to enable this functionality but I am unsure how or where to start, and Googling hasn't helped. So either a bit of guidance on how form elements work with the #states property, or some existing code that someone may have written already to do this would be awesome.

Thanks.

Comments

caschbre’s picture

I've been having the same issue. I'm able to use hook_form_alter to add #states attribute to other fields, however I'm not able to with the addressfield.

@melissavdh... did you get anywhere with this?

caschbre’s picture

So far my work-around is to use hook_form_alter to...

1) Copy the field to a variable
2) Unset the field from the $form
3) Create a fieldset element
4) Copy te variable as a child of the fieldset
5) Attach the #states on the fieldset.

melissavdh’s picture

The only way I was able to achieve this was to put the addressfield element inside a fieldset or a container and then apply #states to that fieldset or container.

caschbre’s picture

Yeah, that's the way I had to go about it.

rszrama’s picture

Title: Improve the addressfield form element so that the #states property can be applied » Make the addressfield form element a container so #states can be applied

Updating the title.