src/Blocks/ValuationForm/ValuationFormBlock.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Blocks\ValuationForm;
  3. use App\Exception\RedirectException;
  4. use App\Form\Frontend\Content\Pages\ValuationForm;
  5. class ValuationFormBlock extends \App\Blocks\Block
  6. {
  7.     static function getName()
  8.     {
  9.         return "Valuation Form";
  10.     }
  11.     static function getDescription()
  12.     {
  13.         return "Valuation form with submissions sent via email";
  14.     }
  15.     public function run()
  16.     {
  17.         // Create form
  18.         $valuationForm $this->formFactory->create(ValuationForm::class, []);
  19.         // Handle form request
  20.         $valuationForm->handleRequest($this->requestStack->getCurrentRequest());
  21.         // Form submitted & valid?
  22.         if($valuationForm->isSubmitted() && $valuationForm->isValid())
  23.         {
  24.             // Send the email
  25.             $this->mailerHelper->send($this->getRecipient(), "Valuation Form Submission""valuationForm.html.twig", [
  26.                 "submitted" => [
  27.                     "name" => $valuationForm->get('name')->getData(),
  28.                     "email" => $valuationForm->get('email')->getData(),
  29.                     "phone" => $valuationForm->get('phone')->getData(),
  30.                     "valuationType" => $valuationForm->get('valuationType')->getData(),
  31.                     "propertyType" => $valuationForm->get('propertyType')->getData(),
  32.                     "propertyAddress" => $valuationForm->get('propertyAddress')->getData()
  33.                 ]
  34.             ]);
  35.             throw new RedirectException(new \Symfony\Component\HttpFoundation\RedirectResponse('/valuation?sent=1'));
  36.         }
  37.         $this->valuationForm $valuationForm;
  38.     }
  39.     public function getDirectory()
  40.     {
  41.         return "ValuationForm";
  42.     }
  43.     public function applyDataDefaults($data)
  44.     {
  45.         return array_merge(array(
  46.             "recipient" => "",
  47.             "classes" => ""
  48.         ), $data);
  49.     }
  50.     public function renderFrontend()
  51.     {
  52.         return $this->twig->render('Blocks/' $this->getDirectory() . '/Frontend/renderFrontend.html.twig', array(
  53.             'block' => array(
  54.                 'identifier' => $this->block->getId(),
  55.                 'block' => $this->block,
  56.                 'instance' => $this
  57.             ),
  58.             'data' => $this->applyDataDefaults($this->block->getData()),
  59.             'current_path' => strtok($_SERVER["REQUEST_URI"], '?'),
  60.             'valuationForm' => $this->valuationForm->createView(),
  61.         ));
  62.     }
  63.     public function getRecipient()
  64.     {
  65.         $data $this->block->getData();
  66.         return $data['recipient'];
  67.     }
  68. }