From 95835886b1297d3c44eb465d61380e99dc23899d Mon Sep 17 00:00:00 2001 From: Stephen Holdaway Date: Thu, 18 Aug 2016 09:50:31 +1200 Subject: [PATCH 1/2] Work with higher level resize methods instead of getFormattedImage #17 raises an issue where a generate*Image method in a module has arguments that differ to the template-facing API, causing incorrect behaviour when used in a responsive image set. The arguments for the image resize methods exposed in templates ($Image.CroppedImage, $Image.PaddedImage, etc) are more appropriate for configuration via YAML as their parameters must be scalar to be written in a template. This change has the bonus of keeping the configuration API for this module consistent with use of image resize methods in templates, rather than the potentially different internal API of an image resize method. --- code/ResponsiveImageExtension.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/code/ResponsiveImageExtension.php b/code/ResponsiveImageExtension.php index 55a8d08..619c455 100644 --- a/code/ResponsiveImageExtension.php +++ b/code/ResponsiveImageExtension.php @@ -100,6 +100,11 @@ protected function createResponsiveSet($config, $defaultArgs, $set) $methodName = Config::inst()->get(__CLASS__, 'default_method'); } + if (!$this->owner->hasMethod($methodName)) { + throw new \RuntimeException(get_class($this->owner) . ' has no method ' . $methodName); + } + + // Create the resampled images for each query in the set $sizes = ArrayList::create(); foreach ($config['arguments'] as $query => $args) { if (is_numeric($query) || !$query) { @@ -110,26 +115,30 @@ protected function createResponsiveSet($config, $defaultArgs, $set) throw new Exception("Responsive set $set doesn't have any arguments provided for the query: $query"); } - array_unshift($args, $methodName); - $image = call_user_func_array(array($this->owner, 'getFormattedImage'), $args); $sizes->push(ArrayData::create(array( - 'Image' => $image, + 'Image' => $this->getResampledImage($methodName, $args), 'Query' => $query ))); } - // The first argument may be an image method such as 'CroppedImage' - if (!isset($defaultArgs[0]) || !$this->owner->hasMethod($defaultArgs[0])) { - array_unshift($defaultArgs, $methodName); - } - - $image = call_user_func_array(array($this->owner, 'getFormattedImage'), $defaultArgs); return $this->owner->customise(array( 'Sizes' => $sizes, - 'DefaultImage' => $image + 'DefaultImage' => $this->getResampledImage($methodName, $defaultArgs) ))->renderWith('ResponsiveImageSet'); } + /** + * Return a resampled image equivalent to $Image.MethodName(...$args) in a template + * + * @param string $methodName + * @param array $args + * @return Image + */ + protected function getResampledImage($methodName, $args) + { + return call_user_func(array($this->owner, '__call'), $methodName, $args); + } + /** * Due to {@link Object::allMethodNames()} requiring methods to be expressed * in all lowercase, getting the config for a given method requires a From b7178e5bd857fc5ad45a4bc9cc5065e26f4926c5 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Wed, 2 Nov 2016 12:24:22 +0000 Subject: [PATCH 2/2] SilverStripe 4 compability --- README.md | 2 +- _config.php | 10 ---------- _config/config.yml | 2 +- code/ResponsiveImageExtension.php | 28 +++++++++++++++------------- composer.json | 2 +- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d6e73b5..5edef9d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This is particularly useful for sites that use responsive design, because it mea This module is highly configurable and relies on [picturefill.js](https://github.com/scottjehl/picturefill) for the client-side magic. ## Requirements -SilverStripe 3.0 or higher +SilverStripe 4.0 or higher ## Installation diff --git a/_config.php b/_config.php index 4afafcc..17cc223 100644 --- a/_config.php +++ b/_config.php @@ -1,13 +1,3 @@ setOption( - 'extended_valid_elements', - $config->getOption('extended_valid_elements') . ',img[class|src|srcset|alt|' - . 'title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|' - . 'usemap|data*]' -); diff --git a/_config/config.yml b/_config/config.yml index 42cc073..74ee9f0 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -1,6 +1,6 @@ --- Name: responsiveimages --- -Image: +SilverStripe\Assets\Image: extensions: - Heyday\ResponsiveImages\ResponsiveImageExtension diff --git a/code/ResponsiveImageExtension.php b/code/ResponsiveImageExtension.php index 619c455..4dd4352 100644 --- a/code/ResponsiveImageExtension.php +++ b/code/ResponsiveImageExtension.php @@ -2,11 +2,13 @@ namespace Heyday\ResponsiveImages; -use ArrayData; -use ArrayList; -use Config; +use SilverStripe\Core\Config\Config; +use SilverStripe\Core\Extension; +use SilverStripe\ORM\ArrayList; +use SilverStripe\View\ArrayData; +use SilverStripe\View\Requirements; use Exception; -use Requirements; +use RuntimeException; /** * An extension to the Image class to inject methods for responsive image sets. @@ -25,13 +27,13 @@ * This provides $MyImage.MyResponsiveImageSet to the template. For more * documentation on implementation, see the README file. */ -class ResponsiveImageExtension extends \Extension +class ResponsiveImageExtension extends Extension { /** * @var array * @config */ - private static $default_arguments = array(800, 600); + private static $default_arguments = [800, 600]; /** * @var string @@ -50,7 +52,7 @@ class ResponsiveImageExtension extends \Extension public function __construct() { parent::__construct(); - $this->configSets = Config::inst()->get(__CLASS__, 'sets') ?: array(); + $this->configSets = Config::inst()->get(__CLASS__, 'sets') ?: []; } /** @@ -101,7 +103,7 @@ protected function createResponsiveSet($config, $defaultArgs, $set) } if (!$this->owner->hasMethod($methodName)) { - throw new \RuntimeException(get_class($this->owner) . ' has no method ' . $methodName); + throw new RuntimeException(get_class($this->owner) . ' has no method ' . $methodName); } // Create the resampled images for each query in the set @@ -115,16 +117,16 @@ protected function createResponsiveSet($config, $defaultArgs, $set) throw new Exception("Responsive set $set doesn't have any arguments provided for the query: $query"); } - $sizes->push(ArrayData::create(array( + $sizes->push(ArrayData::create([ 'Image' => $this->getResampledImage($methodName, $args), 'Query' => $query - ))); + ])); } - return $this->owner->customise(array( + return $this->owner->customise([ 'Sizes' => $sizes, 'DefaultImage' => $this->getResampledImage($methodName, $defaultArgs) - ))->renderWith('ResponsiveImageSet'); + ])->renderWith('Includes/ResponsiveImageSet'); } /** @@ -136,7 +138,7 @@ protected function createResponsiveSet($config, $defaultArgs, $set) */ protected function getResampledImage($methodName, $args) { - return call_user_func(array($this->owner, '__call'), $methodName, $args); + return call_user_func_array([$this->owner, $methodName], $args); } /** diff --git a/composer.json b/composer.json index cef39ae..ce1d762 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,6 @@ "type": "silverstripe-module", "license": "MIT", "require": { - "composer/installers": "~1.0" + "silverstripe/framework": "^4.0" } }