src/Blocks/ContactForm/ContactFormBlock.php line 60

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