Sonata Admin:默认选中mosaic视图的实现方法
Hey there! To make mosaic view the default display mode for your Sonata Admin entity while still keeping list view available for users to switch back to, follow these simple steps:
Locate your Admin class: Find the PHP class corresponding to your entity (e.g.,
ProductAdmin.phpor whatever you've named it) in your project'sAdmindirectory.Update the
configureListFieldsmethod: Inside this method, after defining your list fields, add thesetDefaultView()call to set mosaic as the default. Here's an example:
use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Datagrid\ListMapper; class YourEntityAdmin extends AbstractAdmin { protected function configureListFields(ListMapper $listMapper) { // First, define your regular list fields as usual $listMapper ->add('id') ->add('title') ->add('createdAt') // ... add all your required fields // Set mosaic view as the default display mode ->setDefaultView('mosaic'); } }
- Verify mosaic view is enabled: Make sure you've already configured the mosaic view for your entity (e.g., by adding mosaic-specific fields or setting up the necessary templates). If you haven't done this yet, you'll need to add that configuration first—otherwise, the mosaic view option won't appear at all.
Once you save these changes, when users navigate to the entity's list page, it will load the mosaic view by default. The list view toggle will still be visible in the top-right corner, so users can switch back whenever they need to.
内容的提问来源于stack exchange,提问作者c.bermon

