Browse Source

Issue #3095714 by mdupont, Chi: Add file_uri filter

merge-requests/4/head
mdupont 4 years ago committed by Chi
parent
commit
0e9a422bec
  1. 76
      src/TwigExtension.php
  2. 18
      tests/src/Functional/TwigTweakTest.php
  3. 7
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

76
src/TwigExtension.php

@ -336,6 +336,33 @@ class TwigExtension extends \Twig_Extension {
// @endcode // @endcode
new \Twig_SimpleFilter('children', [$this, 'children']), 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 - // - File URL -
// //
// For string arguments it works similar to core file_url() Twig function. // 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)); 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. * Returns a URL path to the file.
* *

18
tests/src/Functional/TwigTweakTest.php

@ -349,12 +349,28 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-field-item-view" and text() = "Beta"]'; $xpath = '//div[@class = "tt-field-item-view" and text() = "Beta"]';
$this->assertByXpath($xpath); $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. // -- Test file URL from URI.
$xpath = '//div[@class = "tt-file-url-from-uri" and contains(text(), "/files/image-test.png")]'; $xpath = '//div[@class = "tt-file-url-from-uri" and contains(text(), "/files/image-test.png")]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// -- Test file URL from image field. // -- 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")]'; $xpath = '//div[@class = "tt-file-url-from-image-field" and contains(text(), "/files/image-test.png")]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);

7
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -78,6 +78,13 @@
<div class="tt-node-view">{{ node|view }}</div> <div class="tt-node-view">{{ node|view }}</div>
<div class="tt-field-list-view">{{ node.title|view }}</div> <div class="tt-field-list-view">{{ node.title|view }}</div>
<div class="tt-field-item-view">{{ node.title[0]|view }}</div> <div class="tt-field-item-view">{{ node.title[0]|view }}</div>
<div class="tt-file-uri-from-image-field">{{ node.field_image|file_uri }}</div>
<div class="tt-file-uri-from-image-field-delta">{{ node.field_image[0]|file_uri }}</div>
{% set media_uri = node.field_media|file_uri %}
<div class="tt-file-uri-from-media-field">{{ media_uri }}</div>
{% if media_uri is not null %}
<div class="tt-image-style-from-file-uri-from-media-field">{{ media_uri|image_style('thumbnail') }}</div>
{% endif %}
<div class="tt-file-url-from-uri">{{ 'public://image-test.png'|file_url }}</div> <div class="tt-file-url-from-uri">{{ 'public://image-test.png'|file_url }}</div>
<div class="tt-file-url-from-image-field">{{ node.field_image|file_url }}</div> <div class="tt-file-url-from-image-field">{{ node.field_image|file_url }}</div>
<div class="tt-file-url-from-image-field-delta">{{ node.field_image[0]|file_url }}</div> <div class="tt-file-url-from-image-field-delta">{{ node.field_image[0]|file_url }}</div>

Loading…
Cancel
Save