diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index 418f386..fa6eb4c 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -92,25 +92,41 @@ public function form(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function validate(array $form, FormStateInterface $form_state) { - parent::validate($form, $form_state); + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); - $link_uri = $form_state->getValue('link')[0]['uri']; // If the URL is internal, we want to check that the link will resolve in a // way the user expects. We only check that if the form does not have any // other errors, because to try to construct a URL from invalid input throws // an exception, and the user will already be notified from the parent // validation. - if (!$form_state->hasAnyErrors() && !Url::fromUri($link_uri)->isExternal()) { - // Find the user input and validate that no URL negotiator will end up - // changing this URL to something else. For example if a user is entering - // a URL with a language prefix. - $user_link = $form_state->getUserInput(); - // Create a URL object from that. - $user_url = Url::fromUserInput($user_link['link'][0]['uri']); - // Compare the generated link from what Drupal thinks this is. - if ($user_link['link'][0]['uri'] != $user_url->toString()) { - $form_state->setErrorByName('link', $this->t('The link you entered is not valid')); + if (!$form_state->hasAnyErrors()) { + $link_uri = $form_state->getValue('link')[0]['uri']; + $language = $this->language_manager->getLanguage($form_state->getValue('langcode')[0]['value']); + $link_url = Url::fromUri($link_uri, [ + 'language' => $language, + ]); + if (!$link_url->isExternal()) { + // Find the user input and validate that no URL negotiator will end up + // changing this URL to something else. For example if a user is entering + // a URL with a language prefix. + $user_input = $form_state->getUserInput(); + $user_link = $user_input['link'][0]['uri']; + // Create a URL object from that. + $user_url = $this->pathValidator + ->getUrlIfValidWithoutAccessCheck($user_link); + if ($user_url) { + // If the user entered the special case "" we must allow this to + // not correspond to what Drupal thinks the URL is. + if ($user_link == '') { + $user_link = '/'; + } + // Compare the generated link from what Drupal thinks this is. + $user_url->setOption('language', $language); + if ($user_link != $user_url->toString()) { + $form_state->setErrorByName('link', $this->t('The link you entered is not valid')); + } + } } } }