fix Notice: Undefined variable: field

It looks like you are trying to use $field['key'], but it's not being passed. Should be $field_key