How to change Symfony Sonata's flash message - symfony

The default flash message in Sonata admin bundle adds the key and value to the message:
Item "AppBundle\Entity\Users:00000000342d9b58000000004a2ab3f9" has been successfully created.
Could someone tell me how to get rid of the key and just have the value displayed?
Thank you.

I believe there is an answer to this question on sonata project github.
Try to add __toString($object) method to your Sonata Admin class:
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Entity\Category;
class CategoryAdmin extends AbstractAdmin
{
public function toString($object)
{
return $object instanceof Category
? $object->getName()
: 'Category'; // shown in the breadcrumb on the create view
}
}

For example override flash_create_success -> (Item "%name%" has been successfully created.), you need to create SonataAdminBundle.en.xliff file in your SonataAdminBundle's child and after that insert into file:
<trans-unit id="flash_create_success">
<source>flash_create_success</source>
<target>WHAT DO YOU WANT TO SHOW.</target>
</trans-unit>
Clear cache and test :)

Related

Sonata Admin Class : add KnpMenu links pointing Admin class with custom route

Using SonataAdminBundle with Symfony2, I'm looking for a solution to access some Admin classes with a specific route.
For example, I have a ContractAdmin class with boolean fields such as "Enabled".
What I would like is to add in the left KnpMenu of sonata admin, some links pointing to the same Admin class but with a custom route (other than the default "list" route), for example:
Contracts
All Contracts
Contracts enabled (Listing only enabled contract)
Contracts not yet enabled (Listing only not enabled contract)
This would avoid me to use filters.
So, how could I create and put these links to the menu which target the corresponding admin class controller with a custom route?
Thank you ;)
I've solved it declaring a custom CRUDController for this admin class and adding the actions needed calling listAction method :
class ContractAdminController extends Controller {
public function contractsEnabledAction() {
return $this->listAction();
}
I've declared this custom route into the Admin class :
protected function configureRoutes(RouteCollection $collection) {
parent::configureRoutes($collection);
$collection->add('contracts_enabled', 'contractsEnabled/');
}
Then, overriding the createQuery method in the admin class, I'm using the request "_route" attribute like that :
public function createQuery($context = 'list') {
$query = parent::createQuery($context);
switch ($this->getRequest()->get("_route")) {
case "admin_acme_contract_contracts_enabled" :
$query->andWhere(
$query->expr()->eq($query->getRootAliases()[0] . '.enabled', ':param')
);
$query->setParameter('param', true);
break;
}
return $query;
}

override parameter of anohter bundle

I'm building a bundle, which depends on another bundle. The parent bundle loads a services.yml file, which defines some parameters:
parameters:
xbundle.doctrine.factory: Doctrine\ORM\Repository\DefaultRepositoryFactory
services:
....
I know the xbundle.doctrine.factory parameter can be changed from app/config/config.yml, but I want to change its value the from within my custom child bundle. I read the docs, and also the suggested stackoverflow questions, but still can't figure how to achieve it.
You must write a CompilerPass in your child Bundle, and change the value:
// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
namespace Acme\DemoBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class OverrideServiceCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->setParameter('xbundle.doctrine.factory', '..New Value ...');
}
}
Some documentation here.

Why symfony can't find template it rendered in other function

I have a function in my controller like this:
<?php
namespace GradeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use GradeBundle\Entity\User;
use GradeBundle\Entity\SchoolClass;
class MainController extends Controller
{
/**
* #Route("/", name="index_page")
*/
public function index()
{
return $this->render('GradeBundle:Default:index.html.twig');
}
It renders the twig template correctly. However when I use other function:
/**
* #Route("/adminNew", name="add_admin")
*/
public function addAdmin()
{
$session = new Session();
if($session->get('loggedIn') == null)
return $this->redirect($this->generateUrl('index_page'));
else
return $this->render('GradeBundle:Add:newAdmin.html.twig');
}
I have the following error:
Unable to find template "GradeBundle:Default:index.twig.html".
Does anybody have any idea what might be wrong?
It's a typo somewhere you call template:
GradeBundle:Default:index.twig.html
But you have only GradeBundle:Default:index.html.twig template.
Note the difference: html.twig twig.html
I suspect that you extend it in GradeBundle:Add:newAdmin.html.twig by:
{% extends 'GradeBundle:Default:index.twig.html' %}
but should be:
{% extends 'GradeBundle:Default:index.html.twig' %}
Have you made sure to use the correct namespace for the Controller you're using? And are you including the correct files? Also I'm not sure I understand the question correctly - are you saying if you add another function with a different twig file render, the first one no longer renders? Could I see your class names and the namespaces / use statements?
Usually in these instances, it's that the templates are in the wrong place or the correct file is not included in order to find it.
Michael

containerAware and Controller in symfony2

FOSUserBundle profile controller
use Symfony\Component\DependencyInjection\ContainerAware;
class ProfileController extends ContainerAware
some functions ok ... but when i try then creat form
$form = $this->createForm
This error appear: Call to undefined method ProfileController::createForm()
BUT when i change it to this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ProfileController extends Controller
The form is rendered... so ... i dont know how can i add this controller to my class and dont remove the ContainerAware ? :/
//
MY solution ?
instead of containeraware i use
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
And then
class ProfileController extends Controller implements ContainerAwareInterface
But i dont know i cant see a different i am noob now so... is it good solution or i will broke something?
To answer your original question,
Replace:
$form = $this->createForm
With:
$form = $this->container->get('form.factory')->create($type, $data, $options);
The createForm method is just a convenience method defined in Symfony\Bundle\FrameworkBundle\Controller\Controller. For various reasons, 3rd party libraries tend not to extend the Controller class. Hence createForm is not available.
The real question is: why are you trying to extend the Profile controller? In most cases it is not necessary. It's better to do your customization by listening to events. That of course assumes you are using the development version of FOSUserBundle.
Controller is already ContainerAware - from Controller declaration:
class Controller extends ContainerAware
Have a look at this blog Symfony2: Moving Away From the Base Controller by Richard Miller

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