I am using sonata media bundle.
and I was wondering how can I access the media url in twig.
I just want the url, I do not need to show the media.
Any suggestions?
I am using sonata media bundle.
and I was wondering how can I access the media url in twig.
I just want the url, I do not need to show the media.
Any suggestions?
您必须使用路径媒体助手:
{% path media, 'small' %}
在上面的代码中,media
是媒体实体的一个实例,并且small
是选择的格式。
http://sonata-project.org/bundles/media/master/doc/reference/helpers.html#twig-usage
但是,如果您不想在此处呈现媒体并将 url 存储在变量中,则需要向媒体提供者询问公共 url。这是我的情况,我需要将 url 传递给另一个模板。我在我的 Twig 扩展中创建了一个自定义函数(请参见此处:http ://symfony.com/doc/current/cookbook/templating/twig_extension.html )。
如果您的扩展服务中有可用的容器$this->container
,您可以这样做:
public function getMediaPublicUrl($media, $format)
{
$provider = $this->container->get($media->getProviderName());
return $provider->generatePublicUrl($media, $format);
}
在扩展中注册函数:
public function getFunctions() {
....
'media_public_url' => new \Twig_Function_Method($this, 'getMediaPublicUrl'),
....
);
}
并从您的模板中调用您的新助手:
{% set img_url = media_public_url(media, 'small') %}
例如
问候
@javigzz 在默认上下文的情况下是完美的。我使用了自定义上下文,因此必须首先处理 $format 并考虑上下文名称:
$provider = $this->container->get($media->getProviderName());
$format = $provider->getFormatName($media, $format);
$url = $provider->generatePublicUrl($media, $format);
由于注入容器不是最佳实践,最好从提供者池中获取提供者:
class Foo {
public function __construct(Sonata\MediaBundle\Provider\Pool $pool) {
$this->pool = $pool;
}
public function getUrl($media, $format) {
$provider = $this->pool->getProvider($media->getProviderName());
$format = $provider->getFormatName($media, $format);
$url = $provider->generatePublicUrl($media, $format);
return $url;
}
}
由于@javigzz 的回答对我不起作用,这是一个适用于最新版本的树枝扩展sonata_media
:
namespace Socialbit\SonataMediaTwigExtensionBundle\Twig;
use Sonata\CoreBundle\Model\ManagerInterface;
use Symfony\Component\DependencyInjection\Container;
Class:
/**
* Description of MediaPathExtension
*
* @author thomas.kekeisen
*/
class MediaPathExtension extends \Twig_Extension
{
/**
*
* @var type Container
*/
protected $container;
/**
*
* @var type ManagerInterface
*/
protected $mediaManager;
public function __construct(Container $container, $mediaManager)
{
$this->container = $container;
$this->mediaManager = $mediaManager;
}
public function getFunctions()
{
return array
(
'media_public_url' => new \Twig_Function_Method($this, 'getMediaPublicUrl')
);
}
/**
* @param mixed $media
*
* @return null|\Sonata\MediaBundle\Model\MediaInterface
*/
private function getMedia($media)
{
$media = $this->mediaManager->findOneBy(array(
'id' => $media
));
return $media;
}
public function getMediaPublicUrl($media, $format)
{
$media = $this->getMedia($media);
$provider = $this->container->get($media->getProviderName());
return $provider->generatePublicUrl($media, $format);
}
public function getName()
{
return 'SocialbitSonataMediaTwigExtensionBundleMediaPathExtension';
}
}
服务.yml:
services:
socialbit.sonatamediatwigextensionbundle.mediapathextension:
class: Socialbit\SonataMediaTwigExtensionBundle\Twig\MediaPathExtension
public: false
arguments:
- @service_container
- @sonata.media.manager.media
tags:
- { name: twig.extension }
用法将是相同的:
{% set img_url = media_public_url(media, 'reference') %}
{{ dump(img_url) }}
您可以使用:{% path media, 'reference' %}
@Blauesocke - 尝试了您的解决方案,并且文件提供者使用两者的结果完全相同
{% set img_url = media_public_url(media, 'reference') %}
{{ dump(img_url) }}
和
{% path sonata_admin.value, 'reference' %}