I have a content type of Location that has a list (text) field with options for Full Service or Quick Stop.
On another content type, I have an entity reference field that pulls in all the locations (using a select list widget).
On the node creation/edit form, I am trying to group those locations with optgroups into Full Service and Quick Stop based on the value of the nodes in the entity reference field (i.e. a location with field_location_type of "Quick Stop" would go into the Quick Stop optgroup).
Right now I am manually sorting them with the following, but it would be nice to do it automatically via my field_location_type field so that new options are automatically added.
$form['field_location']['und']['#options'] = array ( 'Full Service' => array( 19 => 'Main Street', 20 => 'Oak Hills', 21 => 'Cherry Trail', ), 'Quick Stop' => array( 22 => 'Montreal', 26 => 'Washington Place', ), );