Commit d4bcfb7d authored by Melissa Bent's avatar Melissa Bent
Browse files

Issue #3146351 by merauluka: Drupal 9 readiness and stability updates to module

parent 1567733b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
name: byteplant Email validations
type: module
description: 'Email validations features.'
core: 8.x
core_version_requirement: ^8 || ^9
configure: byteplant_email_validation.setting_form
+26 −13
Original line number Diff line number Diff line
@@ -7,6 +7,10 @@

use Drupal\Core\Form\FormStateInterface;

define('BYTEPLANT_OK_VALID_ADDRESS', 200);
define('BYTEPLANT_OK_CATCH_All_ACTIVE', 207);
define('BYTEPLANT_OK_CATCH_All_TEST_DELAYED', 215);

/**
 * Implements hook_form_alter().
 */
@@ -27,15 +31,23 @@ function byteplant_email_validation_form_alter(&$form, &$form_state, $form_id) {
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function byteplant_email_validation(&$form, FormStateInterface $form_state) {
  $email_validator = \Drupal::service('byteplant_email_validation');
  // Find status details in https://www.email-validator.net/email-verification-results.html.
  $valid_status = [
    BYTEPLANT_OK_VALID_ADDRESS,
    BYTEPLANT_OK_CATCH_All_ACTIVE,
    BYTEPLANT_OK_CATCH_All_TEST_DELAYED,
  ];
  foreach ($form['elements'] as $id => $element) {
    if (!empty($element['#type']) && in_array($element['#type'], [
  byteplant_email_validation_process_fields($form, $form_state, $valid_status);
}

function byteplant_email_validation_process_fields(&$form, FormStateInterface $form_state, $valid_status) {
  $email_validator = \Drupal::service('byteplant_email_validation');
  foreach ($form as $id => $element) {
    if (is_array($element) && isset($element['#type'])) {
      if ($element['#type'] == 'container') {
        byteplant_email_validation_process_fields($form[$id], $form_state, $valid_status);
      }
      elseif (in_array($element['#type'], [
        'webform_email_confirm',
        'email',
        ])
@@ -53,3 +65,4 @@ function byteplant_email_validation(&$form, FormStateInterface $form_state) {
      }
    }
  }
}
+10 −8
Original line number Diff line number Diff line
@@ -5,10 +5,6 @@ namespace Drupal\byteplant_email_validation;
use Drupal\Core\Config\ConfigFactoryInterface;
use GuzzleHttp\Client;

define('BYTEPLANT_OK_VALID_ADDRESS', 200);
define('BYTEPLANT_OK_CATCH_All_ACTIVE', 207);
define('BYTEPLANT_OK_CATCH_All_TEST_DELAYED', 215);

/**
 * Class EmailValidationService.
 *
@@ -80,10 +76,16 @@ class EmailValidationService {
   * @return mixed|string
   */
  public function getRequest($url, $options = NULL) {
    try {
      $client = $this->httpClient;
      $request = $client->get($url, $options);
      $content = $request->getBody()->getContents();
      $content = json_decode($content);
    }
    catch (\Exception $e) {
      watchdog_exception('byteplant_email_validation', $e);
      $content = [];
    }
    return $content;
  }