-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?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\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 <[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_pool' => $this->pool, | ||
'admin' => $admin, | ||
'pager' => $datagrid->getPager(), | ||
'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' => ['_action'], | ||
]); | ||
} | ||
|
||
/** | ||
* @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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.