Browse Source

Issue #3095714 by mdupont: Make image_style more powerful

merge-requests/2/head
mdupont 4 years ago committed by Chi
parent
commit
54aed14429
  1. 20
      README.md
  2. 50
      src/TwigTweakExtension.php
  3. 16
      tests/src/Functional/TwigTweakTest.php
  4. 4
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

20
README.md

@ -273,6 +273,26 @@ This is an opposite of core `without` filter.
</ul>
```
### 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.
```twig
{{ node.field_image|file_uri }}
{{ node.field_image[0]|file_uri }}
```
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.
```twig
{{ 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

50
src/TwigTweakExtension.php

@ -114,6 +114,7 @@ class TwigTweakExtension extends AbstractExtension {
new TwigFilter('view', [self::class, 'viewFilter']),
new TwigFilter('with', [self::class, 'withFilter']),
new TwigFilter('children', [self::class, 'childrenFilter']),
new TwigFilter('file_uri', [self::class, 'fileUriFilter']),
new TwigFilter('file_url', [self::class, 'fileUrlFilter']),
];
@ -570,6 +571,30 @@ class TwigTweakExtension extends AbstractExtension {
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 static function fileUriFilter($input): ?string {
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);
}
}
/**
* Returns a URL path to the file.
*
@ -613,6 +638,31 @@ class TwigTweakExtension extends AbstractExtension {
return $output;
}
/**
* Extracts file URI from content entity.
*
* @param \Drupal\Core\Entity\EntityInterface $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(EntityInterface $entity): ?string {
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();
}
}
/**
* Extracts file URL from content entity.
*

16
tests/src/Functional/TwigTweakTest.php

@ -302,6 +302,10 @@ 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);
@ -343,6 +347,18 @@ final class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-field-item-view" and text() = "Alpha"]';
$this->assertXpath($xpath);
// -- File URI from image field.
$xpath = '//div[@class = "tt-file-uri-from-image-field" and contains(text(), "public://image-test.png")]';
$this->assertXpath($xpath);
// -- 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->assertXpath($xpath);
// -- File URI from media field.
$xpath = '//div[@class = "tt-file-uri-from-media-field" and contains(text(), "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);

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

@ -62,6 +62,7 @@
<div class="tt-token-replace">{{ 'Site name: [site:name]'|token_replace }}</div>
<div class="tt-preg-replace">{{ 'FOO'|preg_replace('/(foo)/i', '$1-bar') }}</div>
<div class="tt-image-style">{{ 'public://images/ocean.jpg'|image_style('thumbnail') }}</div>
<div class="tt-image-style-from-file-uri-from-media-field">{{ node.field_media|file_uri|image_style('thumbnail') }}</div>
<div class="tt-transliterate">{{ 'Привет!'|transliterate('ru') }}</div>
<div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>'|check_markup('twig_tweak_test') }}</div>
<div class="tt-format-size">{{ 12345|format_size() }}</div>
@ -85,6 +86,9 @@
<div class="tt-node-view">{{ node|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-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>
<div class="tt-file-uri-from-media-field">{{ node.field_media|file_uri }}</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-delta">{{ node.field_image[0]|file_url }}</div>

Loading…
Cancel
Save