From 996446432af850c01edc81fc26212efbc40479a7 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 20 Apr 2019 20:09:24 -0300 Subject: [PATCH] Add "block.admin_preview" block --- docs/reference/dashboard.rst | 27 ++++ src/Block/AdminPreviewBlockService.php | 135 ++++++++++++++++++ src/Resources/config/block.php | 9 ++ .../translations/SonataAdminBundle.ar.xliff | 4 + .../translations/SonataAdminBundle.bg.xliff | 4 + .../translations/SonataAdminBundle.ca.xliff | 4 + .../translations/SonataAdminBundle.cs.xliff | 4 + .../translations/SonataAdminBundle.de.xliff | 4 + .../translations/SonataAdminBundle.en.xliff | 4 + .../translations/SonataAdminBundle.es.xliff | 4 + .../translations/SonataAdminBundle.eu.xliff | 4 + .../translations/SonataAdminBundle.fa.xliff | 4 + .../translations/SonataAdminBundle.fr.xliff | 4 + .../translations/SonataAdminBundle.hr.xliff | 4 + .../translations/SonataAdminBundle.hu.xliff | 4 + .../translations/SonataAdminBundle.it.xliff | 4 + .../translations/SonataAdminBundle.ja.xliff | 4 + .../translations/SonataAdminBundle.lb.xliff | 4 + .../translations/SonataAdminBundle.lt.xliff | 4 + .../translations/SonataAdminBundle.lv.xliff | 4 + .../translations/SonataAdminBundle.nl.xliff | 4 + .../translations/SonataAdminBundle.no.xliff | 4 + .../translations/SonataAdminBundle.pl.xliff | 4 + .../translations/SonataAdminBundle.pt.xliff | 4 + .../SonataAdminBundle.pt_BR.xliff | 4 + .../translations/SonataAdminBundle.ro.xliff | 4 + .../translations/SonataAdminBundle.ru.xliff | 4 + .../translations/SonataAdminBundle.sk.xliff | 4 + .../translations/SonataAdminBundle.sl.xliff | 4 + .../SonataAdminBundle.sv_SE.xliff | 4 + .../translations/SonataAdminBundle.tr.xliff | 4 + .../translations/SonataAdminBundle.uk.xliff | 4 + .../SonataAdminBundle.zh_CN.xliff | 4 + .../SonataAdminBundle.zh_HK.xliff | 4 + .../views/Block/block_admin_preview.html.twig | 88 ++++++++++++ tests/Block/AdminPreviewBlockServiceTest.php | 87 +++++++++++ 36 files changed, 470 insertions(+) create mode 100644 src/Block/AdminPreviewBlockService.php create mode 100644 src/Resources/views/Block/block_admin_preview.html.twig create mode 100644 tests/Block/AdminPreviewBlockServiceTest.php diff --git a/docs/reference/dashboard.rst b/docs/reference/dashboard.rst index 5e3ecbedfe7..7a25730e38b 100644 --- a/docs/reference/dashboard.rst +++ b/docs/reference/dashboard.rst @@ -347,6 +347,33 @@ which could also have a pluralized translation target: {count, plural, =0 {results} one {result} other {results}} +Preview Block +~~~~~~~~~~~~~ + +A preview block can be used to display a brief of an admin list. + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/sonata_admin.yaml + + sonata_admin: + dashboard: + blocks: + - + position: top # zone in the dashboard + type: sonata.admin.block.admin_preview # block id + settings: + code: sonata.page.admin.page # admin code - service id + icon: fa-magic # font awesome icon + limit: 10 + text: Latest Edited Pages + filters: # filter values + edited: { value: 1 } + _sort_by: updatedAt + _sort_order: DESC + Dashboard Layout ~~~~~~~~~~~~~~~~ diff --git a/src/Block/AdminPreviewBlockService.php b/src/Block/AdminPreviewBlockService.php new file mode 100644 index 00000000000..7123580f819 --- /dev/null +++ b/src/Block/AdminPreviewBlockService.php @@ -0,0 +1,135 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Block; + +use Sonata\AdminBundle\Admin\AdminInterface; +use Sonata\AdminBundle\Admin\Pool; +use Sonata\AdminBundle\Datagrid\ListMapper; +use Sonata\BlockBundle\Block\BlockContextInterface; +use Sonata\BlockBundle\Block\Service\AbstractBlockService; +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Twig\Environment; + +/** + * @author Javier Spagnoletti + */ +final class AdminPreviewBlockService extends AbstractBlockService +{ + /** + * @var Pool + */ + private $pool; + + public function __construct(Environment $twig, Pool $pool) + { + parent::__construct($twig); + + $this->pool = $pool; + } + + public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response + { + $admin = $this->getAdmin($blockContext->getSetting('code')); + $this->handleFilters($admin, $blockContext); + + foreach ($blockContext->getSetting('remove_list_fields') as $listField) { + $admin->getList()->remove($listField); + } + + $datagrid = $admin->getDatagrid(); + + return $this->renderPrivateResponse($blockContext->getTemplate(), [ + 'block' => $blockContext->getBlock(), + 'settings' => $blockContext->getSettings(), + 'admin' => $admin, + 'datagrid' => $datagrid, + ], $response); + } + + public function getName() + { + return 'Admin preview'; + } + + public function configureSettings(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'text' => 'Preview', + 'filters' => [], + 'icon' => false, + 'limit' => 10, + 'code' => false, + 'template' => '@SonataAdmin/Block/block_admin_preview.html.twig', + 'remove_list_fields' => [ListMapper::NAME_ACTIONS], + ]); + } + + /** + * @throws \InvalidArgumentException if the provided admin code is invalid + */ + private function getAdmin(string $code): AdminInterface + { + try { + $admin = $this->pool->getAdminByAdminCode($code); + } catch (ServiceNotFoundException $e) { + throw new \InvalidArgumentException('Unable to find the Admin instance.', $e->getCode(), $e); + } + + if (!$admin instanceof AdminInterface) { + throw new \InvalidArgumentException('The requested service is not an Admin instance.'); + } + + $admin->checkAccess('list'); + + return $admin; + } + + /** + * Maps the block filters to standard admin filters. + */ + private function handleFilters(AdminInterface $admin, BlockContextInterface $blockContext): void + { + $filters = $blockContext->getSetting('filters'); + + if ($sortBy = $filters['_sort_by'] ?? null) { + $sortFilters = ['_sort_by' => $sortBy]; + if ($sortOrder = $filters['_sort_order'] ?? null) { + $sortFilters['_sort_order'] = $sortOrder; + unset($filters['_sort_order']); + } + // Setting a request to the admin is a workaround since the admin only + // can handle the "_sort_by" parameter from the query string. + $request = new Request(['filter' => $sortFilters]); + $request->setSession(new Session()); + $admin->setRequest($request); + unset($filters['_sort_by'], $request); + } + + if (!isset($filters['_per_page'])) { + $filters['_per_page'] = ['value' => $blockContext->getSetting('limit')]; + } + + $datagrid = $admin->getDatagrid(); + + foreach ($filters as $name => $data) { + $datagrid->setValue($name, $data['type'] ?? null, $data['value']); + } + + $datagrid->buildPager(); + } +} diff --git a/src/Resources/config/block.php b/src/Resources/config/block.php index 16c51f08ac8..4c3957a454a 100644 --- a/src/Resources/config/block.php +++ b/src/Resources/config/block.php @@ -12,6 +12,7 @@ */ use Sonata\AdminBundle\Block\AdminListBlockService; +use Sonata\AdminBundle\Block\AdminPreviewBlockService; use Sonata\AdminBundle\Block\AdminSearchBlockService; use Sonata\AdminBundle\Block\AdminStatsBlockService; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; @@ -42,6 +43,14 @@ ]) ->set('sonata.admin.block.stats', AdminStatsBlockService::class) + ->public() + ->tag('sonata.block') + ->args([ + new ReferenceConfigurator('twig'), + new ReferenceConfigurator('sonata.admin.pool'), + ]) + + ->set('sonata.admin.block.admin_preview', AdminPreviewBlockService::class) ->public() ->tag('sonata.block') ->args([ diff --git a/src/Resources/translations/SonataAdminBundle.ar.xliff b/src/Resources/translations/SonataAdminBundle.ar.xliff index f68f6e03fa3..b4566f504bb 100644 --- a/src/Resources/translations/SonataAdminBundle.ar.xliff +++ b/src/Resources/translations/SonataAdminBundle.ar.xliff @@ -466,6 +466,10 @@ stats_view_more عرض المزيد + + preview_view_more + stats_view_more + title_select_subclass حدد نوع الكائن diff --git a/src/Resources/translations/SonataAdminBundle.bg.xliff b/src/Resources/translations/SonataAdminBundle.bg.xliff index 5e496e85d7a..ae8377f6852 100644 --- a/src/Resources/translations/SonataAdminBundle.bg.xliff +++ b/src/Resources/translations/SonataAdminBundle.bg.xliff @@ -466,6 +466,10 @@ stats_view_more Показване на още + + preview_view_more + Показване на още + title_select_subclass Избор на тип обект diff --git a/src/Resources/translations/SonataAdminBundle.ca.xliff b/src/Resources/translations/SonataAdminBundle.ca.xliff index d8e945caa67..23c13ef6455 100644 --- a/src/Resources/translations/SonataAdminBundle.ca.xliff +++ b/src/Resources/translations/SonataAdminBundle.ca.xliff @@ -466,6 +466,10 @@ stats_view_more Veure més + + preview_view_more + Veure més + title_select_subclass Escull el tipus d'element diff --git a/src/Resources/translations/SonataAdminBundle.cs.xliff b/src/Resources/translations/SonataAdminBundle.cs.xliff index 43f0e8c43f5..4cfc48e54d5 100644 --- a/src/Resources/translations/SonataAdminBundle.cs.xliff +++ b/src/Resources/translations/SonataAdminBundle.cs.xliff @@ -466,6 +466,10 @@ stats_view_more Zobrazit více + + preview_view_more + Zobrazit více + title_select_subclass Vyberte typ objektu diff --git a/src/Resources/translations/SonataAdminBundle.de.xliff b/src/Resources/translations/SonataAdminBundle.de.xliff index 8f4e59d958d..4a5b947d39f 100644 --- a/src/Resources/translations/SonataAdminBundle.de.xliff +++ b/src/Resources/translations/SonataAdminBundle.de.xliff @@ -466,6 +466,10 @@ stats_view_more mehr + + preview_view_more + mehr + title_select_subclass Typ auswählen diff --git a/src/Resources/translations/SonataAdminBundle.en.xliff b/src/Resources/translations/SonataAdminBundle.en.xliff index 1f16790cd67..6d94c840c3b 100644 --- a/src/Resources/translations/SonataAdminBundle.en.xliff +++ b/src/Resources/translations/SonataAdminBundle.en.xliff @@ -466,6 +466,10 @@ stats_view_more View more + + preview_view_more + View more + title_select_subclass Select object type diff --git a/src/Resources/translations/SonataAdminBundle.es.xliff b/src/Resources/translations/SonataAdminBundle.es.xliff index 84d031e3482..905cc514846 100644 --- a/src/Resources/translations/SonataAdminBundle.es.xliff +++ b/src/Resources/translations/SonataAdminBundle.es.xliff @@ -466,6 +466,10 @@ stats_view_more Ver más + + preview_view_more + Ver más + title_select_subclass Selecciona el tipo de elemento diff --git a/src/Resources/translations/SonataAdminBundle.eu.xliff b/src/Resources/translations/SonataAdminBundle.eu.xliff index 5be4b6fcea8..234b0e55520 100644 --- a/src/Resources/translations/SonataAdminBundle.eu.xliff +++ b/src/Resources/translations/SonataAdminBundle.eu.xliff @@ -466,6 +466,10 @@ stats_view_more Ikusi gehiago + + preview_view_more + stats_view_more + title_select_subclass Aukeratu objektu mota diff --git a/src/Resources/translations/SonataAdminBundle.fa.xliff b/src/Resources/translations/SonataAdminBundle.fa.xliff index 8078f2edbe8..4e1dc201179 100644 --- a/src/Resources/translations/SonataAdminBundle.fa.xliff +++ b/src/Resources/translations/SonataAdminBundle.fa.xliff @@ -466,6 +466,10 @@ stats_view_more مشاهده بیشتر + + preview_view_more + مشاهده بیشتر + title_select_subclass انتخاب نوع شی diff --git a/src/Resources/translations/SonataAdminBundle.fr.xliff b/src/Resources/translations/SonataAdminBundle.fr.xliff index 972e8cc9f57..24f84dd0ff7 100644 --- a/src/Resources/translations/SonataAdminBundle.fr.xliff +++ b/src/Resources/translations/SonataAdminBundle.fr.xliff @@ -466,6 +466,10 @@ stats_view_more Voir plus + + preview_view_more + Voir plus + title_select_subclass Sélectionnez le type d'objet diff --git a/src/Resources/translations/SonataAdminBundle.hr.xliff b/src/Resources/translations/SonataAdminBundle.hr.xliff index 24ae5f4fc98..e5f24b9f04c 100644 --- a/src/Resources/translations/SonataAdminBundle.hr.xliff +++ b/src/Resources/translations/SonataAdminBundle.hr.xliff @@ -466,6 +466,10 @@ stats_view_more Vidi više + + preview_view_more + stats_view_more + title_select_subclass Odaberite vrstu objekta diff --git a/src/Resources/translations/SonataAdminBundle.hu.xliff b/src/Resources/translations/SonataAdminBundle.hu.xliff index ab3b818b19b..e567cc11cf8 100644 --- a/src/Resources/translations/SonataAdminBundle.hu.xliff +++ b/src/Resources/translations/SonataAdminBundle.hu.xliff @@ -466,6 +466,10 @@ stats_view_more Több megjelenítése + + preview_view_more + Több megjelenítése + title_select_subclass Objektum típus kiválasztása diff --git a/src/Resources/translations/SonataAdminBundle.it.xliff b/src/Resources/translations/SonataAdminBundle.it.xliff index 337c8753b39..91f706a475f 100644 --- a/src/Resources/translations/SonataAdminBundle.it.xliff +++ b/src/Resources/translations/SonataAdminBundle.it.xliff @@ -466,6 +466,10 @@ stats_view_more Mostra dettagli + + preview_view_more + Mostra dettagli + title_select_subclass Seleziona il tipo diff --git a/src/Resources/translations/SonataAdminBundle.ja.xliff b/src/Resources/translations/SonataAdminBundle.ja.xliff index c54f7ccf84b..e88d5a90fb1 100644 --- a/src/Resources/translations/SonataAdminBundle.ja.xliff +++ b/src/Resources/translations/SonataAdminBundle.ja.xliff @@ -466,6 +466,10 @@ stats_view_more 詳細を表示 + + preview_view_more + 詳細を表示 + title_select_subclass オブジェクトの種類を選択 diff --git a/src/Resources/translations/SonataAdminBundle.lb.xliff b/src/Resources/translations/SonataAdminBundle.lb.xliff index 4e10fc4b140..7871d0206a3 100644 --- a/src/Resources/translations/SonataAdminBundle.lb.xliff +++ b/src/Resources/translations/SonataAdminBundle.lb.xliff @@ -466,6 +466,10 @@ stats_view_more Méi + + preview_view_more + Méi + title_select_subclass Typ auswielen diff --git a/src/Resources/translations/SonataAdminBundle.lt.xliff b/src/Resources/translations/SonataAdminBundle.lt.xliff index f2a14d2b2ef..cc9c8c91e31 100644 --- a/src/Resources/translations/SonataAdminBundle.lt.xliff +++ b/src/Resources/translations/SonataAdminBundle.lt.xliff @@ -466,6 +466,10 @@ stats_view_more Peržiūrėti daugiau + + preview_view_more + stats_view_more + title_select_subclass Pasirinkite objekto tipą diff --git a/src/Resources/translations/SonataAdminBundle.lv.xliff b/src/Resources/translations/SonataAdminBundle.lv.xliff index f1666628dc7..0ae8cd51983 100644 --- a/src/Resources/translations/SonataAdminBundle.lv.xliff +++ b/src/Resources/translations/SonataAdminBundle.lv.xliff @@ -468,6 +468,10 @@ stats_view_more Skatīt vēl + + preview_view_more + Skatīt vēl + title_select_subclass Izvēlieties objekta veidu diff --git a/src/Resources/translations/SonataAdminBundle.nl.xliff b/src/Resources/translations/SonataAdminBundle.nl.xliff index 5e208b1d38a..b428c027c5c 100644 --- a/src/Resources/translations/SonataAdminBundle.nl.xliff +++ b/src/Resources/translations/SonataAdminBundle.nl.xliff @@ -466,6 +466,10 @@ stats_view_more Bekijk meer + + preview_view_more + Bekijk meer + title_select_subclass Selecteer object type diff --git a/src/Resources/translations/SonataAdminBundle.no.xliff b/src/Resources/translations/SonataAdminBundle.no.xliff index 777030a37c6..62e5ecafff0 100644 --- a/src/Resources/translations/SonataAdminBundle.no.xliff +++ b/src/Resources/translations/SonataAdminBundle.no.xliff @@ -467,6 +467,10 @@ stats_view_more Se mer + + preview_view_more + stats_view_more + title_select_subclass Velg objekttype diff --git a/src/Resources/translations/SonataAdminBundle.pl.xliff b/src/Resources/translations/SonataAdminBundle.pl.xliff index 9a7b0eb92bc..f58a35ad147 100644 --- a/src/Resources/translations/SonataAdminBundle.pl.xliff +++ b/src/Resources/translations/SonataAdminBundle.pl.xliff @@ -466,6 +466,10 @@ stats_view_more Zobacz więcej + + preview_view_more + Zobacz więcej + title_select_subclass Wybierz podtyp obiektu diff --git a/src/Resources/translations/SonataAdminBundle.pt.xliff b/src/Resources/translations/SonataAdminBundle.pt.xliff index 8fd4a429e65..69e0b856743 100644 --- a/src/Resources/translations/SonataAdminBundle.pt.xliff +++ b/src/Resources/translations/SonataAdminBundle.pt.xliff @@ -467,6 +467,10 @@ stats_view_more Veja mais + + preview_view_more + stats_view_more + title_select_subclass Selecionar o tipo de objeto diff --git a/src/Resources/translations/SonataAdminBundle.pt_BR.xliff b/src/Resources/translations/SonataAdminBundle.pt_BR.xliff index 12f8460011b..bcf69c92078 100644 --- a/src/Resources/translations/SonataAdminBundle.pt_BR.xliff +++ b/src/Resources/translations/SonataAdminBundle.pt_BR.xliff @@ -466,6 +466,10 @@ stats_view_more Ver mais + + preview_view_more + Ver mais + title_select_subclass Selecione o tipo do objeto diff --git a/src/Resources/translations/SonataAdminBundle.ro.xliff b/src/Resources/translations/SonataAdminBundle.ro.xliff index 914005b4967..21b99fa6177 100644 --- a/src/Resources/translations/SonataAdminBundle.ro.xliff +++ b/src/Resources/translations/SonataAdminBundle.ro.xliff @@ -466,6 +466,10 @@ stats_view_more Vezi mai mult + + preview_view_more + stats_view_more + title_select_subclass Selectați tipul obiectului diff --git a/src/Resources/translations/SonataAdminBundle.ru.xliff b/src/Resources/translations/SonataAdminBundle.ru.xliff index eb488901b05..2059fb3c996 100644 --- a/src/Resources/translations/SonataAdminBundle.ru.xliff +++ b/src/Resources/translations/SonataAdminBundle.ru.xliff @@ -466,6 +466,10 @@ stats_view_more Показать еще + + preview_view_more + Показать еще + title_select_subclass Выберите тип объекта diff --git a/src/Resources/translations/SonataAdminBundle.sk.xliff b/src/Resources/translations/SonataAdminBundle.sk.xliff index 1ff4650bf7a..5f9af951466 100644 --- a/src/Resources/translations/SonataAdminBundle.sk.xliff +++ b/src/Resources/translations/SonataAdminBundle.sk.xliff @@ -466,6 +466,10 @@ stats_view_more Zobraziť viac + + preview_view_more + Zobraziť viac + title_select_subclass Vyberte typ objektu diff --git a/src/Resources/translations/SonataAdminBundle.sl.xliff b/src/Resources/translations/SonataAdminBundle.sl.xliff index fc014fe5eec..3fb9ef69343 100644 --- a/src/Resources/translations/SonataAdminBundle.sl.xliff +++ b/src/Resources/translations/SonataAdminBundle.sl.xliff @@ -466,6 +466,10 @@ stats_view_more Poglej več + + preview_view_more + Poglej več + title_select_subclass Izberi tip objekta diff --git a/src/Resources/translations/SonataAdminBundle.sv_SE.xliff b/src/Resources/translations/SonataAdminBundle.sv_SE.xliff index 6acc509fbef..40ac961db1c 100644 --- a/src/Resources/translations/SonataAdminBundle.sv_SE.xliff +++ b/src/Resources/translations/SonataAdminBundle.sv_SE.xliff @@ -466,6 +466,10 @@ stats_view_more Visa mer + + preview_view_more + stats_view_more + title_select_subclass Välj objekttyp diff --git a/src/Resources/translations/SonataAdminBundle.tr.xliff b/src/Resources/translations/SonataAdminBundle.tr.xliff index a55ab8168a0..c2579c14487 100644 --- a/src/Resources/translations/SonataAdminBundle.tr.xliff +++ b/src/Resources/translations/SonataAdminBundle.tr.xliff @@ -466,6 +466,10 @@ stats_view_more Devamını göster + + preview_view_more + Devamını göster + title_select_subclass Nesne türünü seç diff --git a/src/Resources/translations/SonataAdminBundle.uk.xliff b/src/Resources/translations/SonataAdminBundle.uk.xliff index 7ccd8095391..36420ca1c16 100644 --- a/src/Resources/translations/SonataAdminBundle.uk.xliff +++ b/src/Resources/translations/SonataAdminBundle.uk.xliff @@ -466,6 +466,10 @@ stats_view_more Показати ще + + preview_view_more + Показати ще + title_select_subclass Оберіть тип об'єкта diff --git a/src/Resources/translations/SonataAdminBundle.zh_CN.xliff b/src/Resources/translations/SonataAdminBundle.zh_CN.xliff index 94402e3f26a..eaf4e882f81 100644 --- a/src/Resources/translations/SonataAdminBundle.zh_CN.xliff +++ b/src/Resources/translations/SonataAdminBundle.zh_CN.xliff @@ -466,6 +466,10 @@ stats_view_more 查看更多 + + preview_view_more + stats_view_more + title_select_subclass 选择对象类型 diff --git a/src/Resources/translations/SonataAdminBundle.zh_HK.xliff b/src/Resources/translations/SonataAdminBundle.zh_HK.xliff index 29f5fcba7c9..38522da971f 100644 --- a/src/Resources/translations/SonataAdminBundle.zh_HK.xliff +++ b/src/Resources/translations/SonataAdminBundle.zh_HK.xliff @@ -466,6 +466,10 @@ stats_view_more 顯示更多 + + preview_view_more + 顯示更多 + title_select_subclass 選擇物件類別 diff --git a/src/Resources/views/Block/block_admin_preview.html.twig b/src/Resources/views/Block/block_admin_preview.html.twig new file mode 100644 index 00000000000..a04899cce37 --- /dev/null +++ b/src/Resources/views/Block/block_admin_preview.html.twig @@ -0,0 +1,88 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends sonata_block.templates.block_base %} + +{% set translation_domain = settings.translation_domain ?? admin.translationDomain %} + +{% block block %} + {% set inlineAnchor = settings.text|replace({'.':'_'}) %} + {% set results_count = admin.datagrid.results|length %} +
+
+ {% set icon = settings.icon|default('') %} + {% if icon %} + + {% endif %} +

+ {{ settings.text|trans({}, translation_domain) }} +

+
+
+ {{ sonata_block_render_event('sonata.admin.list.table.top', { 'admin': admin }) }} + + {% block list_header %}{% endblock %} + + {% if results_count > 0 %} + + {% block table_header %} + + + {% for field_description in admin.list.elements %} + {% if field_description.option('code') == '_select' %} + {# NEXT_MAJOR: Replace the previous condition with the following one #} + {#{% if field_description.option('code') == constant('Sonata\\AdminBundle\\Datagrid\\ListMapper::NAME_SELECT') %}#} + + {% else %} + {% filter spaceless %} + + {% endfilter %} + {% endif %} + {% endfor %} + + + {% endblock %} + + {% block table_body %} + + {% include get_admin_template('outer_list_rows_' ~ admin.getListMode(), admin.code) %} + + {% endblock %} + + {% block table_footer %} + {% endblock %} +
+ {% if field_description.option('label_icon') %} + + {% endif %} + {{ field_description.label|trans({}, field_description.translationDomain) }} +
+ + {% else %} + {% block no_result_content %} +
+
+ {{ 'no_result'|trans({}, 'SonataAdminBundle') }} +
+
+ {% endblock %} + {% endif %} + + {{ sonata_block_render_event('sonata.admin.list.table.bottom', { 'admin': admin }) }} +
+
+{% endblock %} diff --git a/tests/Block/AdminPreviewBlockServiceTest.php b/tests/Block/AdminPreviewBlockServiceTest.php new file mode 100644 index 00000000000..827a25fafce --- /dev/null +++ b/tests/Block/AdminPreviewBlockServiceTest.php @@ -0,0 +1,87 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Block; + +use Sonata\AdminBundle\Admin\AdminInterface; +use Sonata\AdminBundle\Admin\FieldDescriptionCollection; +use Sonata\AdminBundle\Admin\Pool; +use Sonata\AdminBundle\Block\AdminPreviewBlockService; +use Sonata\AdminBundle\Datagrid\Datagrid; +use Sonata\BlockBundle\Test\BlockServiceTestCase; +use Symfony\Component\DependencyInjection\Container; +use Twig\Environment; + +/** + * @author Javier Spagnoletti + */ +final class AdminPreviewBlockServiceTest extends BlockServiceTestCase +{ + /** + * @var Pool + */ + private $pool; + + protected function setUp(): void + { + parent::setUp(); + + $this->pool = new Pool(new Container()); + } + + public function testDefaultSettings(): void + { + $blockService = new AdminPreviewBlockService($this->createMock(Environment::class), $this->pool); + $blockContext = $this->getBlockContext($blockService); + + $this->assertSettings([ + 'text' => 'Preview', + 'filters' => [], + 'icon' => false, + 'limit' => 10, + 'code' => false, + 'template' => '@SonataAdmin/Block/block_admin_preview.html.twig', + 'remove_list_fields' => ['_action'], + ], $blockContext); + } + + public function testBlockExecution(): void + { + $adminCode = 'admin.bar'; + $responseContent = '
AdminBlockPreview
'; + + $admin = $this->createMock(AdminInterface::class); + $admin + ->method('getCode') + ->willReturn($adminCode); + + $container = new Container(); + $container->set($adminCode, $admin); + $pool = new Pool($container, [$adminCode]); + $datagrid = $this->createStub(Datagrid::class); + $twig = $this->createMock(Environment::class); + + $blockService = new AdminPreviewBlockService($twig, $pool); + $blockContext = $this->getBlockContext($blockService)->setSetting('code', 'admin.bar'); + + $admin->expects(self::once())->method('checkAccess')->with('list')->willReturn(true); + $admin->expects(self::exactly(2))->method('getDatagrid')->willReturn($datagrid); + $admin->expects(self::once())->method('getList')->willReturn(new FieldDescriptionCollection()); + $twig->expects(self::once())->method('render')->willReturn($responseContent); + + $response = $blockService->execute($blockContext); + + static::assertSame($responseContent, $response->getContent()); + static::assertSame(200, $response->getStatusCode()); + } +}