Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flash messages do not get reset #2424

Open
a-v-l opened this issue Feb 28, 2023 · 1 comment
Open

Flash messages do not get reset #2424

a-v-l opened this issue Feb 28, 2023 · 1 comment

Comments

@a-v-l
Copy link
Contributor

a-v-l commented Feb 28, 2023

Contao v4.13.15
Isotope v2.8.9

To be able to add a product "Subscription" to the cart, the visitor must be registered as a member and logged in.
Similar to #2269 I use addProductToCollection to check whether the product is a "Subscription" and the user is logged in. If not display a message and return $intQuantity = 0 to prevent the item being added to the cart/collection. This works fine so far:

#services.yml
App\EventListener\ShopEventListener:
    tags:
        - { name: isotope.hook, hook: addProductToCollection }
// src/EventListener/ShopEventListener.php
namespace App\EventListener;
use Isotope\Message;

class ShopEventListener
{
    public function __invoke($objProduct, $intQuantity) {
        $security = \Contao\System::getContainer()->get('security.helper');
        $user = $security->getUser();

        if ($objProduct->digital === "true" && !$user) {
            Message::addInfo('Must be logged in!');
            return 0;
        } else {
            Message::reset();
        }
        return $intQuantity;
    }
}

But after closing the flash message and navigating to any other page of the site (e.g. to /login) the message reappears on the next page, one time.

Also if I remove the Message::reset() line in the __invoke-function and a different product is (successfully) being added to the cart, the message 'Must be logged in!' will still be displayed together with the success message.
Shouldn't closing a message also trigger Message::reset()?

@aschempp
Copy link
Member

Shouldn't closing a message also trigger Message::reset()

Closing a message (in the front end) does not reset the message on the server. But generating the messages for the page should lead to cleared messages. It looks like the message is added after the messages of the current page are generated. Did you enable the "include messages" checkbox in any of your front end modules?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants