Skip to content

Commit

Permalink
Merge pull request #3625 from ostark/redirect
Browse files Browse the repository at this point in the history
Idea: Allow error/notice message with the redirect tag
  • Loading branch information
brandonkelly authored Jan 15, 2019
2 parents 25566f7 + b2d5dae commit 0869136
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-v3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
- The `app/migrate` web action now applies pending `project.yaml` changes, if the `useProjectConfigFile` config setting is enabled.
- The `svg()` function now strips `<title>`, `<desc>`, and comments from the SVG document as part of its sanitization process.
- The `svg()` function now supports a `class` argument, which will add a class name to the root `<svg>` node. ([#3174](https://github.com/craftcms/cms/issues/3174))
- The `{% redirect %}` tag now supports `with notice` and `with error` params for setting flash messages. ([#3625](https://github.com/craftcms/cms/pull/3625))
- `info` buttons can now also have a `warning` class.
- User permission definitions can now include `info` and/or `warning` keys.
- The old “Administrate users” permission has been renamed to “Moderate users”.
Expand Down
10 changes: 10 additions & 0 deletions docs/dev/tags/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ You can customize which status code accompanies your redirect response by typing
```twig
{% redirect "pricing" 301 %}
```

### Flash Messages

You can optionally set flash messages that will show up for the user on the next request using the `with notice` and/or `with error` params:

```twig
{% if not currentUser.isInGroup('members') %}
{% redirect "pricing" 301 with notice "You have to be a member to access that!" %}
{% endif %}
```
17 changes: 16 additions & 1 deletion src/web/twig/nodes/RedirectNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,23 @@ class RedirectNode extends \Twig_Node
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);

if ($this->hasNode('error')) {
$compiler
->write('\Craft::$app->getSession()->setError(')
->subcompile($this->getNode('error'))
->raw(");\n");
}

if ($this->hasNode('notice')) {
$compiler
->write('\Craft::$app->getSession()->setNotice(')
->subcompile($this->getNode('notice'))
->raw(");\n");
}

$compiler
->addDebugInfo($this)
->write('\Craft::$app->getResponse()->redirect(' . UrlHelper::class . '::url(')
->subcompile($this->getNode('path'))
->raw('), ')
Expand Down
7 changes: 7 additions & 0 deletions src/web/twig/tokenparsers/RedirectTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function parse(\Twig_Token $token)
$nodes['httpStatusCode'] = new \Twig_Node_Expression_Constant(302, 1);
}

// Parse flash message(s)
while ($stream->test(\Twig_Token::NAME_TYPE, 'with')) {
$stream->next();
$type = $stream->expect(\Twig_Token::NAME_TYPE, ['notice', 'error'])->getValue();
$nodes[$type] = $this->parser->getExpressionParser()->parseExpression();
}

$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);

return new RedirectNode($nodes, [], $lineno, $this->getTag());
Expand Down

0 comments on commit 0869136

Please sign in to comment.