public function createCustomFieldAction(Request $request, $id = null)
{
/** @var User $user */
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
if ($id) {
/** @var CustomField $customField */
$customField = $em->find('OjsUserBundle:CustomField', $id);
if (!$customField) {
throw new NotFoundException();
}
if ($customField->getUserId() != $user->getId()) {
throw new AccessDeniedException();
}
} else {
$customField = new CustomField();
}
$customFieldForm = $this->createForm(new CustomFieldType(), $customField, ['user' => $user->getId()]);
if ($request->isMethod('POST')) {
$customFieldForm->handleRequest($request);
if ($customFieldForm->isValid()) {
$customField->setUser($user);
$em->persist($customField);
$em->flush();
return $this->redirect($this->get('router')->generate('ojs_user_custom_field'));
} else {
$session = $this->get('session');
$bag = $session->getFlashBag();
$bag->add('error', $this->get('translator')->trans("An error has occured!"));
$session->save();
}
}
$data = [];
$data['form'] = $customFieldForm->createView();
return $this->render("OjsSiteBundle:User:create_custom_field.html.twig", $data);
}