Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nelson-ph/27128b4f034dd2b2f60f7b34d669fb22 to your computer and use it in GitHub Desktop.

Select an option

Save nelson-ph/27128b4f034dd2b2f60f7b34d669fb22 to your computer and use it in GitHub Desktop.
Drupal 8 - Token image style url - support relative case
Index: token.tokens.inc
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/token.tokens.inc b/token.tokens.inc
--- a/token.tokens.inc (date 1622738647667)
+++ b/token.tokens.inc (date 1622738647667)
@@ -1767,7 +1767,15 @@
$replacements[$original] = $style->buildUri($original_uri);
}
elseif ($property_name === 'url') {
- $replacements[$original] = $style->buildUrl($original_uri);
+ if (count($parts) > 1) {
+ $filtered_tokens = [implode(':', array_slice($parts, 1)) => $filtered_tokens[implode(':', $parts)]];
+ $url = Url::fromUri('base:'. file_url_transform_relative($style->buildUrl($original_uri)));
+ $field_tokens = \Drupal::token()->findWithPrefix($filtered_tokens, $property_name);
+ $replacements += \Drupal::token()->generate('url', $field_tokens, ['url' => $url], $options, $bubbleable_metadata);
+ }
+ else {
+ $replacements[$original] = $style->buildUrl($original_uri);
+ }
}
else {
// Generate the image derivative, if it doesn't already exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment