- Issue created by @shiks
- 🇮🇳India nikhil_dawar
1. Usage of Preprocess Image Hook:
Implemented a preprocess image hook to address a specific issue.
2. Call to removeAllQueryParameter Function:
Within the preprocess image hook, called the removeAllQueryParameter function to remove all query parameters from the URL.3. Extension Check and Update:
Checked the file extension by extracting it from the modified URL.
4. Condition for SVG Files:Added a condition to check if the file extension is 'svg'.
5. Modification of srcset Attribute:
If the file is an SVG (Scalable Vector Graphics), updated the value of the srcset attribute with the src attribute.
In summary, the preprocess image hook is utilized to manipulate image URLs. The removeAllQueryParameter function is employed to eliminate all query parameters. Subsequently, the file extension is checked, and if it is an SVG file, the srcset attribute is adjusted to match the src attribute.
I have applied code in the .theme file
function removeAllQueryParameters($url) { // Parse the URL into its components. $urlParts = parse_url($url); // Check if there are query parameters. if (isset($urlParts['query'])) { // Remove all query parameters. $urlParts['query'] = ''; // Rebuild the full URL. $url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path']; if (!empty($urlParts['fragment'])) { $url .= '#' . $urlParts['fragment']; } } return $url; } function hook_preprocess_image(&$variables) { $modifiedUrl = removeAllQueryParameters($variables['uri']); $modified_file_extension = pathinfo($modifiedUrl, PATHINFO_EXTENSION); if ($modified_file_extension == 'svg') { $variables['attributes']['srcset'] = $variables['attributes']['src']; } }
- 🇩🇪Germany Anybody Porta Westfalica
Just ran into the same issue. I think the idea to add a specific setting to bypass certain mime types or formats (or displaying that they are skipped and adding this programmatically in ways of "Supported mime types" or something like that.
Maybe there are related issues for other cases or mime types / file formats already?