Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit f2ddea4

Browse files
committed
fixes #50 uses bolt's register assets method for picturefill include
1 parent 0f03b61 commit f2ddea4

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

src/BetterThumbsExtension.php

+32-39
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Bolt\Extension\cdowdy\betterthumbs;
44

5-
use Bolt\Asset\Snippet\Snippet;
6-
use Bolt\Asset\Target;
5+
use Bolt\Asset\File\JavaScript;
76
use Bolt\Controller\Zone;
87
use Bolt\Extension\cdowdy\betterthumbs\Controller\BetterThumbsBackendController;
98
use Bolt\Extension\cdowdy\betterthumbs\Controller\BetterThumbsController;
@@ -16,7 +15,6 @@
1615
use Bolt\Version as Version;
1716
use League\Flysystem\Adapter\Local;
1817
use League\Flysystem\Filesystem;
19-
2018
use Pimple as Container;
2119

2220

@@ -60,7 +58,7 @@ public function image( $sourceImage, $name = 'betterthumbs', array $options = []
6058
$app = $this->getContainer();
6159
$config = $this->getConfig();
6260

63-
$this->addAssets();
61+
// $this->addAssets();
6462

6563
$file = $this->filterEmptyImageArray( $sourceImage );
6664

@@ -147,49 +145,44 @@ public function image( $sourceImage, $name = 'betterthumbs', array $options = []
147145
return new \Twig_Markup( $renderTemplate, 'UTF-8' );
148146
}
149147

150-
/**
151-
* You can't rely on bolts methods to insert javascript/css in the location you want.
152-
* So we have to hack around it. Use the Snippet Class with their location methods and insert
153-
* Picturefill into the head. Add a check to make sure the script isn't loaded more than once ($_scriptAdded)
154-
* and stop the insertion of the files multiple times because bolt's registerAssets method will blindly insert
155-
* the files on every page
156-
*
157-
*/
158148

159-
protected function addAssets()
160-
{
161-
$app = $this->getContainer();
162149

163-
$config = $this->getConfig();
150+
/**
151+
* @return array
152+
*/
153+
protected function registerAssets()
154+
{
164155

165-
$pfill = $config['picturefill'];
156+
$config = $this->getConfig();
166157

167-
$extPath = $app['resources']->getUrl( 'extensions' );
158+
$pfill = $config['picturefill'];
168159

169-
$vendor = 'vendor/cdowdy/';
170-
$extName = 'betterthumbs/';
171160

172-
$pictureFillJS = $extPath . $vendor . $extName . 'picturefill/' . $this->_currentPictureFill . '/picturefill.min.js';
173-
$pictureFill = <<<PFILL
174-
<script src="{$pictureFillJS}" async defer></script>
175-
PFILL;
176-
$asset = new Snippet();
177-
$asset->setCallback( $pictureFill )
178-
->setZone( ZONE::FRONTEND )
179-
->setLocation( Target::AFTER_HEAD_CSS );
161+
$vendor = 'vendor/cdowdy/';
162+
$extName = 'betterthumbs/';
180163

164+
$pictureFillJS = '/extensions/' . $vendor . $extName . 'picturefill/' . $this->_currentPictureFill . '/picturefill.min.js';
181165

182-
// add picturefill script only once for each time the extension is used
183-
if ( $pfill ) {
184-
if ( $this->_scriptAdded == false ) {
185-
$app['asset.queue.snippet']->add( $asset );
186-
$this->_scriptAdded = true;
187-
} else {
166+
$asset = JavaScript::create()
167+
->setFileName($pictureFillJS)
168+
->setPriority(5)
169+
->setAttributes(['defer', 'async'])
170+
->setZone(Zone::FRONTEND);
188171

189-
$this->_scriptAdded = true;
190-
}
191-
}
192-
}
172+
if ($pfill && $this->_scriptAdded == false) {
173+
174+
$this->_scriptAdded = true;
175+
return [
176+
$asset,
177+
];
178+
} else {
179+
180+
$this->_scriptAdded = true;
181+
182+
return null;
183+
}
184+
185+
}
193186

194187
/**
195188
* @param $name
@@ -800,7 +793,7 @@ public function btSrc( $sourceImage, $name = 'betterthumbs', array $options = []
800793
$app = $this->getContainer();
801794
$config = $this->getConfig();
802795

803-
$this->addAssets();
796+
// $this->addAssets();
804797

805798

806799
$configName = $this->getNamedConfig( $name );

0 commit comments

Comments
 (0)