disable action in sonata admin bundle CRUD - symfony

IS there a simple way to disable some CRUD actions for given admin class? E.g. I just want a list of users added via front-end without the option to manually add them.

In your admin class :
protected function configureRoutes(RouteCollection $collection)
{
// to remove a single route
$collection->remove('delete');
// OR remove all route except named ones
$collection->clearExcept(array('list', 'show'));
}
Also use routeCollection at top of admin class
use Sonata\AdminBundle\Route\RouteCollection;
Docs : http://sonata-project.org/bundles/admin/master/doc/reference/routing.html#removing-a-single-route

Related

Need just edit page not listing page in sonata admin

In my one module i have t display only global data and admin can edit it .
I want to create module for that , but it is going to list page but i need direct edit page where admin can edit global values in sonata admin.
Any idea ?
Thanks in advance
Ok, here is what you can do ...
Create a MenuBuilderListener class to let them listen to the menu building event by registering to the sidebar event
In your services.yml
app.menu_listener:
class: AppBundle\Listener\MenuBuilderListener
tags:
- { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }
In your class, search for the menu-item you want to change to "only edit" ...
class MenuBuilderListener
{
public function addMenuItems(ConfigureMenuEvent $event)
{
$event->getMenu()->removeChild('the_name_of_your_menu_item');
$event->getMenu()->addChild('the_name_of_your_menu_item', ['route' => 'your_route_to_create_view']);
}
}
Mabe in newer KnpMenu Version there should be a setRoute Method directly for the MenuItem object, in my version it doesn't.
Doing this, your item should be replaced with the one pointing to your create route. To get avalable routes use the debugger in console with debug:router
Don't forget to block other routes if you dont want to list/edit and so on ...

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;
}

Get data in twig function

Is it bad practice to get data from db in twig function or I should pass it to view in controller?
My function is some kind of interface widget that is used on all pages of site admin section. Then on data change I will have to make changes in all actions. But when I get data directly in extension class our teamlead tells that it's bad MVC.
It would be best if you pass it to a view from a controller.
Your team leader is right. What you can do is create an action specific to render that widget. I.e create a custom widget, let's say you want to show the number of current active users:
class WidgetController extends Controller
{
public function usersCountWidgetAction()
{
return $this->render('widget/usersCount.html.twig', array(
"usersCount" => $this->getUsersCount();
));
}
public function getUsersCount()
{
// call the manager and get the result
}
}
Now in all your other twigs you can use
{{ render(controller('AppBundle:Widget:usersCountWidget')) }}

symfony 1.4 add custom fields to frontend form

I need to add extra fields to my registration form in front end. I used this article to add it to back end, and it works. Now I can't add them to front end.
You'll need to add those fields to the form class used on your frontend.
For example let's say your frontend form class is called myClassForm, you would add your fields in lib/form/doctrine/MyClassForm.class.php
class MyClassForm extends BaseMyClassForm
{
public function configure()
{
$this->setWidget('extra_field1', new sfWidgetFormInput());
$this->setWidget('extra_field2', new ....);
//set validators
$this->validatorSchema['extra_field1'] = ...;
}
}

Symfony 2.3 FOSUserBundle.en.yml not registering override

I am trying to override the default FOSUserBundle.en.yml from the FOS user-bundle.
I have the user bundle working fine, i have registered a user and logged in.
However when I copy the FOSUserBundle.en.yml into my own UserBundle to override the wording Symfony doesn't seem to pick it up.
This is the path I have copied the transation file to:
src/Blogger/UserBundle/Resources/translations/FOSUserBundle.en.ym
But no joy.. I have cleared the caches tried another browser but the change in the override will not come through.
I can place the change the in the original and see the changes:
vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/translations/FOSUserBundle.en.yml
Am I missing something?
Thanks,
John
To override the bundle, you should add this :
// src/Blogger/UserBundle/BloggerUserBundle.php
namespace Blogger\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BloggerUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Here's a link from the docs : http://symfony.com/doc/current/cookbook/bundles/inheritance.html
All the steps you should follow are in there.

Resources