You are here

public function UnpublishedNodesRedirectSettingsForm::validateForm in Unpublished Nodes Redirect 2.x

Same name and namespace in other branches
  1. 8 src/Form/UnpublishedNodesRedirectSettingsForm.php \Drupal\unpublished_nodes_redirect\Form\UnpublishedNodesRedirectSettingsForm::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/UnpublishedNodesRedirectSettingsForm.php, line 80

Class

UnpublishedNodesRedirectSettingsForm
Configure example settings for this site.

Namespace

Drupal\unpublished_nodes_redirect\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $content_types = Utils::getNodeTypes();
  foreach ($content_types as $key => $type_name) {

    // Validate whether the provided redirect paths are all internal.
    $element_name = Utils::getNodeTypeKey($type_name);
    $submitted_redirect_path = $form_state
      ->getValue($element_name);
    $test = UrlHelper::isExternal($submitted_redirect_path);
    if (!empty($submitted_redirect_path) && UrlHelper::isExternal($submitted_redirect_path)) {
      $form_state
        ->setErrorByName($element_name, $this
        ->t('The path provided needs to be an internal path.'));
    }

    // If a path is provided, make sure a response code is selected.
    $element_name = Utils::getResponseCodeKey($type_name);
    $submitted_response_code = $form_state
      ->getValue($element_name);
    if (!empty($submitted_redirect_path) && $submitted_response_code == 0) {
      $form_state
        ->setErrorByName($element_name, $this
        ->t('Please select a response code for the @type content type.', [
        '@type' => $type_name,
      ]));
    }
  }
}