externalIsLocal example


  public function testExternalIsLocal($url$base_url$expected) {
    $this->assertSame($expected, UrlHelper::externalIsLocal($url$base_url));
  }

  /** * Provider for local external URL detection. * * @see \Drupal\Tests\Component\Utility\UrlHelperTest::testExternalIsLocal() */
  public function providerTestExternalIsLocal() {
    return [
      // Different mixes of trailing slash.       ['http://example.com', 'http://example.com', TRUE],
      [
// Check for HTTP and data URI-encoded URLs so that we don't have to       // implement getExternalUrl() for the HTTP and data schemes.       $options = UrlHelper::parse($uri);
      return Url::fromUri(urldecode($options['path'])$options);
    }
    elseif ($wrapper = $this->streamWrapperManager->getViaUri($uri)) {
      $external_url = $wrapper->getExternalUrl();
      $options = UrlHelper::parse($external_url);

      // @todo Switch to dependency injected request_context service after       // https://www.drupal.org/project/drupal/issues/3256884 is fixed.       if (UrlHelper::externalIsLocal($external_url, \Drupal::service('router.request_context')->getCompleteBaseUrl())) {
        // Attempt to return an external URL using the appropriate wrapper.         return Url::fromUri('base:' . $this->transformRelative(urldecode($options['path']), FALSE)$options);
      }
      else {
        return Url::fromUri(urldecode($options['path'])$options);
      }
    }
    throw new InvalidStreamWrapperException();
  }

  /** * {@inheritdoc} */
/** * Determines whether a path is local. * * @param string $url * The internal path or external URL being linked to, such as "node/34" or * "http://example.com/foo". * * @return bool * TRUE or FALSE, where TRUE indicates a local path. */
  protected function isLocal($url) {
    return !UrlHelper::isExternal($url) || UrlHelper::externalIsLocal($url$this->getRequestContext()->getCompleteBaseUrl());
  }

  /** * Returns the request context. * * @return \Drupal\Core\Routing\RequestContext * The request context. */
  protected function getRequestContext() {
    if (!isset($this->requestContext)) {
      $this->requestContext = \Drupal::service('router.request_context');
    }
Home | Imprint | This part of the site doesn't use cookies.