|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @file |
| 4 | + * Contains \Drupal\slack_invite\Form\SlackInviteForm. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Drupal\slack_invite\Form; |
| 8 | + |
| 9 | +use Drupal\Core\Http\Client; |
| 10 | +use Drupal\Core\Form\FormBase; |
| 11 | +use Drupal\Core\Form\FormStateInterface; |
| 12 | +use Exception; |
| 13 | +use Drupal\Core\Url; |
| 14 | + |
| 15 | +/** |
| 16 | + * Builds the search form for the search block. |
| 17 | + */ |
| 18 | +class SlackInviteForm extends FormBase { |
| 19 | + |
| 20 | + /** |
| 21 | + * {@inheritdoc} |
| 22 | + */ |
| 23 | + public function getFormID() { |
| 24 | + return 'slack_invite_form'; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 31 | + $config = $this->config('slack_invite.settings'); |
| 32 | + |
| 33 | + $form['#action'] = Url::fromRoute('<current>', ['query' => $this->getDestinationArray(), 'external' => FALSE])->toString(); |
| 34 | + $form['slack_email'] = [ |
| 35 | + '#type' => 'textfield', |
| 36 | + '#title' => $this->t('Email'), |
| 37 | + '#description' => $this->t('Enter email address for slack invite'), |
| 38 | + '#required' => TRUE, |
| 39 | + ]; |
| 40 | + |
| 41 | + $form['actions'] = [ |
| 42 | + '#type' => 'actions', |
| 43 | + ]; |
| 44 | + $form['actions']['submit'] = [ |
| 45 | + '#type' => 'submit', |
| 46 | + '#value' => $this->t('Send') |
| 47 | + ]; |
| 48 | + return $form; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + */ |
| 54 | + public function validateForm(array &$form, FormStateInterface $form_state) { |
| 55 | + $email = $form_state->getValue('slack_email'); |
| 56 | + if (!\Drupal::service('email.validator')->isValid($email)) { |
| 57 | + $form_state->setErrorByName('slack_email', $this->t('Enter email address in valid format (ex. example@example.com)')); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + */ |
| 64 | + public function submitForm(array &$form, FormStateInterface $form_state) { |
| 65 | + $slack_invite = \Drupal::service('slack_invite'); |
| 66 | + $slack_invite->send($form_state->getValue('slack_email')); |
| 67 | + } |
| 68 | +} |
0 commit comments