I have a slow load time in a node form and I suspect the reason is that I am populating entity reference select lists using views & therefore a number of queries are being run to populate those select lists when the form is loaded.

As most of the entity reference fields don't need to be on the node-add form but only the node-edit form I tried using unset($form['field']) and hook_form_alter to remove those fields from the $form array but there is still a slow load time.

Could it be that the fields that get unset in hook_form_alter are still running their views queries?

Thanks very much and have a great day!

1 Answer 1


Short Answer: Unsetting the form element won't stop the view from being called. You may want to use an autocomplete widget instead if that is an option (if only to test your theory is correct - that the views are slowing the page load).

Long Answer: When hook_form_alter is called, what you get is a form that contains elements from the Field API. Modules such as Entity Reference typically do not create new Form API elements - instead they provide field widgets.

Such modules contain code that generate their widget using the base elements in the Form API. This is typically done when field_attach_form is called, and it is at this point that the view would be called.

This is done before hook_form_alter is called. So what you get in form alter is the low level Form API elements - not the high level widget. Unsetting the field won't make a difference, since the low-level elements that contain the list of options have already been created.

It is possible that some module would actually use hook_form_alter to populate their widget - I have seen this done, and must say it is very bad practice - but I do not think this is the case with Entity Reference.

  • Thanks so much Alice.That is the best answer I've ever seen on any forum, ever and I really appreciate your time and smarts!
    – I am Bish
    Commented Apr 10, 2013 at 13:33

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.