+
+ // If the URI has a schema and that is not http, https or data, convert
+ // the URI to the external URL. Otherwise the appended query argument
+ // will be encoded.
+ // @see file_create_url()
+ $scheme = \Drupal::service('file_system')->uriScheme($uri);
+ if ($scheme && !in_array($scheme, ['http', 'https', 'data'])) {
+ if ($wrapper = \Drupal::service('stream_wrapper_manager')->getViaUri($uri)) {
+ $uri = $wrapper->getExternalUrl();
+ }
+ }
+
+ // Append either with a ? or a & if there are existing query arguments.
+ if (strpos($uri, '?') === FALSE) {
+ $uri .= '?h=' . $shortened_hash;
+ }
+ else {
+ $uri .= '&h=' . $shortened_hash;
+ }