How to handle the perPageOptions deprecation in Symfony 4 using the admin-bundle?

Viewed 98

Recently i update a 3.4 symfony project to 4.4 and i try to clean all the new deprecations but i can't find a way to change this one from the admin-bundle. I extend the

protected $perPageOptions = [16, 32, 64, 128, 256];

property that you can find in the

vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php

and add in some of my pages extra options. How i can do that in this new versions. Is a work around or they just stop use it and need to make it on your own? I don't find anywhere any suggestion on this topic.

1 Answers

It looks like you will instead need to redefine the getPerPageOptions method, see below from https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Admin/AbstractAdmin.php#L2550

    /**
     * Returns predefined per page options.
     *
     * @return array<string, mixed>
     */
    public function getPerPageOptions()
    {
        // NEXT_MAJOR: Remove this line and uncomment the following
        return $this->perPageOptions;
//        $perPageOptions = [10, 25, 50, 100, 250];
//        $perPageOptions[] = $this->getMaxPerPage();
//
//        $perPageOptions = array_unique($perPageOptions);
//        sort($perPageOptions);
//
//        return $perPageOptions;
    }

So in your extended admin remove this line:

protected $perPageOptions = [16, 32, 64, 128, 256];

Then add this block:

public function getPerPageOptions()
{
  $perPageOptions = [16, 32, 64, 128, 256];
  $perPageOptions[] = $this->getMaxPerPage();

  $perPageOptions = array_unique($perPageOptions);
  sort($perPageOptions);

  return $perPageOptions;
}
Related