Customizing a mosaic list
=========================
.. figure:: ../images/list_mosaic_default.png
:align: center
:alt: Default view
:width: 700px
It is possible to configure the default view by creating a dedicated template.
.. note::
If you want to change the default mosaic background globally,
please use the following configuration:
.. code-block:: yaml
# config/packages/sonata_admin.yaml
sonata_admin:
# ...
options:
# ...
mosaic_background: '/path/to/image.png' # or use base64
First, configure the ``outer_list_rows_mosaic`` template key:
.. code-block:: xml
@SonataMedia/MediaAdmin/list_outer_rows_mosaic.html.twig
The ``list_outer_rows_mosaic.html.twig`` is the name of one mosaic's tile. You should also extends the template and overwrite the default blocks availables.
.. code-block:: html+twig
{% extends '@SonataAdmin/CRUD/list_outer_rows_mosaic.html.twig' %}
{% block sonata_mosaic_background %}{{ meta.image }}{% endblock %}
{% block sonata_mosaic_default_view %}
{{ object.providerName|trans({}, 'SonataMediaBundle') }}
{% endblock %}
{% block sonata_mosaic_hover_view %}
{{ object.providerName|trans({}, 'SonataMediaBundle') }}
{% if object.width %} {{ object.width }}{% if object.height %}x{{ object.height }}{% endif %}px{% endif %}
{% if object.length > 0 %}
({{ object.length }})
{% endif %}
{% if object.authorname is not empty %}
{{ object.authorname }}
{% endif %}
{% if object.copyright is not empty and object.authorname is not empty %}
~
{% endif %}
{% if object.copyright is not empty %}
© {{ object.copyright }}
{% endif %}
{% endblock %}
{% block sonata_mosaic_description %}
{% if admin.hasAccess('edit', object) and admin.hasRoute('edit') %}
{{ meta.title|u.truncate(40) }}
{% elseif admin.hasAccess('show', object) and admin.hasRoute('show') %}
{{ meta.title|u.truncate(40) }}
{% else %}
{{ meta.title|u.truncate(40) }}
{% endif %}
{% endblock %}
Block types
-----------
- ``sonata_mosaic_background``: this block is the background value defined in the ObjectMetadata object.
- ``sonata_mosaic_default_view``: this block is used when the list is displayed.
- ``sonata_mosaic_hover_view``: this block is used when the mouse is over the tile.
- ``sonata_mosaic_description``: this block will be always on screen and should represent the entity's name.
The ``ObjectMetadata`` object is returned by the related admin class, and can be
used to define which image field from the entity will be displayed if available.
For instance, the SonataMediaBundle defines the method as::
use Sonata\AdminBundle\Object\MetadataInterface;
final class MediaAdmin extends AbstractAdmin
{
public function getObjectMetadata(object $object): MetadataInterface
{
$provider = $this->pool->getProvider($object->getProviderName());
$url = $provider->generatePublicUrl($object, $provider->getFormatName($object, 'admin'));
return new Metadata($object->getName(), $object->getDescription(), $url);
}
}
.. note::
In your own admin, ``media`` is a field and not the ``$object``. Therefore,
the code above must be updated this way::
use Sonata\AdminBundle\Object\MetadataInterface;
public function getObjectMetadata(object $object): MetadataInterface
{
$media = $object->getMediaField();
$provider = $this->pool->getProvider($media->getProviderName());
$url = $provider->generatePublicUrl($media, $provider->getFormatName($media, 'admin'));
return new Metadata($media->getName(), $media->getDescription(), $url);
}
You will also have to use dependency injection. For this, first define
the ``$pool`` variable and override the constructor::
use Sonata\MediaBundle\Provider\Pool;
private Pool $pool;
public function __construct(Pool $pool)
{
$this->pool = $pool;
}
Then add ``'@sonata.media.pool'`` to your service definition arguments:
.. code-block:: yaml
# config/services.yaml
services:
app.admin.post:
class: App\Admin\PostAdmin
arguments:
- '@sonata.media.pool'
tags:
-
name: sonata.admin
model_class: App\Entity\Post
manager_type: orm
group: 'Content'
label: 'Post'
The final view will look like:
.. figure:: ../images/list_mosaic_custom.png
:align: center
:alt: Customize view
:width: 700px