diff --git a/core/modules/image/image.api.php b/core/modules/image/image.api.php index 258945b..cd0e4ef 100644 --- a/core/modules/image/image.api.php +++ b/core/modules/image/image.api.php @@ -56,10 +56,11 @@ function hook_image_style_flush($style) { * - image_style: The \Drupal\image\ImageStyleInterface image style object. */ function hook_image_style_uri_alter(&$uri, array $context) { - if (($name = $context['image_style']->id()) == 'high_resolution') { + if (($name = $context['image_style']->id()) == 'watermarked_low_resolution') { $path = $context['path']; - // Show this image derivative in private:// stream wrapper. - $uri = "private://styles/$name/private/$path"; + // Low resolution, watermarked images are available publicly, in the + // public:// stream wrapper. + $uri = "public://styles/$name/public/$path"; } } diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 34556e8..9b65db5 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -11,6 +11,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityWithPluginCollectionInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Routing\RequestHelper; use Drupal\Core\Site\Settings; use Drupal\Core\Url; @@ -94,6 +95,28 @@ class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, Entity protected $effectsCollection; /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * Constructs an image style object. + * + * @param array $values + * An array of values to set, keyed by property name. + * @param string $entity_type + * The type of the entity to create. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + */ + public function __construct(array $values, $entity_type, ModuleHandlerInterface $module_handler = NULL) { + parent::__construct($values, $entity_type); + $this->moduleHandler = $module_handler ?: \Drupal::moduleHandler(); + } + + /** * Overrides Drupal\Core\Entity\Entity::id(). */ public function id() { @@ -192,7 +215,7 @@ public function buildUri($uri) { // Allow modules to alter the uri before is returned. $context = ['scheme' => $scheme, 'path' => $path, 'image_style' => $this]; - \Drupal::moduleHandler()->alter('image_style_uri', $uri, $context); + $this->moduleHandler->alter('image_style_uri', $uri, $context); return $uri; } diff --git a/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php index 293245c..2f8ae53 100644 --- a/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php +++ b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php @@ -231,15 +231,15 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = */ public function testImageStyleUriAlter() { $style = ImageStyle::create([ - 'name' => 'high_resolution', + 'name' => 'watermarked_low_resolution', 'label' => $this->randomString(), ]); $style->save(); - $original = 'public://bar/baz.png'; - // The 'high_resolution' derivatives are available in private:// stream - // wrapper. - $this->assertIdentical($style->buildUri($original), 'private://styles/high_resolution/private/bar/baz.png'); + $original = 'private://bar/baz.png'; + // The 'watermarked_low_resolution' derivatives are available publicly, in + // the public:// stream wrapper. + $this->assertIdentical($style->buildUri($original), 'public://styles/watermarked_low_resolution/public/bar/baz.png'); } } diff --git a/core/modules/image/tests/modules/image_module_test/image_module_test.module b/core/modules/image/tests/modules/image_module_test/image_module_test.module index 1e89de9..5cc1460 100644 --- a/core/modules/image/tests/modules/image_module_test/image_module_test.module +++ b/core/modules/image/tests/modules/image_module_test/image_module_test.module @@ -37,9 +37,10 @@ function image_module_test_image_style_presave(ImageStyleInterface $style) { * Implements hook_image_style_uri_alter(). */ function image_module_test_image_style_uri_alter(&$uri, array $context) { - if (($name = $context['image_style']->id()) == 'high_resolution') { + if (($name = $context['image_style']->id()) == 'watermarked_low_resolution') { $path = $context['path']; - // Show this image derivative in private:// stream wrapper. - $uri = "private://styles/$name/private/$path"; + // Low resolution, watermarked images are available publicly, in the + // public:// stream wrapper. + $uri = "public://styles/$name/public/$path"; } } diff --git a/core/modules/image/tests/src/Unit/ImageStyleTest.php b/core/modules/image/tests/src/Unit/ImageStyleTest.php index 80ce713..c67d77a 100644 --- a/core/modules/image/tests/src/Unit/ImageStyleTest.php +++ b/core/modules/image/tests/src/Unit/ImageStyleTest.php @@ -63,10 +63,13 @@ protected function getImageStyleMock($image_effect_id, $image_effect, $stubs = a 'fileUriTarget', 'fileDefaultScheme', ); + $module_handler = $this->getMockBuilder('\Drupal\Core\Extension\ModuleHandlerInterface') + ->getMock(); $image_style = $this->getMockBuilder('\Drupal\image\Entity\ImageStyle') ->setConstructorArgs(array( array('effects' => array($image_effect_id => array('id' => $image_effect_id))), $this->entityTypeId, + $module_handler, )) ->setMethods(array_merge($default_stubs, $stubs)) ->getMock();