sonata-adminsonatasonata-media-bundle

Default sort in Sonata Admin - Mediabundle


When I use the Sonata Mediabundle, the latest images which were uploaded come up on the 80th something page, therefore I thought it can be easier to create a new extended bundle, which changes the order of the sort using the updated time :

<?php

namespace Application\Sonata\AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin as SonataAdmin;

/**
 * Class ClientAdmin
 *
 * @package AppBundle\Admin
 */
class ClientAdmin extends SonataAdmin
{

    /**
     * @param string $code
     * @param string $class
     * @param string $baseControllerName
     */
    public function __construct($code, $class, $baseControllerName)
    {
        parent::__construct($code, $class, $baseControllerName);
        $this->datagridValues = array(
            '_page' => 1,
            '_sort_order' => 'DESC',
            '_sort_by' => 'updatedAt',
        );
    }
}

The service looks like this:

services:
  application_sonata_admin.admin.client_admin:
      class: Application\Sonata\AdminBundle\Admin\ClientAdmin
      tags:
          - { name: sonata.admin, manager_type: orm, label: "Clients"}
      arguments:
          - ~
          - '%sonata.classification.admin.collection.entity%'
          - '%sonata.classification.admin.collection.controller%'

When I directly modified the datagridvalues in the admin bundle, the sort worked well ( DESC by updatedAt), what did I do wrongly with the extend? I followed this instruction.

UPDATE: As a helpful comment below, I removed the extension of the main admin, and tried to extend the BaseMedia, but the order is still wrong.

<?php

namespace Application\Sonata\MediaBundle\Admin;

use Sonata\MediaBundle\Admin\BaseMediaAdmin;

/**
 * Class MediaAdmin
 *
 * @package AppBundle\Admin
 */
class MediaAdmin extends BaseMediaAdmin
{
    /**
     * @param string $code
     * @param string $class
     * @param string $baseControllerName
     */
    public function __construct($code, $class, $baseControllerName, $pool)
    {
        parent::__construct($code, $class, $baseControllerName, $pool);

        $this->datagridValues = array(
            '_page' => 1,
            '_sort_order' => 'DESC',
            '_sort_by' => 'createdAt',
        );
    }
}

Solution

  • In the services.yml, I needed to add the following line:

     parameters:
        sonata.media.admin.media.class: Application\Sonata\MediaBundle\Admin\MediaAdmin
    

    Also I extended wrong class, what I needed to extend was the Sonata\MediaBundle\Admin\PHPCR\MediaAdmin class instead of the Sonata\MediaBundle\Admin\BaseMediaAdmin.

    <?php
    
    namespace Application\Sonata\MediaBundle\Admin;
    
    use Sonata\MediaBundle\Admin\PHPCR\MediaAdmin as BaseMediaAdmin;
    
    /**
     * Class MediaAdmin
     *
     * @package Application\Sonata\MediaBundle\Admin
     */
    class MediaAdmin extends BaseMediaAdmin
    {
        protected $datagridValues = array(
            '_page' => 1,
            '_per_page' => 25,
            '_sort_order' => 'DESC',
            '_sort_by' => 'createdAt',
        );
    }