diff --git a/crop.module b/crop.module index 1dbcbeb..14ae562 100644 --- a/crop.module +++ b/crop.module @@ -116,22 +116,21 @@ function crop_file_delete(FileInterface $file) { * Implements hook_file_url_alter(). */ function crop_file_url_alter(&$uri) { - // Parse given URI. - $uri_info = parse_url($uri); - // Use schema information to skip relative URIs. - $protocol = isset($uri_info['scheme']) ? in_array($uri_info['scheme'], ['http', 'https']) : FALSE; - // Process only files that are stored in "styles" directory. - if ($protocol && strpos($uri, PublicStream::basePath() . '/styles/') !== FALSE) { + if (strpos($uri, PublicStream::basePath() . '/styles/') !== FALSE) { // Match image style, schema, file subdirectory and file name. preg_match("/styles\/(.*?)\/(.*?)\/(.*+)/", $uri, $match); // Get the image style ID. $image_style = $match[1]; + // Get the file path without query parameter. + $parsed_uri = parse_url($match[3]); // Get the file URI using parsed schema and file path. - $file_uri = $match[2] . '://' . $match[3]; + $file_uri = $match[2] . '://' . $parsed_uri['path']; /** @var \Drupal\image\Entity\ImageStyle $image_style */ - $image_style = ImageStyle::load($image_style); + if (!$image_style = ImageStyle::load($image_style)) { + return; + } $crop_type = NULL; // Find whether matched image style uses "crop_type" effect. diff --git a/tests/src/Kernel/CropCRUDTest.php b/tests/src/Kernel/CropCRUDTest.php index 652b7d6..8a2a5ff 100644 --- a/tests/src/Kernel/CropCRUDTest.php +++ b/tests/src/Kernel/CropCRUDTest.php @@ -117,8 +117,6 @@ class CropCRUDTest extends CropUnitTestBase { $image_style_uri = $this->testStyle->buildUri($file->getFileUri()); // Creating the image style URL. $image_style_url = file_create_url($image_style_uri); - // Trigger crop_file_url_alter() function. - \Drupal::moduleHandler()->alter('file_url', $image_style_url); $shortened_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8); $this->assertContains('&h=' . $shortened_hash, $image_style_url); }