Symfony2 hook after template render - symfony

I've got a small question here: is there a way to do some actions after a template has been rendered ? A sort of Listener or hook which is called after a specific template has been rendered ?
In my case I want to know the last datetime the user visited the homepage. If I persist this value in the Controller, so before the rendering of the corresponding template, I won't be able to use my app.user.getLastHomepageVisit datetime variable in the twig template, as the value of the latter variable would be 'now' which is not what I want. I want to update and persist this value to 'now' after the page has been rendered.
Thanks!

There's a hook on kernel.response:
http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html

Related

Many forms for editing entity on one page

I have a list of objects on my page. I need to edit an object in a popup.
There are many objects, and generating many forms for each object is not correct.
What can I do, can an iframe do in a popup?
You don't need to use iframe.
Build your form in your controller and render it in the html. The fact that it's in popup doesn't change anything. It doesn't matter if it's in popup or not, the final result will be POST call to your action.
You should have an action that renders a form, callable from an ajax in the view where you have all those entities.
Just change the entity id that will be received in the action as argument by changing the ajax url using js depending on the clicked entity you want to edit.
Then return with the ajax the form already rendered so with only one form you can edit as many entities (of the same class) as you want, without even need to render one form before they click which one they want to edit.

Editable field action in sonata list view

I have a list view in sonata admin. I want to edit integer field directly in list view. New data for this field must change other fields in my entity. How can I add action function to this editable field in which I may do some actions with other fields depending on entering integer value?
Please check documentation: https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_custom_action.html
Basically, what you need is a custom route and and a controller, same way as you handle any page/request. Than you can do any logic you like making requests to this routes (even with AJAX). Documentation will help you the best with this.
And to add an "editable field" or a link or a button in List view, just create custom template piece: https://sonata-project.org/bundles/admin/master/doc/reference/templates.html

How can I re-insert the values on a form, after a user clicks to edit something?

I have a single template that's wired up to show a post if the current user has a post in the collection. If not, it automatically shows a form.
Now I want to add an EDIT functionality. When the user clicks on the Edit button, it
Saves the post text in a variable.
Deletes the post from the collection, thus the template reactively reveals the form again.
Up to this part it all works. How can I then add the text that I just saved in a variable, into the "input" element of the form?
jQuery works for this on the console, but I don't know where to put it in my code.
On Discover Meteor, they use the Router to set the context. I'd like to try my idea with jQuery, if possible.
Thank you. Any suggestions are welcome.
You could keep your post in a session variable and in the edit click handler, set the session variable to the one you're currently viewing.
In the form inputs, you can set the value attributes to their corresponding post values.
ex: <input name="title" value="{{post.title}}" />
and in your template helper
Template.form.helpers({
post: function() {
return Session.get('post')
}
})

Twig - binary negation

I have the following code inside the twig template file:
<th class="head0">ID</th>
Order is a binary integer value (0/1). What I wanna do is to change the value of that variable everytime I click the link. I also cannot use negation in the controller, because it would change that value everytime I click any link redirecting to this specific route.
Is there a way to do this in twig?
What you want to achieve shouldn't be in Twig file. You can only render the initial values with twig. As you said you want the link to respond to user's click. There's two classic ways to do that:
this can be a Javascript code that respond to click and send an ajax request to your
controller. Then you can persist it or do whatever you want with it.
This is the recommended way.
The click redirect you to a new page and the controller that serves that route has the logic you want that is toggling a value or checking a status.

Symfony 1.4 - Accessing values from the parent of an embedded form in an instance of sfValidatorCallback

$values seems to be sandboxed to that of the form you are calling the sfValidatorCallback from.
I need to be able to query a posted value from the parent form against something in the embedded form I'm calling the post validator from. Can anyone think of a way to do this?
Can you check the possibility of overwriting the form submit/validation method in the form class to pass these value when the parent::method() is being called?

Resources