Browse Source

Filtre sur les select des formulaires liés à des entités (currentMerchant)

reduction
Guillaume 4 years ago
parent
commit
03e496f781
1 changed files with 38 additions and 1 deletions
  1. +38
    -1
      ShopBundle/Controller/Admin/AdminController.php

+ 38
- 1
ShopBundle/Controller/Admin/AdminController.php View File

@@ -4,7 +4,9 @@ namespace Lc\ShopBundle\Controller\Admin;

use App\Entity\PointSale;
use App\Entity\ProductCategory;
use App\Entity\ProductFamily;
use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
@@ -26,7 +28,10 @@ use Lc\ShopBundle\Repository\ProductCategoryRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

@@ -34,11 +39,13 @@ class AdminController extends EasyAdminController
{
protected $security;
protected $userManager;
protected $em ;

public function __construct(Security $security, UserManagerInterface $userManager)
public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em)
{
$this->security = $security;
$this->userManager = $userManager;
$this->em = $em ;
}

public function showAction()
@@ -311,6 +318,36 @@ class AdminController extends EasyAdminController
)
);
}

// @TODO : À passer dans le controller de App
$formBuilder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$allChilds = $form->all() ;
foreach($allChilds as $child) {

$type = $child->getConfig()->getType()->getInnerType() ;
if($type instanceof EntityType) {
$attributes = $child->getConfig()->getAttributes() ;
$passedOptions = $attributes['data_collector/passed_options'] ;
$classImplements = class_implements($passedOptions['class']) ;

if(in_array('App\Context\FilterHubInterface', $classImplements)) {
$form->add($child->getName(), EntityType::class, array(
'class' => $this->em->getClassMetadata($passedOptions['class'])->getName(),
'label' => $passedOptions['label'],
'query_builder' => function (EntityRepository $repo) {
return $repo->createQueryBuilder('e')
->where('e.hub = :currentMerchant')
->setParameter(':currentMerchant', $this->getUser()->getMerchant()->getId());
},
'required' => $passedOptions['required'],
)
);
}
}
}
});

return $formBuilder;
}
}

Loading…
Cancel
Save