Skip to content

Commit

Permalink
fixed conflicts, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Dubuisson committed Aug 14, 2017
2 parents 0463e16 + b7178e5 commit 941f74c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
.idea
9 changes: 9 additions & 0 deletions .idea/composerJson.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ 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

For a SS 3.x compatible-version, please see branch 1.0

## Installation

Expand Down
10 changes: 0 additions & 10 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
<?php

define('RESPONSIVE_IMAGES_DIR', basename(dirname(__FILE__)));

// Enable srcset attributes on image tags (the rest of the attributes are copied
// from framework/admin/_config.php)
$config = HtmlEditorConfig::get('cms');
$config->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*]'
);
2 changes: 1 addition & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Name: responsiveimages
---
Image:
SilverStripe\Assets\Image:
extensions:
- Heyday\ResponsiveImages\ResponsiveImageExtension
58 changes: 32 additions & 26 deletions code/ResponsiveImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -56,7 +58,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') ?: [];
}

/**
Expand Down Expand Up @@ -106,12 +108,12 @@ protected function createResponsiveSet($config, $defaultArgs, $set)
$methodName = Config::inst()->get(__CLASS__, 'default_method');
}

if (isset($config['css_classes'])) {
$cssClasses = $config['css_classes'];
} else {
$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,25 +124,29 @@ 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,
$sizes->push(ArrayData::create([
'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(
return $this->owner->customise([
'Sizes' => $sizes,
'DefaultImage' => $image,
'ExtraClasses' => $cssClasses
))->renderWith('ResponsiveImageSet');

'DefaultImage' => $this->getResampledImage($methodName, $defaultArgs)
])->renderWith('Includes/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, $methodName], $args);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"type": "silverstripe-module",
"license": "MIT",
"require": {
"composer/installers": "~1.0"
"silverstripe/framework": "^4.0"
}
}

0 comments on commit 941f74c

Please sign in to comment.