Skip to content

Commit

Permalink
Merge branch 'mimic-template-api' into 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stecman committed Apr 17, 2018
2 parents 0463e16 + b4afc6e commit 8ecd551
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions code/ResponsiveImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ protected function createResponsiveSet($config, $defaultArgs, $set)
$cssClasses = Config::inst()->get(__CLASS__, 'default_css_classes');
}

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) {
Expand All @@ -122,27 +127,31 @@ 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),
'ExtraClasses' => $cssClasses
))->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
Expand Down

0 comments on commit 8ecd551

Please sign in to comment.