Bolt\Controller\Backend\Records::edit PHP Method

edit() public method

Edit a record, or create a new one.
public edit ( Request $request, string $contenttypeslug, integer $id ) : TemplateResponse | RedirectResponse
$request Symfony\Component\HttpFoundation\Request The Symfony Request
$contenttypeslug string The content type slug
$id integer The content ID
return Bolt\Response\TemplateResponse | Symfony\Component\HttpFoundation\RedirectResponse
    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);
    }