|
|
|
@ -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. |
|
|
|
|
* |
|
|
|
|