Craft CMS Asked by Kap1tein on June 24, 2021
I’m locally working on a Craft Plugin with a Page in the Control Panel where I have a form that posts to a controller.
<form action="/actions/import-products/product/create" method="POST" enctype="multipart/form-data">
...
</form>
The controller returns the following at the of it’s action
// Render and return the plugin's 'index.twig' template
return $this->renderTemplate('import-products/index.twig', $variables, View::TEMPLATE_MODE_CP);
However, after the renderTemplate, the CP navigation is broken.
It seems that Craft is using the base URL to build the navigation items. Where it should be localhost/admin/dashboard, it’s localhost/dashboard? How do I fix this?
Changing the Base URL of the site to localhost/admin breaks everything because the default routes are changed to localhost/admin/admin/dashboard.
Alright! I've been able to fix my problem. I did a couple of things wrong so I'm writing this for whoever makes the same mistakes I made and is now trapped in an endless loop of despair and impending doom.
First mistake!
You should leave the action attribute of your form element empty. Call you action by using a hidden input, like this
<input type="hidden" name="action" value="/import-products/product/create">
This will automatically keep the user on your page, so there's no need to redirect in the controller and break the CP while doing it.
Second mistake!
I shouldn't have used $this->renderTemplate() at the end of my controller. It's not the preferred flow of doing it. After your controller did everything it needed to do, you should either stay on the page and display feedback or redirect to another page.
Staying on the page to display feedback can be done by sending variables back to the template. That's done like so:
Craft::$app->getUrlManager()->setRouteParams([
'variable' => $myVariable
]);
return null;
If you want to redirect to a confirm-page. You should use the redirectToPostedUrl. To be able to use this, you need a hidden input field with a url as value. Luckily Craft has a Twig function to do so.
{{ redirectInput(url|hash) }}
The URL needs to be hashed otherwise craft will throw an Invalid Body Parameter error. In your controller you can end your action with:
//You should wrap this in an if that checks if there are no errors
return $this->redirectToPostedUrl($variable);
Correct answer by Kap1tein on June 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP