You are here

check_dns.module in Check DNS 8

Same filename and directory in other branches
  1. 7 check_dns.module

Contains check_dns.module.

Prevents user registration with invalid email domain.

File

check_dns.module
View source
<?php

/**
 * @file
 * Contains check_dns.module.
 *
 * Prevents user registration with invalid email domain.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function check_dns_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the check_dns module.
    case 'help.page.check_dns':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Checks if email domain has DNS Record before user registration.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function check_dns_form_user_register_form_alter(&$form, FormStateInterface $form_state) {

  // Custom validate function.
  $form['#validate'][] = 'check_dns_user_register_validate';
}

/**
 * Custom validation function.
 *
 * Checks if the domain in the email address is on a list of allowed domains.
 */
function check_dns_user_register_validate($form, FormStateInterface &$form_state) {
  $mail = $form_state
    ->getValue('mail');
  $result = \Drupal::service('check_dns.service')
    ->validateEmail($mail);
  if (!$result) {

    // If no record is found.
    $form_state
      ->setErrorByName('mail', t('Your email domain is not recognised. Please enter a valid email id.'));
  }
}

Functions