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



use App\Entity\PointSale; use App\Entity\PointSale;
use App\Entity\ProductCategory; use App\Entity\ProductCategory;
use App\Entity\ProductFamily;
use Doctrine\DBAL\Types\TextType; use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController; use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType; 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\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;


{ {
protected $security; protected $security;
protected $userManager; protected $userManager;
protected $em ;


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


public function showAction() public function showAction()
) )
); );
} }

// @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; return $formBuilder;
} }
} }

Loading…
Cancel
Save