aDependentBlocks = $aDependentBlocks; $this->oFormBuilder = $oFormBuilder; // add form ready listener $this->AddFormReadyListener(); } /** * Add form ready listener. * * Listen the form PRE_SET_DATA * First event from Symfony framework, we know that the form is built at this step. * * @return void */ private function AddFormReadyListener(): void { // Initialize the dependencies listeners once the form is built $this->oFormBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { // dependencies map $this->aDependenciesMap = new DependencyMap($this->aDependentBlocks); /** Iterate throw listened blocks */ foreach ($this->aDependenciesMap->GetListenedBlockNames() as $sOutputBlockName) { // Listen the output block POST_SET_DATA & POST_SUBMIT $this->oFormBuilder->get($sOutputBlockName)->addEventListener(FormEvents::POST_SET_DATA, $this->GetEventListeningCallback()); $this->oFormBuilder->get($sOutputBlockName)->addEventListener(FormEvents::POST_SUBMIT, $this->GetEventListeningCallback()); } }); } /** * Get the listening callback. * * @return callable */ private function GetEventListeningCallback(): callable { return function (FormEvent $oEvent) { // Get the event type $sEventType = FormHelper::GetEventType($oEvent); // Get the form $oForm = $oEvent->getForm(); /** Iterate throw dependencies map... */ $sOutputBlockName = $oForm->getName(); $oOutputBlock = $this->oFormBuilder->GetFormBlock($sOutputBlockName); foreach ($this->aDependenciesMap->GetOutputsForBlock($sOutputBlockName) as $sOutputName) { $oOutput = $oOutputBlock->GetOutput($sOutputName); $oOutput->UpdateOutputValue($oEvent->getData(), $sEventType); } return; }; } }