public function edit(Request $request, $contenttypeslug, $id)
{
// Is the record new or existing
$new = empty($id);
// Test the access control
if ($response = $this->checkEditAccess($contenttypeslug, $id)) {
return $response;
}
// Set the editreferrer in twig if it was not set yet.
$this->setEditReferrer($request);
// Get the ContentType object
$contenttype = $this->getContentType($contenttypeslug);
// Save the POSTed record
if ($request->isMethod('POST')) {
$this->validateCsrfToken();
$formValues = $request->request->all();
$returnTo = $request->get('returnto');
$editReferrer = $request->get('editreferrer');
return $this->recordSave()->action($formValues, $contenttype, $id, $new, $returnTo, $editReferrer);
}
try {
// Get the record
$repo = $this->getRepository($contenttypeslug);
} catch (InvalidRepositoryException $e) {
$this->flashes()->error(Trans::__('contenttypes.generic.not-existing', ['%contenttype%' => $contenttypeslug]));
return $this->redirectToRoute('dashboard');
}
if ($new) {
$content = $repo->create(['contenttype' => $contenttypeslug, 'status' => $contenttype['default_status']]);
} else {
$content = $repo->find($id);
if ($content === false) {
// Record not found, advise and redirect to the dashboard
$this->flashes()->error(Trans::__('contenttypes.generic.not-existing', ['%contenttype%' => $contenttypeslug]));
return $this->redirectToRoute('dashboard');
}
}
// We're doing a GET
$duplicate = $request->query->get('duplicate', false);
$context = $this->recordEdit()->action($content, $content->getContenttype(), $duplicate);
return $this->render('@bolt/editcontent/editcontent.twig', $context);
}