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

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.

Related

How to embed Comment on Custom Template in Drupal 8 Twig

I have a content type named Project and I created a template for it page--project.html.twig I am new with Drupal and I want to include a comment box and comment list on this page but not sure what to do. I already added a comment field on my content type. I tried rendering it using {{ node.field_comments }} but I am getting error. How can I include a comment on a
Because variable node is not available in template page so you have to get field comment, render it and then append it to your template by implementing hook_preprocess_page:
<your_theme>.theme
/**
* Implements hook_preprocess_HOOK().
*/
function <your_theme>_preprocess_page(array &$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
if (!empty($node)) {
$variables['field_comment'] = $node->comment->view('full');
}
}
page--project.html.twig
{{ field_comment }}

Undefined offset : 1 error on render controller Symfony

I am stuck with a problem.
I have a controller in my Symfony 2.8 project named 'AchievementController'. which I put inside a folder named 'Registration'. I have a action named listAction which use to list all achievements. So my folder structure to controller is
AppBundle > Controller > Registration > AchievementController
I am using a file named list.html.twig to list all achievemets. I am planning to use render controller method to render listAction on another twig named new.html.twig. And my twig is placed at
AppBundle > Resources > views > Registration > Achievements > list.html.twig and
AppBundle > Resources > views > Registration > Achievements > new.html.twig.
My problem is when I used render controller method I got an error
"An exception has been thrown during the rendering of a template ("Notice: Undefined offset: 1") in AppBundle:Registration/Achievement:new.html.twig"
I have search a lot but every post tell to do the same way I did (I believe), but not working. And I am stuck there. I am attaching my action and twig. Do I want to set some configuration? What mistake I have done?? I have check the same in other projects and it works fine. So I'm confused. (And when I use a folder structure AppBundle > Controller > AchievementController and change code for this, it works fine. Simply it feels like a escape slash problem. But I didn't get any way to fix it.)
new.html.twig
{{ render(controller('AppBundle:Registration/Achievement:list')) }}
listAction
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:Registration\Achievement')->findAll();
return $this->render('AppBundle:Registration/Achievement:list.html.twig', array(
'entities' => $entities,
));
list.html.twig
{% if entities %}
--------
--------
{% endif %}
Thanks for every answers.
I have used yml method to generate bundle.
I am attaching my action and controller here. (Removed other actions in controller)
<?php
namespace AppBundle\Controller\Registration;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Registration\Achievement;
use AppBundle\Form\Registration\AchievementType;
class AchievementController extends Controller {
public function listAction() {
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('AppBundle:Registration\Achievement')->findAll();
return $this->render('AppBundle:Registration/Achievement:list.html.twig', array(
'entities' => $entities,
));
}
}

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.

Symfony2 FOSUserBundle login form styling

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.

Twig CamelCase Filter in Symfony2

So I'm pretty new to Symfony2 and I'm trying to use the camelize filter in a twig template. However when I request the page I get an error saying that the filter doesn't exist:
The filter "camelize" does not exist in ::base.html.twig
Here's the line from my template file:
{{ 'hello world' | camelize }}
The filter is listed on Twig's quick reference page.
I'm confused, doesn't Symfony2 support all of twig's filters? There seem to be quite a few missing, why? And if it doesn't support them, then is there any way to add the missing ones in?
Thanks in advance!
edit Ok, so it turns out I'm retarded and I need to remember to check that I've actually got the right git project. No wonder I was confused. Thanks replies!
Symfony 2 has title filter for camel case use
{{ entity.yourstring | title }}
to camel case your string
Your link points to a fork on GitHub, meaning a modified copy of the original project. The original project is https://github.com/fabpot/Twig.
There is no camelize filter in Twig. Built-in filters are here. You can write your own camilize filter (it's easy, actually...) following this tutorial: How to write a custom Twig Extension.
EDIT: just for fun, you can write something like:
class MyTwigExtension extends Twig_Extension
{
public function getFilters()
{
return array(
'camelize' => new Twig_Filter_Method($this, 'camelizeFilter'),
);
}
public function camelizeFilter($value)
{
if(!is_string($value)) {
return $value;
}
$chunks = explode(' ', $value);
$ucfirsted = array_map(function($s) { return ucfirst($s); }, $chunks);
return implode('', $ucfirsted);
}
public function getName()
{
return 'my_twig_extension';
}
}
Note that this is a quick and dirty filter! Take a look at the built-in filters to learn best practice!
The filter you're looking for is named "title": http://twig.sensiolabs.org/doc/filters/title.html
Here is the best solution by default in Craft CMS 3
Craft 3 now has a |camel filter for twig
https://docs.craftcms.com/v3/dev/filters.html#camel
{{ 'foo bar'|camel }}
{# Output: fooBar #}

Resources