1

The question was already asked (Possible to redirect elsewhere after saving an entry?) but this was about Craft 2.x.

Up until now I used the following code in my plugin (I took it from one of the comments from the same question):

Craft::$app->getView()->hook('cp.entries.edit', function (array &$context) {
    if (isset ($context['section']['type']) && $context['section']['type'] === 'single')
    {
        Craft::$app->getView()->registerJs("
            $(function () {
                $('input[name=\"redirect\"]').val('/');
            });
        ");
    }
});

The code did the job, but after I updated to 3.1.32.1, it stopped working, every time after entry was saved the following exception was thrown:

HTTP 400 – Bad Request – yii\web\BadRequestHttpException
Request contained an invalid body param

Stacktrace:

yii\web\BadRequestHttpException: Request contained an invalid body param in .../vendor/craftcms/cms/src/web/Request.php:651
Stack trace:
#0 .../vendor/craftcms/cms/src/web/Controller.php(314): craft\web\Request->getValidatedBodyParam('redirect')
#1 .../vendor/craftcms/cms/src/controllers/EntriesController.php(595): craft\web\Controller->redirectToPostedUrl(Object(craft\elements\Entry))
#2 [internal function]: craft\controllers\EntriesController->actionSaveEntry()
#3 .../vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#4 .../vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#5 .../vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller->runAction('save-entry', Array)
#6 .../vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('save-entry', Array)
#7 .../vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module->runAction('entries/save-en...', Array)
#8 .../vendor/craftcms/cms/src/web/Application.php(565): craft\web\Application->runAction('entries/save-en...', Array)
#9 .../vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#10 .../vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#11 .../web/index.php(21): yii\base\Application->run()
#12 {main}

Is there any chance to make it work again on 3.1.32?

1

The redirect url always needs to be hashed:

https://docs.craftcms.com/v3/dev/functions.html#redirectinput

In Twig you can use {{ redirectInput(url) }} which is a shortcut of

<input type="hidden" name="redirect" value="{{ url|hash }}">

But if you need the same in a PHP code you can use

Craft::$app->getSecurity()->hashData($url)

based on the code how the Twig filter defined in craftcms/cms/src/web/twig/Extension.php.

| improve this answer | |
  • One problem, though. Now after the value of the <input name='redirect'> is changed, every time I leave the page, I receive This page is asking you to confirm that you want to leave - data you have entered may not be saved.. Is there a way to solve it? – anvar Jul 31 '19 at 17:35
  • Addition to my previous comment: this behavior is observable only for certain entries, but I could not find any system. Two entries have got exactly the same set of fields, but content is different. One is triggering the alert, another don't. – anvar Jul 31 '19 at 18:02
  • The redirect should be a hidden input and the value is always the given url prefixed with a hash when you check the source in a browser. The other half sounds unrelated to your original question, but this is definitely not how the redirection works. I would look for a script or anything else which changes your form data. Check your headers sent to the action. – terdelyi Aug 4 '19 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.