GetEmail.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\materio_expo\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. // use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Drupal\Core\Url;
  8. /**
  9. * Class GetEmail.
  10. */
  11. class GetEmail extends FormBase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function getFormId() {
  16. return 'get_email';
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function buildForm(array $form, FormStateInterface $form_state, $ref = null) {
  22. // check if email cookie is already present
  23. $session = \Drupal::request()->getSession();
  24. $alreadysetemail = $session->get('materio_expo_email');
  25. if($alreadysetemail){
  26. // https://x-team.com/blog/drupal-goto/
  27. $url = Url::fromRoute('materio_expo.qr_controller_getfile', ['ref'=>$ref], ['absolute' => TRUE]);
  28. return new RedirectResponse($url->toString());
  29. }else{
  30. $form['email'] = [
  31. '#type' => 'email',
  32. // '#title' => $this->t('Email'),
  33. '#attributes' => [
  34. 'placeholder' => $this->t('Email')
  35. ],
  36. '#weight' => '0',
  37. ];
  38. $form['ref'] = [
  39. '#type' => 'hidden',
  40. '#value' => $ref
  41. ];
  42. $form['submit'] = [
  43. '#type' => 'submit',
  44. '#value' => $this->t("Submit"),
  45. ];
  46. return $form;
  47. }
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function validateForm(array &$form, FormStateInterface $form_state) {
  53. parent::validateForm($form, $form_state);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function submitForm(array &$form, FormStateInterface $form_state) {
  59. $values = $form_state->getValues();
  60. //* send form submission by email
  61. $mailManager = \Drupal::service('plugin.manager.mail');
  62. $module = 'materio_expo';
  63. $key = 'expo_getmail_submitted';
  64. $to = \Drupal::config('system.site')->get('mail');
  65. $params['email'] = $values['email'];
  66. $params['message'] = $values['email'];
  67. $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
  68. $send = true;
  69. $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
  70. if ($result['result'] !== true) {
  71. $logger = \Drupal::logger('flag_lists');
  72. $logger->error('There was a problem sending your message.' . $values['mail']);
  73. }
  74. //* record email as session variable
  75. // https://drupal.stackexchange.com/questions/197576/storing-data-session-for-anonymous-user
  76. $session = \Drupal::request()->getSession();
  77. $session->set('materio_expo_email', true);
  78. //* redirect to getfile controller
  79. $ref = $values['ref'];
  80. $form_state->setRedirect('materio_expo.qr_controller_getfile', ['ref'=>$ref]);
  81. }
  82. }