vendor/sonata-project/admin-bundle/src/Block/AdminListBlockService.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <[email protected]>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\Block;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Sonata\AdminBundle\Templating\TemplateRegistry;
  14. use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
  15. use Sonata\BlockBundle\Block\BlockContextInterface;
  16. use Sonata\BlockBundle\Block\Service\AbstractBlockService;
  17. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Twig\Environment;
  21. /**
  22.  * @final since sonata-project/admin-bundle 3.52
  23.  *
  24.  * @author Thomas Rabaix <[email protected]>
  25.  */
  26. class AdminListBlockService extends AbstractBlockService
  27. {
  28.     /**
  29.      * @var Pool
  30.      */
  31.     protected $pool;
  32.     /**
  33.      * @var TemplateRegistryInterface
  34.      */
  35.     private $templateRegistry;
  36.     /**
  37.      * NEXT_MAJOR: Remove `$templating` argument.
  38.      *
  39.      * @param Environment|string $twigOrName
  40.      */
  41.     public function __construct(
  42.         $twigOrName,
  43.         ?EngineInterface $templating,
  44.         Pool $pool,
  45.         TemplateRegistryInterface $templateRegistry null
  46.     ) {
  47.         parent::__construct($twigOrName$templating);
  48.         $this->pool $pool;
  49.         $this->templateRegistry $templateRegistry ?: new TemplateRegistry();
  50.     }
  51.     public function execute(BlockContextInterface $blockContextResponse $response null)
  52.     {
  53.         $dashboardGroups $this->pool->getDashboardGroups();
  54.         $settings $blockContext->getSettings();
  55.         $visibleGroups = [];
  56.         foreach ($dashboardGroups as $name => $dashboardGroup) {
  57.             if (!$settings['groups'] || \in_array($name$settings['groups'], true)) {
  58.                 $visibleGroups[] = $dashboardGroup;
  59.             }
  60.         }
  61.         return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [
  62.             'block' => $blockContext->getBlock(),
  63.             'settings' => $settings,
  64.             'admin_pool' => $this->pool,
  65.             'groups' => $visibleGroups,
  66.         ], $response);
  67.     }
  68.     public function getName()
  69.     {
  70.         return 'Admin List';
  71.     }
  72.     public function configureSettings(OptionsResolver $resolver)
  73.     {
  74.         $resolver->setDefaults([
  75.             'groups' => false,
  76.         ]);
  77.         $resolver->setAllowedTypes('groups', ['bool''array']);
  78.     }
  79. }