protected function getFormEntity(Request $request)
    {
        if ($this->formEntity === null) {
            $entityId = $request->get('id');
            $repository = $this->getRepository();
            $entity = null;
            $id = 0;

            if (!empty($entityId) && $entityId !== null) {
                $entity = $repository->findOneById($entityId);
                $id = $entity->getId();
            }

            if (!is_object($entity) || empty($id)) {
                // new form, return empty entity
                $entity_class_name = $repository->getClassName();
                $entity = new $entity_class_name();
            }

            $this->formEntity = $entity;
        }

        return $this->formEntity;
    }