Skip to content

Latest commit

 

History

History
51 lines (41 loc) · 1.37 KB

30_Events.md

File metadata and controls

51 lines (41 loc) · 1.37 KB

Events

services:
    App\FormBuilder\OutputWorkflowEventListener:
        tags:
            - { name: kernel.event_subscriber }
<?php

namespace App\FormBuilder;

use FormBuilderBundle\FormBuilderEvents;
use FormBuilderBundle\Event\OutputWorkflow\ChannelSubjectGuardEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OutputWorkflowEventListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            FormBuilderEvents::OUTPUT_WORKFLOW_GUARD_SUBJECT_PRE_DISPATCH  => 'checkSubject',
        ];
    }

    public function checkSubject(ChannelSubjectGuardEvent $event)
    {
        // this could be a data object but also a field collection
        $subject = $event->getSubject();

        if($event->getWorkflowName() === 'my_weird_workflow') {
            $event->shouldFail('My invalid message for a specific channel! Allow further channels to pass!', true);
            return;
        }
    
        if($event->getWorkflowName() === 'my_second_weird_workflow') {
            $event->shouldFail('My invalid message! If this happens, no further channel will be executed!', false);
            return;
        }

        if($event->getChannelType() === 'object') {
            // silently skip channel
            $event->shouldSuspend();
            return;
        }
    }
}