getUrlIfValid example

->with('link to any page')
      ->willReturn(FALSE);

    $this->accessAwareRouter->expects($this->exactly(2))
      ->method('match')
      ->with('/test-path')
      ->willReturn([RouteObjectInterface::ROUTE_NAME => 'test_route', '_raw_variables' => new InputBag(['key' => 'value'])]);
    $this->pathProcessor->expects($this->exactly(2))
      ->method('processInbound')
      ->willReturnArgument(0);

    $url = $this->pathValidator->getUrlIfValid('test-path');
    $this->assertInstanceOf('Drupal\Core\Url', $url);

    $this->assertEquals('test_route', $url->getRouteName());
    $this->assertEquals(['key' => 'value']$url->getRouteParameters());

    // Test with leading /.     $url = $this->pathValidator->getUrlIfValid('/test-path');
    $this->assertInstanceOf('Drupal\Core\Url', $url);

    $this->assertEquals('test_route', $url->getRouteName());
    $this->assertEquals(['key' => 'value']$url->getRouteParameters());
  }
public function __construct(AccessAwareRouterInterface $access_aware_router, UrlMatcherInterface $access_unaware_router, AccountInterface $account, InboundPathProcessorInterface $path_processor) {
    $this->accessAwareRouter = $access_aware_router;
    $this->accessUnawareRouter = $access_unaware_router;
    $this->account = $account;
    $this->pathProcessor = $path_processor;
  }

  /** * {@inheritdoc} */
  public function isValid($path) {
    return (bool) $this->getUrlIfValid($path);
  }

  /** * {@inheritdoc} */
  public function getUrlIfValid($path) {
    return $this->getUrl($path, TRUE);
  }

  /** * {@inheritdoc} */
/** * Form element validation handler for matched_path elements. * * Note that #maxlength is validated by _form_validate() already. * * This checks that the submitted value matches an active route. */
  public static function validateMatchedPath(&$element, FormStateInterface $form_state, &$complete_form) {
    if (!empty($element['#value']) && ($element['#validate_path'] || $element['#convert_path'] != self::CONVERT_NONE)) {
      /** @var \Drupal\Core\Url $url */
      if ($url = \Drupal::service('path.validator')->getUrlIfValid($element['#value'])) {
        if ($url->isExternal()) {
          $form_state->setError($elementt('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) {
          
Home | Imprint | This part of the site doesn't use cookies.