recipe_customizing_a_mosaic_list.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Customizing a mosaic list
  2. =========================
  3. Since version 2.4, the AdminBundle now include a mosaic list mode in order to have a more visual representation.
  4. .. figure:: ../images/list_mosaic_default.png
  5. :align: center
  6. :alt: Default view
  7. :width: 700px
  8. It is possible to configure the default view by creating a dedicated template.
  9. First, configure the ``outer_list_rows_mosaic`` template key:
  10. .. code-block:: xml
  11. <service id="sonata.media.admin.media" class="%sonata.media.admin.media.class%">
  12. <tag name="sonata.admin" manager_type="orm" group="sonata_media" label_catalogue="%sonata.media.admin.media.translation_domain%" label="media" label_translator_strategy="sonata.admin.label.strategy.underscore"/>
  13. <argument />
  14. <argument>%sonata.media.admin.media.entity%</argument>
  15. <argument>%sonata.media.admin.media.controller%</argument>
  16. <call method="setTemplates">
  17. <argument type="collection">
  18. <argument key="outer_list_rows_mosaic">SonataMediaBundle:MediaAdmin:list_outer_rows_mosaic.html.twig</argument>
  19. </argument>
  20. </call>
  21. 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.
  22. .. code-block:: jinja
  23. {% extends 'SonataAdminBundle:CRUD:list_outer_rows_mosaic.html.twig' %}
  24. {% block sonata_mosaic_background %}{{ meta.image }}{% endblock %}
  25. {% block sonata_mosaic_default_view %}
  26. <span class="label label-primary pull-right">{{ object.providerName|trans({}, 'SonataMediaBundle') }}</span>
  27. {% endblock %}
  28. {% block sonata_mosaic_hover_view %}
  29. <span class="label label-primary pull-right">{{ object.providerName|trans({}, 'SonataMediaBundle') }}</span>
  30. {% if object.width %} {{ object.width }}{% if object.height %}x{{ object.height }}{% endif %}px{% endif %}
  31. {% if object.length > 0 %}
  32. ({{ object.length }})
  33. {% endif %}
  34. <br />
  35. {% if object.authorname is not empty %}
  36. {{ object.authorname }}
  37. {% endif %}
  38. {% if object.copyright is not empty and object.authorname is not empty %}
  39. ~
  40. {% endif %}
  41. {% if object.copyright is not empty %}
  42. &copy; {{ object.copyright }}
  43. {% endif %}
  44. {% endblock %}
  45. {% block sonata_mosaic_description %}
  46. {% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
  47. <a href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
  48. {% elseif admin.isGranted('SHOW', object) and admin.hasRoute('show') %}
  49. <a href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
  50. {% else %}
  51. {{ meta.title|truncate(40) }}
  52. {% endif %}
  53. {% endblock %}
  54. Block types:
  55. - ``sonata_mosaic_background``: this block is the background value defined in the ObjectMetadata object.
  56. - ``sonata_mosaic_default_view``: this block is used when the list is displayed.
  57. - ``sonata_mosaic_hover_view``: this block is used when the mouse is over the tile.
  58. - ``sonata_mosaic_description``: this block will be always on screen and should represent the entity's name.
  59. The ``ObjectMetadata`` object is returned by the related admin class, for instance the MediaBundle defines the method as:
  60. .. code-block:: jinja
  61. <?php
  62. class MediaAdmin extends AdminInterfance
  63. {
  64. // [...] others methods
  65. public function getObjectMetadata($object)
  66. {
  67. $provider = $this->pool->getProvider($object->getProviderName());
  68. $url = $provider->generatePublicUrl($object, $provider->getFormatName($object, 'admin'));
  69. return new Metadata($object->getName(), $object->getDescription(), $url);
  70. }
  71. }
  72. The final view will look like:
  73. .. figure:: ../images/list_mosaic_custom.png
  74. :align: center
  75. :alt: Customize view
  76. :width: 700px