You are here

public function RoleLoginPageSettingsEdit::validateForm in Multiple role login pages 8

@global type $base_url

Parameters

array $form:

FormStateInterface $form_state:

Overrides FormBase::validateForm

File

src/Form/RoleLoginPageSettingsEdit.php, line 139
�* �* Contains \Drupal\role_login_page\Form\RoleLoginPageSettingsEdit. �

Class

RoleLoginPageSettingsEdit
Edit login page form.

Namespace

Drupal\role_login_page\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  global $base_url;
  $rl_id = $this->rlid;
  $url = trim($form_state
    ->getValue([
    'loginmenu_url',
  ]));
  $redirect_path = trim($form_state
    ->getValue([
    'redirect_path',
  ]));
  if ($redirect_path) {
    $redirect_path_exists = \Drupal::service('path.validator')
      ->getUrlIfValid($redirect_path);
    if ($redirect_path_exists) {
      if (!$redirect_path_exists
        ->getRouteName()) {
        $form_state
          ->setErrorByName('redirect_path', $this
          ->t('Please enter a valid redirect path.'));
      }
    }
    else {
      $form_state
        ->setErrorByName('redirect_path', $this
        ->t('Please enter a valid redirect path.'));
    }
  }
  if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0 || strpos($url, 'www') === 0 && strpos($url, '.') || strpos($url, '.')) {
    $form_state
      ->setErrorByName('loginmenu_url', $this
      ->t("@comurl is not a valid URL", [
      '@comurl' => $url,
    ]));
  }
  else {
    $menu_exists = \Drupal::service('path.validator')
      ->getUrlIfValid($url);
    $login_page_exists_query = $this->connection
      ->select('role_login_page_settings', 'rlps');
    $login_page_exists_query
      ->fields('rlps', [
      'rl_id',
    ]);
    $login_page_exists_query
      ->condition('url', $url);
    $login_page_exists_query
      ->condition('rl_id', $rl_id, '<>');
    $login_page_exists = $login_page_exists_query
      ->execute()
      ->fetchObject();
    $current_data_match_query = $this->connection
      ->select('role_login_page_settings', 'rlps');
    $current_data_match_query
      ->fields('rlps', [
      'rl_id',
    ]);
    $current_data_match_query
      ->condition('url', $url);
    $current_data_match_query
      ->condition('rl_id', $rl_id);
    $current_data_match = $current_data_match_query
      ->execute()
      ->fetchObject();
    if ($login_page_exists && !$current_data_match) {
      $form_state
        ->setErrorByName('loginmenu_url', $this
        ->t('The menu URL already exists'));
    }
    elseif (!$login_page_exists && !$current_data_match) {
      if ($menu_exists && $menu_exists
        ->getRouteName()) {
        $form_state
          ->setErrorByName('loginmenu_url', $this
          ->t('The menu URL already exists'));
      }
    }
  }
}