Guest User

Untitled

a guest
Jan 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupalharlib_newsletter_signupForm;
  4.  
  5. use DrupalCoreFormFormBase;
  6. use DrupalCoreFormFormStateInterface;
  7.  
  8. /**
  9. * Class NewsletterSignup.
  10. */
  11. class NewsletterSignup extends FormBase {
  12.  
  13. public $uniqueIdentifier = 'no_tagline';
  14.  
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function getFormId() {
  19. return 'newsletter_signup_' . $this->uniqueIdentifier;
  20. }
  21.  
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function buildForm(array $form, FormStateInterface $form_state) {
  26.  
  27. if ($this->uniqueIdentifier == "with_tagline") {
  28. $show_tagline = TRUE;
  29. }
  30. else {
  31. $show_tagline = FALSE;
  32. }
  33.  
  34. $wrapper = 'ajax-wrapper-' . $this->uniqueIdentifier;
  35.  
  36. $form['#prefix'] = '<section class="hl__single-input-form"><div id="' . $wrapper . '">';
  37. $form['#suffix'] = '</div></section>';
  38.  
  39. $form['#attributes']['class'][] = 'hl__single-input-form__form';
  40. $form['#attributes']['novalidate'] = 'novalidate';
  41.  
  42. $form['title'] = [
  43. '#type' => 'label',
  44. '#for' => 'edit-email-address',
  45. '#title' => 'Stay in the know',
  46. '#attributes' => [
  47. 'class' => [
  48. 'hl__label',
  49. 'hl__label--inline',
  50. ],
  51. ],
  52. ];
  53.  
  54. if ($show_tagline) {
  55. $form['tagline'] = [
  56. '#markup' => '<div class="hl__single-input-form__helper-text">Sign up for email updates from MySite</div>',
  57. ];
  58. }
  59.  
  60. $errors = $form_state->getErrors();
  61.  
  62. if ($errors = $form_state->getErrors()) {
  63. $form['errors'] = [
  64. '#markup' => $errors['email_address'],
  65. ];
  66. }
  67.  
  68. $form['email_address'] = [
  69. '#type' => 'email',
  70. '#size' => NULL,
  71. '#required' => TRUE,
  72. '#attributes' => [
  73. 'placeholder' => $this->t('Email'),
  74. 'class' => [
  75. 'hl__input',
  76. 'hl__input--inline',
  77. 'js-is-required',
  78. ],
  79. 'data-twig-suggestion' => 'newsletter_signup_email',
  80. ],
  81. '#prefix' => '<div class="hl__single-input-form__input-group">',
  82. ];
  83.  
  84. $form['submit'] = [
  85. '#type' => 'button',
  86. '#value' => $this->t('Sign up'),
  87. '#submit' => ['::submitForm'],
  88. '#attributes' => [
  89. 'class' => [
  90. 'hl__button',
  91. 'hl__button--small',
  92. 'hl__button--inline',
  93. ],
  94. 'data-twig-suggestion' => 'newsletter_signup_submit',
  95. ],
  96. '#suffix' => '</div>',
  97. '#ajax' => [
  98. 'wrapper' => $wrapper,
  99. 'method' => 'replace',
  100. 'callback' => '::ajaxRebuildForm',
  101. 'progress' => [
  102. '#type' => 'none',
  103. ],
  104. ],
  105. ];
  106.  
  107. return $form;
  108. }
  109.  
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function validateForm(array &$form, FormStateInterface $form_state) {
  114. $email_value = $form_state->getValue('email_address');
  115.  
  116. if (empty($email_value) || !Drupal::service('email.validator')->isValid($email_value)) {
  117. $form_state->setError($form['email_address'], $this->t('Please enter a valid email address.'));
  118. }
  119.  
  120. if (mailchimp_is_subscribed('XXXXXXX', $email_value, TRUE)) {
  121. $form_state->setError($form['email_address'], $this->t('You are already subscribed to our newsletter.'));
  122. }
  123. }
  124.  
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function submitForm(array &$form, FormStateInterface $form_state) {
  129. $result = mailchimp_subscribe('c7d23910fe', $form_state->getValue('email_address'));
  130.  
  131. if (empty($result)) {
  132. $form_state->setError($form['email_address'], $this->t('You are already subscribed to our newsletter.'));
  133. }
  134. else {
  135. drupal_set_message($this->t('Thank you for subscribing to our newsletter!'));
  136. }
  137. }
  138.  
  139. public function ajaxRebuildForm(array $form, FormStateInterface $formState) {
  140. return $form;
  141. }
  142.  
  143. }
Add Comment
Please, Sign In to add comment