Need just edit page not listing page in sonata admin - symfony

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 ...

Related

Hide download button in sonata admin

I would like to hide "Download" button on Sonata Admin from some of custom entity. How to hide/delete it?
If i override base_list.html.twig and remove the download button from table_footer, it disappears all of entity lists. Is there any way to hide it from Admin class?
You can hide the "download" button by removing the corresponding route:
class YourClass extends AbstractAdmin {
public function configureRoutes(RouteCollection $collection) {
$collection->remove('export');
}
}
PROBLEM SOLVED!
I solved it by managing user roles. For example:
You want to remove Download button in Post section. So you need to add below code in app/config/security.yml
security:
role_hierarchy:
ROLE_PUBLISHER:
- ROLE_ADMIN_POST_CREATE
- ROLE_ADMIN_POST_EDIT
- ROLE_ADMIN_POST_LIST
- ROLE_ADMIN_POST_VIEW
- ROLE_ADMIN_POST_EXPORT #If you comment or delete this line. Download button don't show in Sonata Post List.

SilverStripe custom FormField_Holder

I've created a simple contact form within the page controller. For the front-end view of this contact form, I wish to use a customised FormField_Holder rather than the default one.
I've created a FormField_Holder.ss within themes/templates/Includes. How do I apply this template to my $ContactForm?
I've tried this already:
public function ContactForm() {
$form = Form::create(
...
);
foreach($form->Fields() as $field) {
$field->setFieldHolderTemplate('myHolder');
}
return $form;
}
I relocated the custom form template from
themes/mytheme/templates/Includes/
to
themes/mytheme/templates/forms/
..and it works now.
Edit: The official documentation mentions the following folder for form templates: mysite/templates/Includes but this oddly doesn't work oddly.
https://docs.silverstripe.org/en/3.4/developer_guides/forms/form_templates

disable action in sonata admin bundle CRUD

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

Sonata Admin Change Edit link by Show link

I'm using SonataAdminBundle and I'm triying to change the edit link of and entity by the show link.
I want to do this because I need the entity couldn't be modified but I want you can show the entity by clicking in the Identifier field of the list page.
I need to show the entity by clicking in the Identifier, and not using the show action buttom.
So I tried in the ClassAdmin:
protected function configureRoutes(RouteCollection $collection){
$collection->add('edit', $this->getRouterIdParameter().'/show');
}
Despite the url is generated with the show correctly, the Identifier in the list page redirect to the edit page. Really, wathever I change in the edit link doesn't take efect and always redirect to the edit page.
Thansk a lot!
You can give the default action like this (in your admin classes):
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id', null, ['route' => ['name' => 'show']])
;
}
Finally, it works by:
protected function configureRoutes(RouteCollection $collection){
$collection->remove('edit');
$collection->add('edit', $this->getRouterIdParameter().'/show');
}
I don't know why I have to remove the edit link first... but it works.

How to add custom link or button to SonataAdminBundle Dashboard in Symfony2

I am new with symfony2 and SonataAdminBundle.
I have added 3 entities to the SonataAdminBundle Dashboard and they appear successfully.
The Entities appear with the default links - "Add new" and "List" buttons.
I want to be able to do the following
I want to add a third link to one of the entities on the dashboard.
I want to be able to add a link to the buttons located above the grid in the
default list page.
I want to be able to add/customize links appearing under the form on Edit or Create new page
I have not been able to find a way to do any of the above, been searching for hours, any help will be highly appreciated.
thanks.
To display custom elements in the dashbord Sonata Admin uses Sonata Block Bundle.
To add custom link or button you need to create a new block using Sonata Block Bundle.
The core template (dashboard.html.twig) of the Admin Bundle iterates blocks that is configured to run (in config.yml of the application).
Still, Admin Bundle iterates all your entity blocks in the template block_admin_list.html.twig. Creating your custom block template it is from here that you can take layout to wrap your custom groups (sections) and buttons so they have same feel as those of the entities groups.
Ok, it was introduction.
For example we want to make custom newsletter section.
There are steps:
create new block class that implements BlockBundleInterface
create new block template
create block service (read and understand what are services in Symfony 2 library)
add newly created service to Sonata Block Bundle configuration
add newly created service to Sonata Admin Bundle configuration
enter dashboard and enjoy new group/button/link/whatever-stuff-you-put-in-your-block-template :)
Ad1) Create new block class
General instruction under: http://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html
My file looks like this:
<?php
namespace InstitutoStorico\Bundle\NewsletterBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
class NewsletterBlockService extends BaseBlockService
{
public function getName()
{
return 'My Newsletter';
}
public function getDefaultSettings()
{
return array();
}
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
}
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
}
public function execute(BlockInterface $block, Response $response = null)
{
// merge settings
$settings = array_merge($this->getDefaultSettings(), $block->getSettings());
return $this->renderResponse('InstitutoStoricoNewsletterBundle:Block:block_my_newsletter.html.twig', array(
'block' => $block,
'settings' => $settings
), $response);
}
}
I added some lines reading Sonata Media Bundle code files.
Ad2) Create new block template
Layout I took from block_admin_list.html.twig of Sonata Admin bundle.
My file looks like this:
{% extends 'SonataBlockBundle:Block:block_base.html.twig' %}
{% block block %}
<table class="table table-bordered table-striped sonata-ba-list">
<thead>
<tr>
<th colspan="3">Newsletter - inviare</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="btn-group" align="center">
<a class="btn btn-small" href="#">Servizio Newsletter</a>
</div>
</td>
</tr>
</tbody>
</table>
{% endblock %}
Ad3) Create block service
In your bundle there's a file where you declare services (services.yml or admin.yml). Whatever. But it's important that it is plugged into config.yml of your application in "imports" section.
My service declaration looks like this:
sonata.block.service.newsletter:
class: InstitutoStorico\Bundle\NewsletterBundle\Block\NewsletterBlockService
arguments: [ "sonata.block.service.newsletter", #templating ]
tags:
- { name: sonata.block }
Ad4) Add newly created service to Sonata Block Bundle configuration
This configuration is put into config.yml of your application.
My config looks like this:
#Sonata Block Bundle
sonata_block:
default_contexts: [cms]
blocks:
sonata.admin.block.admin_list:
contexts: [admin]
sonata.block.service.text: ~
sonata.block.service.action: ~
sonata.block.service.rss: ~
sonata.block.service.newsletter: ~
Ad5) Add newly created service to Sonata Admin Bundle configuration
This configuration is put into config.yml of your application.
My config looks like this:
# Sonata Admin Generator
sonata_admin:
...
dashboard:
blocks:
# display a dashboard block
- { position: left, type: sonata.admin.block.admin_list }
- { position: left, type: sonata.block.service.newsletter}
Ad6) Enter dashboard and enjoy
That's all. Looks like complicated, but to be sincere it is not so much. It's important it's a clean way of modifying your dashboard page without overriding whole templates without great need.
All those Il learned reading source code of Admin Bundle :) Whole day
I had troubles with method execute (I am using Sonata 2.3.x ).
Here is the code that works for me.
Note BlockContextInterface and $blockContext->getBlock() :
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// merge settings
$settings = array_merge($this->getDefaultSettings(), $blockContext->getSettings());
return $this->renderResponse('bundleName:Block:templateName.html.twig', array(
'block' => $blockContext->getBlock(),
'settings' => $settings
), $response);
}

Resources