Is it possible to set a new entity directly from twig? - symfony

Sometime i need to check if an user can comment from a voter, obviously at this point of the code the comment does not yet exist, but still i need to check if the user is allowed to comment or not.
From the controller i would usually do $this->isGranted('create', new Comment()) but how i'm supposed to do the same thing in twig? I can't find a clear explaination, and i obviously can't do something like {% if is_granted('create', new comment()) %}.
Is there a way to do this without hacking an object from the controller such as rendering a new Comment() from the controller? Doing so will allow me to avoid a lot of spaghetti code in my voters.

you can extend twig with Symfony Twig Extensions
Look at this documentation link:
Symfony Twig Extensions
Inside your twig you can use is_granted like this:
{{ is_granted(role, object = null, field = null) }}

Related

Symfony 3 EasyAdminBundle Collection Type override twig

I have Entity Product with productImages. ManyToOne relationship.
I want to override product images twig. To do it, I created easy_admin folder under view and inside ProductImage folder, then edit and new twigs for test.
It is not overriding at all. Product is overridden easily is this way, but ProductImage is not.
Don't you think in theory it should work like this?
If not, please give me some clue how to override, change collection type twig.
I want to have nice bootstrap table instead current one.
Thanks
If anyone is curious how to override collection type:
in config.yml add:
easy_admin:
design:
form_theme: '/custom_form_layout.html.twig'
Then in custom_form_layout.html.twig write:
{% extends 'EasyAdminBundle:form:bootstrap_3_horizontal_layout.html.twig' %}
Or extend your base form whatever it is.
after that override block
{% block collection_row %}<div>I Love Symfony! (or just use parent())</div>{% endblock collection_row %}

TWIG variables between templates?

Beeing completely new to TWIG (it's also my first templating language) I have a little problem understanding the variables.
Heres what I need:
I have 2 layouts. One inheriting from the other.
On the first one I need to put an if on a div to add a class if on the second layout a variable is declared.
Thanks in advance.
Say in Symfony you write:
return $this->render('::index.html.twig', array('variable' => $somevar));
And 'index.html.twig' looks like this:
{% extends '::foo.html.twig' %}
{# some contents #}
and 'foo.html.twig' looks like this:
{{ variable }}
It should just work. If it doesn't work, post some code and errors here, and I'll see what I can do to help. Obviously this example is unrealistic, but all templates should have access to all variables passed from Symfony, in addition to the global ones and anything you define as Twig extensions.

Symfony2 - Call the same getRepository in all controller actions

I'm using a repository in Symfony2-controller like this:
$blog = $em->getRepository('BlogBloggerBundle:BlogData')->getBlogData($id);
Before I call it into the twig view through an array.
All works right but the issue is that it make up a footer menu, then, i should call it almost in every action which i need.
How i can call it from a "common" repository every time which i need?
Create a view reponse listener registering a twig variable and add the repository call in there...
... or create a twig extension exposing the data received from the repository as a global twig variable.
... or (my preferred choice) create a controller dedicated to rendering the footer and include the footer like this:
{{ render(controller('Bundle:controller:action')) }}
Read more about rendering fragments in this blog post on the symfony homepage.
FYI Official documentation about embedding controllers : http://symfony.com/doc/current/book/templating.html#embedding-controllers

Get current URI in Twig template

Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.

How to reference twig for custom field types in dedicated bundle?

I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.

Resources