diff --git a/README.md b/README.md index f5635bb..b36d008 100644 --- a/README.md +++ b/README.md @@ -215,8 +215,17 @@ Twig filters. ### Image Style ```twig +{# Basic usage #} {{ 'public://images/ocean.jpg'|image_style('thumbnail') }} + +{# Make sure to check that the URI is valid #} +{% set image_uri = node.field_media_optional_image|file_uri %} +{% if image_uri is not null %} + {{ image_uri|image_style('thumbnail') }} +{% endif %} ``` +`image_style` will trigger an error on invalid or empty URIs, to avoid broken +images when used in an `` tag. ### Transliterate ```twig @@ -288,11 +297,6 @@ it will return the URL to the resource, similar to the `file_url` filter. {{ node.field_media|file_uri }} ``` -Useful to apply the `image_style` filter to Media fields. -```twig -{{ node.field_media|file_uri|image_style('thumbnail') }} -``` - ### File URL For string arguments it works similar to core `file_url()` Twig function. ```twig diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 05eb194..d317a8e 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -302,10 +302,6 @@ final class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-image-style" and contains(text(), "styles/thumbnail/public/images/ocean.jpg")]'; $this->assertXpath($xpath); - // -- Image style from File URI from media field. - $xpath = '//div[@class = "tt-image-style-from-file-uri-from-media-field" and contains(text(), "styles/thumbnail/public/image-1.png")]'; - $this->assertXpath($xpath); - // -- Transliterate. $xpath = '//div[@class = "tt-transliterate" and contains(text(), "Privet!")]'; $this->assertXpath($xpath); @@ -359,6 +355,10 @@ final class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-file-uri-from-media-field" and contains(text(), "public://image-1.png")]'; $this->assertXpath($xpath); + // -- Image style from File URI from media field. + $xpath = '//div[@class = "tt-image-style-from-file-uri-from-media-field" and contains(text(), "styles/thumbnail/public/image-1.png")]'; + $this->assertXpath($xpath); + // -- File URL from URI. $xpath = '//div[@class = "tt-file-url-from-uri" and contains(text(), "/files/image-test.png")]'; $this->assertXpath($xpath); diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index b3fe761..00ed338 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -62,7 +62,6 @@
{{ 'Site name: [site:name]'|token_replace }}
{{ 'FOO'|preg_replace('/(foo)/i', '$1-bar') }}
{{ 'public://images/ocean.jpg'|image_style('thumbnail') }}
-
{{ node.field_media|file_uri|image_style('thumbnail') }}
{{ 'Привет!'|transliterate('ru') }}
{{ 'bold strong'|check_markup('twig_tweak_test') }}
{{ 12345|format_size() }}
@@ -88,7 +87,11 @@
{{ node.title[0]|view }}
{{ node.field_image|file_uri }}
{{ node.field_image[0]|file_uri }}
-
{{ node.field_media|file_uri }}
+ {% set media_uri = node.field_media|file_uri %} +
{{ media_uri }}
+ {% if media_uri is not null %} +
{{ media_uri|image_style('thumbnail') }}
+ {% endif %}
{{ 'public://image-test.png'|file_url }}
{{ node.field_image|file_url }}
{{ node.field_image[0]|file_url }}