I am using Sonata Admin and I have a list and a mosaic view.
How to select the mosaic view by default?
I do not want to HIDE the list view, just select the mosaic view by default.
2 Answers
The Admin classes inherit from AbstractAdmin
. If we have an Entity class Foo
, we would create an Admin class FooAdmin
extending Sonata\AdminBundle\Admin\AbstractAdmin
.
Let's take a look into the source code:
public function setListMode($mode)
{
if (!$this->hasRequest()) {
throw new \RuntimeException(sprintf('No request attached to the current admin: %s', $this->getCode()));
}
$this->getRequest()->getSession()->set(sprintf('%s.list_mode', $this->getCode()), $mode);
}
public function getListMode()
{
if (!$this->hasRequest()) {
return 'list';
}
return $this->getRequest()->getSession()->get(sprintf('%s.list_mode', $this->getCode()), 'list');
}
These are the methods that set and get the list mode. There are buttons for two list modes: list
and mosaic
. If you hover with the mouse pointer you'll see that they point to the same URL, but with different parameters:
- /list?_list_mode=list
- /list?_list_mode=mosaic
In the method getListMode
we see that the list
mode is called per default.
The way I found out to set mosaic
as default is to call the method setListMode
in the Admin class:
protected function configureListFields(ListMapper $listMapper)
{
if ($mode = $this->request->query->get('_list_mode')) {
$this->setListMode($mode);
} else {
$this->setListMode('mosaic');
}
$listMapper
->addIdentifier('fooId')
->add('fooBar')
;
}
I hope it can help someone. I was looking for more elegant ways, like setting in the admin service, but couldn't find a better solution. If someone has other suggestions, I'd be happy to learn something new.

- 11,616
- 6
- 48
- 84
-
1This solution will fail if there's no request, for example as a result of a global search. I would suggest to simply do: if ($this->hasRequest()) { $this->setListMode('mosaic'); } – devilcius Nov 27 '18 at 09:00
-
@devilcius I didn't think of that edge case.Thank you for pointing out. For me it did work this way. I think an ideal solution would be to set the option in the admin service, but I don't know how to do it. Any fiddling in the method `configureListFields` is a dirty approach. – cezar Nov 27 '18 at 09:33
-
@devilcius The problem with your solution would be that the mosaic view will be shown, even if the user clicks on the list button. That would be irritating. The if condition should be expanded with your snippet, in order to catch the edge case you mentioned. It is however still a quick-and-dirty solution and I'm aware of that, but in lack of deeper understanding and better knowledge I have to stick with it for the time being. – cezar Nov 27 '18 at 09:37
In the method getListMode we see that the list mode is called per default.
So in my opinion the simplest way is to override getListMode
and to replace 'list' by 'mosaic'.
public function getListMode()
{
if (!$this->hasRequest()) {
return 'mosaic';
}
return $this->getRequest()->getSession()->get(sprintf('%s.list_mode', $this->getCode()), 'mosaic');
}

- 79
- 2