Skip to content

Instantly share code, notes, and snippets.

@LogansUA
Last active August 2, 2019 11:55
Show Gist options
  • Save LogansUA/e89456c3d1fdd88946ae to your computer and use it in GitHub Desktop.
Save LogansUA/e89456c3d1fdd88946ae to your computer and use it in GitHub Desktop.
This is a custom sonata admin list button action.

Custom Sonata Admin List view Action

<?php
// YourBundle/Admin/EntityAdmin.php
namespace Namespace\YourBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;

/**
 * Class EntityAdmin
 */
class EntityAdmin extends Admin
{
    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('field1')
            ->add('field2')
            ...
            ->add('_action', 'actions', [
                'actions' => [
                    'entity_board' => [
                        'template' => 'YourBundle:Admin/CRUD:list__action_entity_board.html.twig'
                    ],
                ],
            ]);
    }
    ...
}

Create custom action template

  • You can change icon just changing the fa-tachometer class to fa-stop. Full icons list
{# src/Namespace/YourBundle/Resources/views/Admin/CRUD/list__action_entity_board.html.twig #}
{% if admin.isGranted('VIEW', object) and admin.hasRoute('show') %}
    <a href="{{ path('your_controller_action', { 'id': object.id }) }}" class="btn btn-sm btn-default view_link" title="Show entity board">
        <i class="fa fa-tachometer"></i>
        Your action
    </a>
{% endif %}
@manu-sparheld
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment