Skip to content

Instantly share code, notes, and snippets.

@baldurrensch
Forked from merk/ApplianceTypeListener.php
Created January 28, 2013 22:30
Show Gist options
  • Select an option

  • Save baldurrensch/4659886 to your computer and use it in GitHub Desktop.

Select an option

Save baldurrensch/4659886 to your computer and use it in GitHub Desktop.

Revisions

  1. @merk merk revised this gist Jan 28, 2013. 1 changed file with 16 additions and 25 deletions.
    41 changes: 16 additions & 25 deletions ApplianceTypeListener.php
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@

    namespace Ibms\AppBundle\Form\EventListener;

    use Doctrine\ORM\EntityRepository;
    use Doctrine\ORM\Query\Expr\From;
    use Doctrine\ORM\QueryBuilder;
    use Ibms\SupplierBundle\Entity\Supplier;
    use Ibms\SupplierBundle\Entity\SupplierManager;
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\FormEvents;
    use Symfony\Component\Form\FormFactoryInterface;
    @@ -15,56 +15,47 @@ class ApplianceTypeListener implements EventSubscriberInterface
    {
    private $factory;
    private $fieldName;
    private $supplierManager;

    public function __construct($fieldName, FormFactoryInterface $factory)
    public function __construct($fieldName, FormFactoryInterface $factory, SupplierManager $supplierManager)
    {
    $this->factory = $factory;
    $this->fieldName = $fieldName;
    $this->factory = $factory;
    $this->fieldName = $fieldName;
    $this->supplierManager = $supplierManager;
    }

    public function preBind(FormEvent $event)
    {
    $form = $event->getForm();
    $data = $event->getData();

    $this->addApplianceType($form, $data['supplier']);
    $qb = $this->supplierManager->queryApplianceTypes();
    $qb->setParameter('supplierId', $data['supplier']);

    $this->addApplianceType($form, $qb);
    }

    public function preSetData(FormEvent $event)
    {
    $form = $event->getForm();
    $data = $event->getData();

    if (null === $data) {
    if (null === $data or null === $data->getSupplier()) {
    return;
    }

    $supplierId = $data->getSupplier() ?
    $data->getSupplier()->getId() :
    null;

    $this->addApplianceType($form, $supplierId);
    $qb = $this->supplierManager->queryApplianceTypes($data->getSupplier());
    $this->addApplianceType($form, $qb);
    }

    protected function addApplianceType(FormInterface $form, $supplierId)
    protected function addApplianceType(FormInterface $form, QueryBuilder $qb)
    {
    $form->add($this->factory->createNamed($this->fieldName, 'entity', null, array(
    'class' => 'Ibms\\SupplierBundle\\Entity\\ApplianceType',
    'empty_value' => 'Pick one',
    'property' => 'type',
    'required' => false,
    'query_builder' => function (EntityRepository $er) use ($supplierId) {
    $qb = $er->createQueryBuilder('a');
    $qb->add('from', new From('Ibms\\SupplierBundle\\Entity\\Supplier', 's'), true)
    ->andWhere('a MEMBER OF s.applianceTypes')
    ->andWhere('s.id = :supplierId');

    $qb->setParameters(array(
    'supplierId' => $supplierId,
    ));

    return $qb;
    }
    'query_builder' => $qb,
    )));
    }

  2. @merk merk created this gist Sep 28, 2012.
    78 changes: 78 additions & 0 deletions ApplianceTypeListener.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    <?php

    namespace Ibms\AppBundle\Form\EventListener;

    use Doctrine\ORM\EntityRepository;
    use Doctrine\ORM\Query\Expr\From;
    use Ibms\SupplierBundle\Entity\Supplier;
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\FormEvents;
    use Symfony\Component\Form\FormFactoryInterface;
    use Symfony\Component\Form\FormInterface;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;

    class ApplianceTypeListener implements EventSubscriberInterface
    {
    private $factory;
    private $fieldName;

    public function __construct($fieldName, FormFactoryInterface $factory)
    {
    $this->factory = $factory;
    $this->fieldName = $fieldName;
    }

    public function preBind(FormEvent $event)
    {
    $form = $event->getForm();
    $data = $event->getData();

    $this->addApplianceType($form, $data['supplier']);
    }

    public function preSetData(FormEvent $event)
    {
    $form = $event->getForm();
    $data = $event->getData();

    if (null === $data) {
    return;
    }

    $supplierId = $data->getSupplier() ?
    $data->getSupplier()->getId() :
    null;

    $this->addApplianceType($form, $supplierId);
    }

    protected function addApplianceType(FormInterface $form, $supplierId)
    {
    $form->add($this->factory->createNamed($this->fieldName, 'entity', null, array(
    'class' => 'Ibms\\SupplierBundle\\Entity\\ApplianceType',
    'empty_value' => 'Pick one',
    'property' => 'type',
    'required' => false,
    'query_builder' => function (EntityRepository $er) use ($supplierId) {
    $qb = $er->createQueryBuilder('a');
    $qb->add('from', new From('Ibms\\SupplierBundle\\Entity\\Supplier', 's'), true)
    ->andWhere('a MEMBER OF s.applianceTypes')
    ->andWhere('s.id = :supplierId');

    $qb->setParameters(array(
    'supplierId' => $supplierId,
    ));

    return $qb;
    }
    )));
    }

    public static function getSubscribedEvents()
    {
    return array(
    FormEvents::PRE_BIND => 'preBind',
    FormEvents::PRE_SET_DATA => 'preSetData'
    );
    }
    }
    12 changes: 12 additions & 0 deletions ConsumingType.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <?php

    class ConsumingType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    // ...

    // Handles adding a dropdown for Type given the forms set data
    $builder->addEventSubscriber(new ApplianceTypeListener('type', $builder->getFormFactory()));
    }
    }