Skip to content

Commit

Permalink
Add "block.admin_preview" block
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Apr 17, 2021
1 parent 3a5b5d3 commit df2999f
Show file tree
Hide file tree
Showing 36 changed files with 458 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/reference/dashboard.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,33 @@ which could also have a pluralized translation target:
<target>{count, plural, =0 {results} one {result} other {results}}</target>
</trans-unit>
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
~~~~~~~~~~~~~~~~

Expand Down
123 changes: 123 additions & 0 deletions src/Block/AdminPreviewBlockService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Twig\Environment;

/**
* @author Javier Spagnoletti <[email protected]>
*/
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
{
$admin = $this->pool->getAdminByAdminCode($code);

$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.
$admin->setRequest(new Request(['filter' => $sortFilters]));
unset($filters['_sort_by']);
}

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();
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.ar.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>عرض المزيد</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>stats_view_more</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>حدد نوع الكائن</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.bg.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Показване на още</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Показване на още</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Избор на тип обект</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.ca.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Veure més</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Veure més</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Escull el tipus d'element</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.cs.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Zobrazit více</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Zobrazit více</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Vyberte typ objektu</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.de.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>mehr</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>mehr</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Typ auswählen</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>View more</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>View more</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Select object type</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.es.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Ver más</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Ver más</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Selecciona el tipo de elemento</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.eu.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Ikusi gehiago</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>stats_view_more</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Aukeratu objektu mota</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.fa.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>مشاهده بیشتر</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>مشاهده بیشتر</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>انتخاب نوع شی</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Voir plus</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Voir plus</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Sélectionnez le type d'objet</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.hr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Vidi više</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>stats_view_more</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Odaberite vrstu objekta</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.hu.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Több megjelenítése</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Több megjelenítése</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Objektum típus kiválasztása</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.it.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Mostra dettagli</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Mostra dettagli</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Seleziona il tipo</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.ja.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>詳細を表示</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>詳細を表示</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>オブジェクトの種類を選択</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.lb.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Méi</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Méi</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Typ auswielen</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.lt.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Peržiūrėti daugiau</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>stats_view_more</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Pasirinkite objekto tipą</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.lv.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@
<source>stats_view_more</source>
<target>Skatīt vēl</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Skatīt vēl</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Izvēlieties objekta veidu</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.nl.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@
<source>stats_view_more</source>
<target>Bekijk meer</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>Bekijk meer</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Selecteer object type</target>
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/translations/SonataAdminBundle.no.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@
<source>stats_view_more</source>
<target>Se mer</target>
</trans-unit>
<trans-unit id="preview_view_more">
<source>preview_view_more</source>
<target>stats_view_more</target>
</trans-unit>
<trans-unit id="title_select_subclass">
<source>title_select_subclass</source>
<target>Velg objekttype</target>
Expand Down
Loading

0 comments on commit df2999f

Please sign in to comment.