I am new to Symfony & trying to list images in sonata admin bundle. But i am bit confuse how to get an object in twig file so that i can get my exact path for image source.
here is my code
protected function configureListFields(ListMapper $listMapper)
{
// $image = $this->getSubject();
$listMapper
->addIdentifier('caption')
->add('image','string', array('template' => 'swaamImageUploaderBundle:Admin:list_image.html.twig'))
;
}
and here is my list_image.html.twig file
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
<img src="{{ 'uploads/images/822b23a922f43bb664cb58ca57de6cccccc962e5.jpeg'}}">
{#<img src="{{ asset(image.getthumbWebPath) }}">#}
{% endblock %}
in my image source tag i have given a hard code path for my testing. but don't know how to get path from db.
plus, when i write only ->add('image') in controller i get my exact path from db displayed in back end.
I have an entity image.
any one who can help me ?
I suppose you're using Sonata Media Bundle
you must use the entity and you can find needed helpers here: http://sonata-project.org/bundles/media/2-2/doc/reference/helpers.html
If you need to get the full path in controller (can happen, very rarely but can happen) you have to use the media service
$mm = $this->container->get('sonata.media.manager.media');
$pr = $this->container->get('sonata.media.provider.image');
$media = $mm->findOneBy(array('id' => $idImage));
$format = $pr->getFormatName($media, 'default');
$path = $pr->generatePublicUrl($media, $format);
or use the twig helper inside the controller
$format = 'default'; //the format you want to show
$path=$this->get('sonata.media.twig.extension')->path($image, $format);
if you want to add the image to the list field of Sonata Admin (your case) you can use this code
{% block field %}
{% thumbnail object.image, 'thumb' %}
{% endblock %}
where image is the image getter method of your entity (es getImage() )
Related
i'm trying to add a field type url in the list view of an entity, this is the link at the documentation -> https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/field_types.html#url .
This is my code, i've simply copied the documentation:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('url', 'url', [
'url' => 'http://example.com'
]);
}
This seems to work but the column "Url" is always empty.
I found the template of Sonata that is responsible to render this field -> #SonataAdmin/CRUD/list_url.html.twig .
Here is the code
{% extends get_admin_template('base_list_field', admin.code) %}
{% block field %}
{% spaceless %}
{% if value is empty %}
{% else %}
{% if field_description.options.url is defined %}
...
The problem is that value is always empty, i don't know what is this variable; and the documentation is not talking about any field named value.
So you can achieve this by creating a template which simply contains a button with the URL you'd like to link to. See below:
First we define a field on the list view which references a template, the type is null:
->add('foo', null, [
'template' => 'example/foobar.html.twig',
])
Inside our template we've just referenced, we can do the following:
{% extends '#SonataAdmin/CRUD/base_list_field.html.twig' %}
{% block field %}
<a class="btn btn-success" href="http://google.co.uk/">My Link</a>
{% endblock %}
and now you should see the button display as a column on the list view.
It would be nice if the documented suggestion worked as intended, this solution is a work around.
does anyone know now to create a custom view type for ez platform? The default 3 have been exhausted and we need a new one for 'link'
Alternatively, does anyone know how to use the render( controller( with a custom template as this would also resolve out block right now.
Basically, we have a multi-relational field in a content object used and we need to print links to all the related contentIds, path works great but we cannot find a way to extract the name of the content object for the link without doing some fairly funky tpl logic of passing in params.
EG: As a hack for now we can pass in "embed_type" as a custom param with the render(controller("ez_content:viewAction" to pull in an alternate view for the content object for a specific content type and view type.
{% if embed_type is defined %}
{% include "embed/#{embed_type}.html.twig" %}
{% else %}
<h1>{{ ez_field_value( content, 'name') }}</h1>
{% endif %}
However, this is very ugly and all we really want to do is use 1 template for all content types, so all we need to do is loop through the relational field and print links (as the only thing available in the content field: "destination ids"). I am sure there used to be this option in the docs but i cannot find it anymore eg:
{% set links = ez_field_value( footer, "first_links_row" ).destinationContentIds%}
{% for id in links %}
{{ render(controller("ez_content:viewAction", {"contentId": id, "template": "link.html.twig"})) }}
{% endfor %}
Where the link.html.twig would simple print the link:
<a href="{{ path( "ez_urlalias", {"contentId": id} ) }}">
{{ ez_field_value( content, "name" ) }}
</a>
If using a custom tpl is not possible with the render (controller ( helper then a new custom view type would also fix this issue, but i cannot find documentation for either.
You can create a twig function that would do that. We have something like this:
Definition:
new Twig_SimpleFunction(
'content_name',
array($this, 'getContentName')
),
Implementation:
public function getContentName($content, $forcedLanguage = null)
{
if (!$content instanceof Content && !$content instanceof ContentInfo) {
$contentInfo = $this->repository->getContentService()->loadContentInfo($content);
} elseif ($content instanceof Content) {
$contentInfo = $content->contentInfo;
} else {
$contentInfo = $content;
}
return $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, $forcedLanguage);
}
which enables you to provide either content id, content info or content itself, and it returns translated content name
Hello so I am using slim framework and twig, and here is my current code in php:
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Now I want to put the if statement in my template file. How can I use the file_exists function in my twig template so I can check whether a file exists?
You can create your own function or test and just pass the arguments to the PHP function.
$test = new Twig_SimpleTest('ondisk', function ($file) {
return file_exists($file);
});
And then in your template:
{% if filename is ondisk %}
blah
{% endif %}
Unfortunately is exists sounds weird in English. Perhaps a function would make more sense.
Creating a custom function is just fine if you really need to make the validation on template side. But Twig is not meant to be used that way.
You can just make the valitadion php side and pass a flag to your template:
PHP
$filename = '/path/to/foo.txt';
$file_exists = file_exists($filename);
// ...
$app->render(
'yourTemplate',
array( 'file_exists' => $file_exists )
);
TWIG
{% if file_exists %}
do stuff
{% endif %}
Disclaimer: I don't know the exact way to render a twig template using Slim (Symfony2 guy here), but it's same logic.
For those who use Symfony with Liip Image vich_uploader, and want to check, if a DB stored files exists or not (for example on image list / gallery), this is my solution:
{% set filePath = vich_uploader_asset(image, 'imageFile') %}
{% if filePath is not null %}
....
{% endif %}
i can't figure out, how to render a macro in Symfony 2 controller. This is how i can render a twig template
$this
->get("twig")
->render("AcmeBundle:Product:table.html.twig", array(
"product" => $product
))
;
So i searching for something similar, but for rendering a twig macro. Thx for any suggestions!
Twig macro's are something inside a template. They are run whenever you render a template executing the macro.
Just create another "wraper" template, which would have only that macro in it. Something like
macro.html.twig file
{% macro sample(item) %}
{# some code here #}
{% endmacro sample #}
sample_macro_wrapper.html.twig
{% from 'macro.html.twig' import sample %}
{{ sample(item) }}
controller.php
public function someAction()
{
// ...........
$renderedMacro = $this->get('twig')
->render('sample_macro_wrapper.html.twig', ['item' => $item]);
}
SETUP:
Twig 1.13.1
PHP 5.4.3
PROBLEM:
I am needing help setting up a custom tag that calls a function that i have already built...
Current Code:
Template Code
{% set stories = get_latest_stories(2, sports) %}
{% for story in stories %}
{{ story.headline }} <br>
{% endfor %}
Controller
$function = new Twig_SimpleFunction('getViewStories', function (section, limit) {
return news_stories::getStories(section,limit);
});
$twig->addFunction($function);
$twig->render("storyList.html");
GOAL:
No with that said I would like to use a custom tag like
{% get_latest_stories 2 sports %}
to call the same function as above. The new way looks nicer and is easier to follow
Why not fetch your stories in the controller instead of the template? This does not seem like a job for the view layer...
So, something like this:
$twig->render("storyList.html", array(
'stories' => news_stories::getStories($section, $limit)
));
Then, you'll have a stories variable available in your template.
here is simple example how to write twig extension
Following code is taken from my unfinished project
function file_import($value){
//some code here
return $value;
}
$app['twig']->addFunction('file_import', new Twig_Function_Function('file_import'));
usage
{{ file_import('value') }}