You are here

public function UserDefaultPageConfigEntityForm::validateForm in User Default Page 8

Same name and namespace in other branches
  1. 8.2 src/Form/UserDefaultPageConfigEntityForm.php \Drupal\user_default_page\Form\UserDefaultPageConfigEntityForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/UserDefaultPageConfigEntityForm.php, line 164

Class

UserDefaultPageConfigEntityForm
Class UserDefaultPageConfigEntityForm.

Namespace

Drupal\user_default_page\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Get Values.
  $values = $form_state
    ->getValues();
  $form_id = $values['form_id'];
  $roles = $values['user_roles'];
  $users = $values['users'];
  if ($roles == NULL && $users == NULL) {
    $form_state
      ->setErrorByName('user_roles', $this
      ->t("Select atleast one role / user"));
    $form_state
      ->setErrorByName('users', "");
  }
  if ($form_id != 'user_default_page_config_entity_edit_form') {

    // Load all entities belongs to "user_default_page_config_entity".
    $entities_load = $this->entityTypeManager
      ->getStorage('user_default_page_config_entity')
      ->loadMultiple();
    $user_roles = $values['user_roles'];
    $current_user = $users[0]['target_id'];

    // Check roles for any existence.
    foreach ($entities_load as $entity) {

      // Check users for any existence.
      $users_array = $entity
        ->getUsers();
      if (strstr($users_array, $current_user)) {
        $form_state
          ->setErrorByName('users', $this
          ->t("User is already present"));
      }

      // Check roles for any existence.
      if ($entity
        ->getUserRoles() == $user_roles && $user_roles == ' ') {
        global $base_url;
        $url = Url::fromUri($base_url . '/admin/config/user_default_page_config_entity/' . $entity
          ->id() . '/edit');
        $internal_link = Link::fromTextAndUrl($this
          ->t('edit'), $url)
          ->toString();
        $form_state
          ->setErrorByName('user_roles', $this
          ->t("The Role <b>'@user_roles'</b> is already present in @label. You can @edit here", [
          '@user_roles' => $user_roles,
          '@label' => $entity
            ->get('label'),
          '@edit' => $internal_link,
        ]));
      }
    }
  }
  if (!$this->pathValidator
    ->isValid($form_state
    ->getValue('logout_redirect'))) {
    $form_state
      ->setErrorByName('redirect_to', $this
      ->t("The Logout redirect path '@link_path' is either invalid or you do not have access to it.", [
      '@link_path' => $form_state
        ->getValue('logout_redirect'),
    ]));
  }
  if (!$this->pathValidator
    ->isValid($form_state
    ->getValue('login_redirect'))) {
    $form_state
      ->setErrorByName('redirect_to', $this
      ->t("The Login redirect path '@link_path' is either invalid or you do not have access to it.", [
      '@link_path' => $form_state
        ->getValue('login_redirect'),
    ]));
  }
  $login_redirect = $values['login_redirect'];
  $logout_redirect = $values['logout_redirect'];
  if ($login_redirect == NULL && $logout_redirect == NULL) {
    $form_state
      ->setErrorByName('login_redirect', $this
      ->t("Fill Login / Logout Redirection path(s)"));
    $form_state
      ->setErrorByName('logout_redirect', $this
      ->t("Fill Login / Logout Redirection path(s)"));
  }
  parent::validateForm($form, $form_state);
}