From 0e9a422becd96c417d7f027ed7e75c159aa612c2 Mon Sep 17 00:00:00 2001 From: mdupont Date: Thu, 7 May 2020 05:03:21 +0000 Subject: [PATCH] Issue #3095714 by mdupont, Chi: Add file_uri filter --- src/TwigExtension.php | 76 +++++++++++++++++++ tests/src/Functional/TwigTweakTest.php | 18 ++++- .../templates/twig-tweak-test.html.twig | 7 ++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index c756402..2d78be7 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -336,6 +336,33 @@ class TwigExtension extends \Twig_Extension { // @endcode new \Twig_SimpleFilter('children', [$this, 'children']), + // - File URI - + // + // When field item list passed the URI will be extracted from the first + // item. In order to get URI of specific item specify its delta explicitly + // using array notation. + // @code + // {{ node.field_image|file_uri }} + // {{ node.field_image[0]|file_uri }} + // @endcode + // + // Media fields are fully supported including OEmbed resources, in which + // case it will return the URL to the resource, similar to the `file_url` + // filter. + // @code + // {{ node.field_media|file_uri }} + // @endcode + // + // Useful to apply the `image_style` filter to Media fields. + // Remember to check whether a URI is actually returned. + // @code + // {% set media_uri = node.field_media|file_uri %} + // {% if media_uri is not null %} + // {{ media_uri|image_style('thumbnail') }} + // {% endif %} + // @endcode + new \Twig_SimpleFilter('file_uri', [$this, 'fileUri']), + // - File URL - // // For string arguments it works similar to core file_url() Twig function. @@ -1158,6 +1185,55 @@ class TwigExtension extends \Twig_Extension { return array_intersect_key($build, array_flip($keys)); } + /** + * Returns a URI to the file. + * + * @param object $input + * An object that contains the URI. + * + * @return string|null + * A URI that may be used to access the file. + */ + public function fileUri($input) { + if ($input instanceof EntityReferenceFieldItemListInterface) { + $referenced_entities = $input->referencedEntities(); + if (isset($referenced_entities[0])) { + return self::getUriFromEntity($referenced_entities[0]); + } + } + elseif ($input instanceof EntityReferenceItem) { + return self::getUriFromEntity($input->entity); + } + elseif ($input instanceof EntityInterface) { + return self::getUriFromEntity($input); + } + } + + /** + * Extracts file URI from content entity. + * + * @param object $entity + * Entity object that contains information about the file. + * + * @return string|null + * A URI that may be used to access the file. + */ + private static function getUriFromEntity($entity) { + if ($entity instanceof MediaInterface) { + $source = $entity->getSource(); + $value = $source->getSourceFieldValue($entity); + if ($source instanceof OEmbedInterface) { + return $value; + } + elseif ($file = File::load($value)) { + return $file->getFileUri(); + } + } + elseif ($entity instanceof FileInterface) { + return $entity->getFileUri(); + } + } + /** * Returns a URL path to the file. * diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 0bedf48..952d838 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -349,12 +349,28 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-field-item-view" and text() = "Beta"]'; $this->assertByXpath($xpath); + // -- Test file URI from image field. + $this->drupalGet('/node/1'); + $xpath = '//div[@class = "tt-file-uri-from-image-field" and contains(text(), "public://image-test.png")]'; + $this->assertByXpath($xpath); + + // -- Test file URI from a specific image field item. + $xpath = '//div[@class = "tt-file-uri-from-image-field-delta" and contains(text(), "public://image-test.png")]'; + $this->assertByXpath($xpath); + + // -- Test file URI from media field. + $xpath = '//div[@class = "tt-file-uri-from-media-field" and contains(text(), "public://image-1.png")]'; + $this->assertByXpath($xpath); + + // -- Test 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->assertByXpath($xpath); + // -- Test file URL from URI. $xpath = '//div[@class = "tt-file-url-from-uri" and contains(text(), "/files/image-test.png")]'; $this->assertByXpath($xpath); // -- Test file URL from image field. - $this->drupalGet('/node/1'); $xpath = '//div[@class = "tt-file-url-from-image-field" and contains(text(), "/files/image-test.png")]'; $this->assertByXpath($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 847ced8..8a74c08 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -78,6 +78,13 @@
{{ node|view }}
{{ node.title|view }}
{{ node.title[0]|view }}
+
{{ node.field_image|file_uri }}
+
{{ node.field_image[0]|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 }}