You are here

protected function InsertImageWidgetUtility::aggregateUrl in Insert 8

@inheritdoc This method is modeled after \Drupal\image\Entity\ImageStyle::buildUrl, but with the modification that it will consistently use absolute or relative URLs, depending on the Insert setting.

Overrides InsertFileWidgetUtility::aggregateUrl

1 call to InsertImageWidgetUtility::aggregateUrl()
InsertImageWidgetUtility::aggregateVariables in src/Utility/InsertImageWidgetUtility.php
@inheritdoc

File

src/Utility/InsertImageWidgetUtility.php, line 272

Class

InsertImageWidgetUtility

Namespace

Drupal\insert\Utility

Code

protected function aggregateUrl($uri, $absolute, $clean_urls = NULL) {
  if ($clean_urls === NULL) {

    // Assume clean URLs unless the request tells us otherwise.
    $clean_urls = TRUE;
    try {
      $request = \Drupal::request();
      $clean_urls = RequestHelper::isCleanUrl($request);
    } catch (ServiceNotFoundException $e) {
    }
  }

  // If not using clean URLs, the image derivative callback is only available
  // with the script path. If the file does not exist, use Url::fromUri() to
  // ensure that it is included. Once the file exists it's fine to fall back
  // to the actual file path, this avoids bootstrapping PHP once the files are
  // built. See \Drupal\image\Entity\ImageStyle::buildUrl.
  if ($clean_urls === FALSE && \Drupal::service('file_system')
    ->uriScheme($uri) == 'public' && !file_exists($uri)) {
    $directory_path = \Drupal::service('stream_wrapper_manager')
      ->getViaUri($uri)
      ->getDirectoryPath();
    $url = Url::fromUri('base:' . $directory_path . '/' . StreamWrapperManager::getTarget($uri), [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  else {
    $url = file_create_url($uri);
  }
  if (!$absolute && strpos($url, $GLOBALS['base_url']) === 0) {
    $url = base_path() . ltrim(str_replace($GLOBALS['base_url'], '', $url), '/');
  }
  return $url;
}