Symfony2 FOSUserBundle login form styling - symfony

I already have register form styled (I changed some widgets by adding fields.html.twig and load it globally) but the styles won't be applied to login form elements, as the html tags are rendered here directly, not using Symfony's helpers. Should I rewrite FOS Security controller to get the form variable or something?

Currently the SecurityController:renderLogin action doesn't return a variable containing a FormView instance.
The form's HTML code (and therefore the classes you could use for styling) is hardcoded into the login template.
You have the following two options for overriding the template containing the HTML code.
1) override the template in app/Resources/FOSUserBundle
You can simply override the template FosUserBundle:Security:login.html.twig by putting a login.html.twig into the folder
app/Resources/FOSUserBundle/views/Security/
Just make sure you include ...
{% extends "FOSUserBundle::layout.html.twig" %}
... in your newly created template.
2) override the template using bundle inheritance
Another way of overriding the template would be using bundle inheritance.
Therefore you would create a new bundle that extends FOSUserBundle by returning 'FOSUserBundle' in it's getParent() method.
class ExtendingBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Don't forget to register the bundle in app/AppKernel.php.
Now you can store your new login.html.twig template at
src\Vendor\ExtendingBundle\Resources\views\login.html.twig
documentation
Read more about template inheritance in the documentation.

Related

How to override template/view of SilverStripe's User Defined Form

I am developing a simple SilverStripe project for learning purpose. I am using SilverStripe User Defined Form module to create form pages.
The way the user defined form module renders the form in the template/ view is using the $UserDefinedForm variable in the content field. Now, I need to do some customization to the built-in User Defined Form template. So I decided to override the controller and page.
This is my EventPage
<?php
namespace {
use SilverStripe\UserForms\Model\UserDefinedForm;
class EventPage extends UserDefinedForm
{
}
}
This is my EventPageController
<?php
namespace {
use SilverStripe\UserForms\Control\UserDefinedFormController;
class EventPageController extends UserDefinedFormController
{
}
}
This is my EventPage.ss
<h1>Event Page</h1>
$UserDefinedForm
This page is overridden, but the form is not rendered. How can I render the form?
A complete answer might help.
ContactPage.php
namespace InsiteApps\CMS\Model {
use SilverStripe\UserForms\Control\UserDefinedFormController;
use SilverStripe\UserForms\Model\UserDefinedForm;
class ContactPage extends UserDefinedForm
{
private static $table_name = 'ContactPage';
}
class ContactPageController extends UserDefinedFormController
{
}
}
In your theme, templates folder (the folder structure is dependant on your namespace)
templates/InsiteApps/CMS/Model/Layout/ContactPage.ss
<div class="contact-page-form">
$Form
</div>

How can I override the layout of the form at /admin/sonata/user/user/{id}/edit

I have extended the Sonata UserAdmin by creating Application\Sonata\UserBundle\Admin\Model\UserAdmin and extending Admin, then commenting out some fields I would rather not display.
From sonata_user in config.yml:
admin: # Admin Classes
user:
class: Application\Sonata\UserBundle\Admin\Entity\UserAdmin
controller: SonataAdminBundle:CRUD
translation: SonataUserBundle
Where is the template for the form which gets displayed at /admin/sonata/user/user/{id}/editand what are the steps required to override it?
The templates for your forms are in vendor/Sonata/...Resources/views
There are two ways to override these templates. The easiest is to override an individual template by creating it at app/Resources/PATH/view.html.twig.
PATH => the path to access the view you override in vendor, you have to recreate it. I said view.html.twig, but it can be another name, just need to be the same.
So the same way you did with the UserAdmin entity, but in the resources.
The other way is in the case you did your own bundle, that will be the son of one of your vendors bundle.
To get more information, FOSUserBundle documentation is great about how to override things from a parent bundle.
Check this : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md
There is also doc on how to override form & controllers.
Good luck !
Override getTemplate method in UserAdmin class:
public function getTemplate($name)
{
switch ($name) {
case 'edit':
return 'Application\Sonata:User:edit.html.twig';
break;
default:
return parent::getTemplate($name);
break;
}
}
and create Application\Sonata\Resources\views\User\edit.html.twig that will override Sonata's template:
{# edit.html.twig #}
{% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}
And now you can override the blocks from SonataAdminBundle:CRUD:edit.html.twig as you want.

Symfony project; SonataUser, FOSUser and their registration_content.html.twig

Solved -- see bottom of entry
I'm trying to get familiar with the SonataUserBundle extending the FOSUserBundle.
The installation worked fine (as far as I can tell) and now I want to customize
the login and registration forms.
I overwrote templates in app/Resources and it worked fine.
However, for the registration form I do not understand why it works...
Here's my problem:
The SonataUserBundle registration controller (RegistrationFOSUser1) sets up the form
and renders it with FOSUserBundle:Registration:register.html.twig as template:
$form = $this->container->get('sonata.user.registration.form');
$formHandler = $this->container->get('sonata.user.registration.form.handler');
[...]
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
register.html.twig includes FOSUserBundle:Registration:register_content.html.twig:
{% block fos_user_content %}
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
{% endblock fos_user_content %}
register_content.html.twig contains the twig code to render the form.
However, what is actually rendered is SonataUserBundle:Registration:register_content.html.twig
I just can't figure out where, when and how SonataUserBundle substitutes FOSUserBundle here...
Thanks for any hints!
Ok, I now see that the solution to my question is well documented in the symfony cookbook:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html
For those as new to symfony as myself:
If you define a parent 'ParentBundle' for another bundle 'ChildBundle', then everytime a function, template etc. from ParentBundle is called, symfony will first look whether there is a file with the same name in ChildBundle.
The parent bundle is defined in the ChildBundle.php:
public function getParent()
{
return 'ParentBundle';
}
This works, as long as the file of the parent bundle is called via the usual ParentBundle:path:file notation.
Ok, I now see that the solution to my question is well documented in the symfony cookbook: http://symfony.com/doc/current/cookbook/bundles/inheritance.html
For those as new to symfony as myself:
If you define a parent 'ParentBundle' for another bundle 'ChildBundle', then everytime a function, template etc. from ParentBundle is called, symfony will first look whether there is a file with the same name in ChildBundle.
The parent bundle is defined in the ChildBundle.php:
public function getParent()
{
return 'ParentBundle';
}
This works, as long as the file of the parent bundle is called via the usual ParentBundle:path:file notation.

Overriding the FOSUserBundle controllers

So I read alot about the overriding of templates and such and overriding of bundles in Symfony.
I am using the new Symfony 2.3, I have not tried this in lower versions of Symfony.
I followed the tutorial about overriding bundles in Symfony:
http://symfony.com/doc/2.3/cookbook/bundles/inheritance.html
I followed the tutorial about overriding the controllers of FOSUserBundle, which is the same thing really:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md
I had a bundle named Acme/WebBundle.
Now I have done the following things:
Created a new bundle named Acme/UserBundle.
Created the file AcmeUserBundle.php in this bundle.
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Created the following file structure:
-src
-Acme
-UserBundle
-Controller
RegistrationController.php
-Entity
User.php
-Resources
-translations
-views
AcmeUserBundle.php
In RegistrationController.php I set the namespace to:
namespace Acme\UserBundle\Controller;
Copied the contents of the registration controller of FOSUserBundle to mine.
Added to the beginning of registerAction()
die("message");
Now when I go to the registration form, the default /register route, I don't get a die, everything works fine. It does not see my bundle as a child, nothing is overridden and I've been trying to get it to work for ages hence my question here.
Did I do something wrong?
Remember that you need to add any new bundle to AppKernel::registerBundles() in app/AppKernel.php like this:
$bundles = array(
...
new Acme\UserBundle()
);

stripslashes inside Twig template

i want to use the php stripslashes function inside a twig template but this function is not a standard twig function, so i have to add it to twig as an extension, i tried this code inside a controller, but it doesnt work:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class XController extends Controller
{
public function YAction($page)
{
$twig=$this->get('twig');
$twig->addFunction('functionName', new Twig_Function_Function('someFunction'));
...
do i need a use statement for the "Twig_Function_Function" class?
am i doing this wrong?
If you want to use it in your twig templates, you don't need to make any add or call inside your controller, Read the How to write a custom Twig Extension section of the documentation.
Basicaly, you need to create an Extension Class that extends \Twig_Extension , then you need to register it as a service using the twig.extension tag. And finally you need to implement the getFunctions() method in order to add customized twig functions.
But in your case better is to add a filter, with the same logic you can also add a getFilters() method in your extension class so that you can specify your customized filters.
Also, take a deeper look at the Extending Twig section of the documentation to understand all the ways twig can be extended.
Or {{ function('stripslashes', "This\\'ll do") }}
(or apply stripslashes() when building your Twig context)
But, also, if you do this in php:
add_filter('timber/twig', function(\Twig_Environment $twig) {
$twig->addFunction(new Twig\TwigFunction('stripslashes', 'stripslashes'));
return $twig;
});
Then this works in twig:
{{ stripslashes("This\'ll do...") }}

Resources