This lines should be moved just before 
if ( $FieldConfig->{TreeView} ) {

where they are now makes no sense.