Question

I have a custom CRUD controller with an action which retrieves a custom doctrine result set and passes it to a custom template. Is it possible to make use of Sonata Admin's pagination mechanism without reusing the standard list templates?

If so, what object format does the paginator expect? I could not see any mention of this in the documentation

The template I would need to populate with results would be base_results.html.twig:

{% block num_pages %}
    {{ admin.datagrid.pager.page }} / {{ admin.datagrid.pager.lastpage }}
     - 
{% endblock %}

{% block num_results %}
    {% transchoice admin.datagrid.pager.nbresults with {'%count%': admin.datagrid.pager.nbresults} from 'SonataAdminBundle' %}list_results_count{% endtranschoice %}
     - 
{% endblock %}
{% block max_per_page %}
    <label class="control-label" for="{{ admin.uniqid }}_per_page">{% trans from 'SonataAdminBundle' %}label_per_page{% endtrans %}</label>
    <select class="form-control per-page small" id="{{ admin.uniqid }}_per_page">
        {% for per_page in admin.getperpageoptions %}
            <option {% if per_page == admin.datagrid.pager.maxperpage %}selected="selected"{% endif %} value="{{ admin.generateUrl('list', {'filter': admin.datagrid.values | merge({'_page': 1, '_per_page': per_page})}) }}">
                {{ per_page }}
            </option>
        {% endfor %}
    </select>
{% endblock %}

If I want to add an additional constraint on the result set, how would I adapt the following code from Sonata's list action?

if (false === $this->admin->isGranted('LIST')) {
            throw new AccessDeniedException();
        }

        $datagrid = $this->admin->getDatagrid();
        $formView = $datagrid->getForm()->createView();

        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

        return $this->render($this->admin->getTemplate('list'), array(
            'action'     => 'list',
            'form'       => $formView,
            'datagrid'   => $datagrid,
            'csrf_token' => $this->getCsrfToken('sonata.batch'),
        ));
Was it helpful?

Solution

The DatagridBundle is a work in progress in order to decouple the list, pagination and filtering mechanisms from the Admin for them to be reusable elsewhere. Hence it is not stable yet. I strongly recommend you use the pagination from the AdminBundle at the moment.

Regarding your main question, if you wish to understand fully how to use the pager, I recommend you take a look at the Datagrid::buildPager method (https://github.com/sonata-project/SonataAdminBundle/blob/master/Datagrid/Datagrid.php#L90).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top