getUrlIfValid($element['#value'])) { if ($url->isExternal()) { $form_state->setError($element, t('You cannot use an external URL, please enter a relative path.')); return; } if ($element['#convert_path'] == self::CONVERT_NONE) { // Url is valid, no conversion required. return; } // We do the value conversion here whilst the Url object is in scope // after validation has occurred. if ($element['#convert_path'] == self::CONVERT_ROUTE) { $form_state->setValueForElement($element, [ 'route_name' => $url->getRouteName(), 'route_parameters' => $url->getRouteParameters(), ]); return; } elseif ($element['#convert_path'] == self::CONVERT_URL) { $form_state->setValueForElement($element, $url); return; } } $form_state->setError($element, t('This path does not exist or you do not have permission to link to %path.', [ '%path' => $element['#value'], ])); } } }