You are here

protected function MenuLinkConfigForm::doValidate in Config menu link 8

Validates the form, both on the menu link edit and content menu link form.

$form is not currently used, but passed here to match the normal form validation method signature.

Parameters

array $form: A nested array form elements comprising the form.

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

2 calls to MenuLinkConfigForm::doValidate()
MenuLinkConfigForm::validate in src/Plugin/Menu/Form/MenuLinkConfigForm.php
MenuLinkConfigForm::validateConfigurationForm in src/Plugin/Menu/Form/MenuLinkConfigForm.php
Form validation handler.

File

src/Plugin/Menu/Form/MenuLinkConfigForm.php, line 292
Contains \Drupal\menu_link_config\Plugin\Menu\Form\MenuLinkConfigForm.

Class

MenuLinkConfigForm

Namespace

Drupal\menu_link_config\Plugin\Menu\Form

Code

protected function doValidate(array $form, FormStateInterface $form_state) {
  $extracted = $this
    ->extractUrl($form_state
    ->getValue('url'));

  // If both URL and route_name are empty, the entered value is not valid.
  $valid = FALSE;
  if ($extracted['url']) {

    // This is an external link.
    $valid = TRUE;
  }
  elseif ($extracted['route_name'] == '<nolink>') {

    // This is a nolink.
    $valid = TRUE;
  }
  elseif ($extracted['route_name']) {

    // Users are not allowed to add a link to a page they cannot access.
    $valid = $this->accessManager
      ->checkNamedRoute($extracted['route_name'], $extracted['route_parameters'], $this->account);
  }
  if (!$valid) {
    $form_state
      ->setErrorByName('url', $this
      ->t("The path '@link_path' is either invalid or you do not have access to it.", [
      '@link_path' => $form_state
        ->getValue('url'),
    ]));
  }
  elseif ($extracted['route_name']) {

    // The user entered a Drupal path.
    $normal_path = $this->pathAliasManager
      ->getPathByAlias($extracted['path']);
    if ($extracted['path'] != $normal_path) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', [
        '%link_path' => $extracted['path'],
        '%normal_path' => $normal_path,
      ]));
    }
  }
}