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.
Related
I have an application using Sonata Admin for the back-office.
If I use disabled => true it will remove the field from being editable in edit and create view.
Is there a way to disable it only on the edit screen?
I think you should try the following code to get the subject of the Admin and check if subject exist then disable the field. Update your configureFormFields method inside admin.
/** #var YourClass $subject */
$subject = $this->getSubject();
if($subject) {
// DISABLE THE FIELD
} else {
// ENABLE THE FIELD
}
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 ...
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
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.
I am using WordPress, and I want to remove "profile" menu-option completely
Any one is having idea how can I achieve this?
Thanks
For the sake of completeness, here's how to do it programmatically...
// Run the function on admin_init
add_action('admin_init', 'remove_profile_menu');
// Removal function
function remove_profile_menu() {
global $wp_roles;
// Remove the menu. Syntax is `remove_submenu_page($menu_slug, $submenu_slug)`
remove_submenu_page('users.php', 'profile.php');
/* Remove the capability altogether. Syntax is `remove_cap($role, $capability)`
* 'Read' is the only capability subscriber has by default, and allows access
* to the Dashboard and Profile page. You can also remove from a specific user
* like this:
* $user = new WP_User(null, $username);
* $user->remove_cap($capability);
*/
$wp_roles->remove_cap('subscriber', 'read');
}
I know this is late but I just stumbled on this and thought I would add to it. That does remove the sub-menu profile menu item but does not remove the menu profile item. For someone like me who has created a completely custom profile page, I don't want my users to access the profile.php page at all. So this code will work for that:
function remove_profile_menu() {
remove_submenu_page('users.php', 'profile.php');
remove_menu_page('profile.php');
}
add_action('admin_menu', 'remove_profile_menu');
And if you only want to do this for certain capabilities....use this code:
function remove_profile_menu() {
// Only the Admin can see the profile menu
if(!current_user_can('update_core')) {
remove_submenu_page('users.php', 'profile.php');
remove_menu_page('profile.php');
}
}
add_action('admin_menu', 'remove_profile_menu');
You can use the current_user_can() function to determine who you want to see the menu items.
Profiless plugin does that on the subscriber-level.
If you wish to do that for other groups, you should probably use it in combination with Capability manager plugin.