Skip to content

Commit

Permalink
bug #3790 Allow to apply Twig filters conditionally (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Allow to apply Twig filters conditionally

Fixes #3762

Commits
-------

3e473bf Allow to apply Twig filters conditionally
  • Loading branch information
javiereguiluz committed Sep 22, 2020
2 parents 48673ee + 3e473bf commit 881d2f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Resources/views/crud/form_theme.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
{% if download_uri|default('') is empty %}
<div class="ea-lightbox-thumbnail">
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
<img style="cursor: initial" src="{{ asset(image_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
<img style="cursor: initial" src="{{ asset(image_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
{% else %}
<img style="cursor: initial" src="{{ asset(image_uri) }}">
{% endif %}
Expand All @@ -277,15 +277,15 @@

<a href="#" class="ea-lightbox-thumbnail" data-featherlight="#{{ _lightbox_id }}" data-featherlight-close-on-click="anywhere">
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
<img src="{{ asset(download_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
<img src="{{ asset(download_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
{% else %}
<img src="{{ asset(download_uri) }}">
{% endif %}
</a>

<div id="{{ _lightbox_id }}" class="ea-lightbox">
{% if formTypeOptions.imagine_pattern is defined and formTypeOptions.imagine_pattern is not empty %}
<img src="{{ asset(download_uri)|imagine_filter(formTypeOptions.imagine_pattern) }}">
<img src="{{ asset(download_uri)|ea_apply_filter_if_exists('imagine_filter', formTypeOptions.imagine_pattern) }}">
{% else %}
<img src="{{ asset(download_uri) }}">
{% endif %}
Expand Down
12 changes: 12 additions & 0 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Exception\MissingResourceException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand Down Expand Up @@ -47,6 +48,7 @@ public function getFilters()
$filters = [
new TwigFilter('ea_flatten_array', [$this, 'flattenArray']),
new TwigFilter('ea_filesize', [$this, 'fileSize']),
new TwigFilter('ea_apply_filter_if_exists', [$this, 'applyFilterIfExists'], ['needs_environment' => true]),
];

if (Kernel::VERSION_ID >= 40200) {
Expand Down Expand Up @@ -85,6 +87,16 @@ public function fileSize(int $bytes): string
return (int) ($bytes / (1024 ** $factor)).@$size[$factor];
}

// Code adapted from https://stackoverflow.com/a/48606773/2804294 (License: CC BY-SA 3.0)
public function applyFilterIfExists(Environment $environment, $value, string $filterName, ...$filterArguments)
{
if (false === $filter = $environment->getFilter($filterName)) {
return $value;
}

return $filter->getCallable()($value, ...$filterArguments);
}

public function getCrudUrlBuilder(array $queryParameters = []): CrudUrlBuilder
{
return $this->crudUrlGenerator->build($queryParameters);
Expand Down

0 comments on commit 881d2f0

Please sign in to comment.