'Return a list of fields, including a field for selecting
a realm entity (such as an organisation or office).
@return: list of Fields'

def get_form_fields(self):

    s3db = current.s3db
    fields = super(S3PersonRoleManager, self).get_form_fields()
    if (not self.entity):
        options = s3db.pr_get_entities(pe_ids=self.realm, types=self.ENTITY_TYPES, group=True)
        nice_name = s3db.table('pr_pentity').instance_type.represent
        filtered_options = []
        for (entity_type, entities) in options.items():
            entities = Storage([(entity_id, entity_name) for (entity_id, entity_name) in entities.items() if (entity_id not in self.assigned_roles)])
            filtered_options.append((nice_name(entity_type), entities))
        object_field = Field('foreign_object', current.T('Entity'), requires=IS_IN_SET(self.objects), widget=(lambda field, value: S3GroupedOptionsWidget.widget(field, value, options=filtered_options)))
        fields.insert(0, object_field)
    return fields
