Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix local asset twig function and allow url #11

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ http://wkhtmltopdf.org/downloads.html
See [KnpSnappyBundle](https://github.com/KnpLabs/KnpSnappyBundle#configuration) for configuration.

## Usage
========

**Controller Trait**

Expand Down Expand Up @@ -100,4 +99,10 @@ The `local_asset` avoids doing a http request by using `file://` instead of `htt
<img src="{{ local_asset('/images/image.jpg') }}" alt="Local Asset">
```

This will only work when `$request->getRequestFormat()` will return `pdf`.
This will only work when `$request->getRequestFormat()` will return `pdf` and not `html`.

If you want to force using `file://` set the second parameter to true:

```twig
<img src="{{ local_asset('/images/image.jpg', true) }}" alt="Local Asset">
```
6 changes: 5 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
<service id="massive_pdf.pdf_manager" class="%massive_pdf.pdf_manager.class%">
<argument type="service" id="templating" />
<argument type="service" id="knp_snappy.pdf" />

<deprecated>The "%service_id%" service is deprecated use massive_pdf.pdf_factory instead.</deprecated>
alexander-schranz marked this conversation as resolved.
Show resolved Hide resolved
</service>

<service id="massive_pdf.pdf_factory" class="Massive\Bundle\PdfBundle\Pdf\PdfFactory">
<service id="massive_pdf.pdf_factory" class="Massive\Bundle\PdfBundle\Pdf\PdfFactory" public="true">
<argument type="service" id="templating"/>
<argument type="service" id="knp_snappy.pdf"/>
<argument type="service" id="request_stack"/>
</service>

<service id="Massive\Bundle\PdfBundle\Pdf\PdfFactory" alias="massive_pdf.pdf_factory"/>

<service id="massive_pdf.twig.local_asset" class="Massive\Bundle\PdfBundle\Twig\LocalAssetTwigExtension">
<argument type="service" id="request_stack"/>
<argument>%massive_pdf.public_dir%</argument>
Expand Down
19 changes: 13 additions & 6 deletions Twig/LocalAssetTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,33 @@ public function getFunctions()
public function getFilters()
{
return [
new \Twig_SimpleFilter('abbr_class', [$this, 'getLocalAsset']),
alexander-schranz marked this conversation as resolved.
Show resolved Hide resolved
new \Twig_SimpleFilter('local_asset', [$this, 'getLocalAsset']),
];
}

/**
* Get local asset path.
*
* @param string $assetUrl
* @param bool $force
*
* @return string
*/
public function getLocalAsset($assetUrl)
public function getLocalAsset($assetUrl, $force = false)
{
$request = $this->requestStack->getCurrentRequest();
$prefix = 'file://' . $this->publicDirectory;
$assetUrl = '/' . ltrim($assetUrl, '/');

if ('html' === $request->getRequestFormat()) {
return $assetUrl;
if ($force) {
return $prefix . $assetUrl;
}

$request = $this->requestStack->getCurrentRequest();

if ($request && 'html' === $request->getRequestFormat()) {
$prefix = $request->getSchemeAndHttpHost();
}

return 'file://' . $this->publicDirectory . $assetUrl;
return $prefix . $assetUrl;
}
}