2

I would like to overload the method FOS FlashListener::getSubscribedEvents(), just for comment the REGISTRATION_COMPLETED line:

// vendor/friendsofsymfony/user-bundle/EventListener/FlashListener.php

public static function getSubscribedEvents() {
    return array(
        FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash',
        FOSUserEvents::GROUP_CREATE_COMPLETED => 'addSuccessFlash',
        FOSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash',
        FOSUserEvents::GROUP_EDIT_COMPLETED => 'addSuccessFlash',
        FOSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash',
        FOSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash',
        FOSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash',
    );
}

There is few infos there:

// vendor/friendsofsymfony/user-bundle/Resources/config/flash_notifications.xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="fos_user.listener.flash" class="FOS\UserBundle\EventListener\FlashListener">
            <tag name="kernel.event_subscriber" />
            <argument type="service" id="session" />
            <argument type="service" id="translator" />
        </service>
    </services>
</container>

I have done that :

<?php
// src/XXXX/UserBundle/EventListener/FlashListener.php
namespace XXXX\UserBundle\EventListener;
use FOS\UserBundle\EventListener\FlashListener AS BaseListener;
use FOS\UserBundle\FOSUserEvents;
class FlashListener extends BaseListener {
    public static function getSubscribedEvents() {
        return [
            FOSUserEvents::CHANGE_PASSWORD_COMPLETED => 'addSuccessFlash',
            FOSUserEvents::GROUP_CREATE_COMPLETED => 'addSuccessFlash',
            FOSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash',
            FOSUserEvents::GROUP_EDIT_COMPLETED => 'addSuccessFlash',
            FOSUserEvents::PROFILE_EDIT_COMPLETED => 'addSuccessFlash',
            // FOSUserEvents::REGISTRATION_COMPLETED => 'addSuccessFlash',
            FOSUserEvents::RESETTING_RESET_COMPLETED => 'addSuccessFlash',
        ];
    }
}

and that :

#src/XXXX/XXXXXBundle/Resources/config/services.yml
services:
    fos_user.listener.flash:
        class: XXXX\UserBundle\EventListener\FlashListener
        arguments: ['@session','@translator']
        tags:
            - { name: kernel.event_subscriber }

But I still have the message after a subscription.

Could somebody help me please?

Nissa
  • 4,636
  • 8
  • 29
  • 37

2 Answers2

1

Because of the way that event subscribers work, that may not be possible, even though that override looks to be the best-practice way to override a normal service, if there wasn't a .class parameter.

Instead, you may need to override it a a deeper level, within your bundle's own CompilerPass.

// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
namespace Acme\DemoBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class OverrideServiceCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('original-service-id');
        $definition->setClass('Acme\DemoBundle\YourService');
    }
}

See also How to override FOSUserBundle's EmailConfirmationListener

Community
  • 1
  • 1
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
0

Thank you very much for your help !

I have done a durty hook (waiting to have more time to work on this, i will try your solution later) :

<?php // src/ACME/UserBundle/EventListener/FlashListener.php

namespace ACME\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class FlashListener implements EventSubscriberInterface {
    private $router;
    private $session;

    public function __construct(UrlGeneratorInterface $router, Session $session) {
        $this->router = $router;
        $this->session = $session;
    }
    public static function getSubscribedEvents() {
        return [
            FOSUserEvents::REGISTRATION_COMPLETED => ['removeSuccessFlash', -1]
        ];
    }

    public function removeSuccessFlash(Event $event, $eventName = null) {
        // on supprime le dernier message success : registration.flash.user_created
        // méthode moche, essayer de surcharger la méthode
        // FOS\UserBundle\EventListener\FlashListener::getSubscribedEvents()
        $flashBag = $this->session->getFlashBag();

        if ($flashBag->peek('success')) {
            $flashes = $flashBag->all();
            array_pop($flashes['success']);
            if (!isset($flashes['success'][0])) {
                unset($flashes['success']);
            }
            if ($flashes) {
                $flashBag->setAll($flashes);
            }
        }
    }
}