Created
February 20, 2014 18:30
-
-
Save dionisos2/9120177 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace Ukratio\TrouveToutBundle\Form\EventListener; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormFactoryInterface; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\Form\FormEvents; | |
use Doctrine\ORM\EntityManager; | |
use Ukratio\TrouveToutBundle\Entity\Element; | |
class AddOwnerElementSubscriber implements EventSubscriberInterface | |
{ | |
private $factory; | |
public function __construct(FormFactoryInterface $factory) | |
{ | |
$this->factory = $factory; | |
} | |
public static function getSubscribedEvents() | |
{ | |
return array(FormEvents::PRE_SET_DATA => 'preSetData', | |
FormEvents::PRE_BIND => 'preBind'); | |
} | |
public function preBind(FormEvent $event) | |
{ | |
$data = $event->getData(); | |
$form = $event->getForm(); | |
$ownerElements = array(); | |
$index = 0; | |
while (isset($data['element_'.$index])) { | |
$ownerElements['element_'.$index] = $data['element_'.$index]; | |
$index++; | |
} | |
krsort($ownerElements); | |
foreach ($ownerElements as $key => $ownerElement) { | |
$form->add($this->factory->createNamed($key, 'text', $ownerElement, array('mapped' => false, | |
'label' => ' ', | |
'read_only' => true, | |
'auto_initialize' => false))); | |
} | |
} | |
public function preSetData(FormEvent $event) | |
{ | |
$data = $event->getData(); | |
$form = $event->getForm(); | |
if (! $data instanceof Element) { | |
return; | |
} | |
$ownerElements = array_slice($data->getPath(), 1); | |
$ownerElements = array_reverse($ownerElements); | |
krsort($ownerElements); | |
foreach ($ownerElements as $key => $pathElement) { | |
$optionsElement = array('label' => ' ', | |
'read_only' => true, | |
'mapped' => false, | |
'auto_initialize' => false, | |
); | |
$builder = $this->factory->createNamedBuilder("element_$key", 'text', $pathElement, $optionsElement); | |
$form->add($builder->getForm()); | |
} | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Ukratio\TrouveToutBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormView; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Ukratio\TrouveToutBundle\Form\EventListener\AddOwnerElementSubscriber; | |
use Ukratio\TrouveToutBundle\Form\EventListener\AddChildElementSubscriber; | |
use Ukratio\TrouveToutBundle\Form\EventListener\AddElementSubscriber; | |
use Ukratio\TrouveToutBundle\Entity\Element; | |
use Ukratio\TrouveToutBundle\Entity\ElementRepository; | |
use Ukratio\TrouveToutBundle\Entity\Type; | |
use Ukratio\TrouveToutBundle\Entity\ConceptRepository; | |
use Ukratio\TrouveToutBundle\Entity\CaractRepository; | |
use Ukratio\TrouveToutBundle\Service\CaractTypeManager; | |
use Doctrine\ORM\EntityManager; | |
class ElementType extends AbstractType | |
{ | |
protected $conceptRepo; | |
protected $elementRepo; | |
protected $caractTypeManager; | |
public function __construct(ConceptRepository $conceptRepo, ElementRepository $elementRepo, CaractTypeManager $caractTypeManager) | |
{ | |
$this->conceptRepo = $conceptRepo; | |
$this->elementRepo = $elementRepo; | |
$this->caractTypeManager = $caractTypeManager; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$type = Type::getEnumerator($options['typeOfValue']); | |
$builder->addEventSubscriber(new AddElementSubscriber($builder->getFormFactory(), $this->conceptRepo, $this->elementRepo, $type, $this->caractTypeManager)); | |
$builder->addEventSubscriber(new AddChildElementSubscriber($builder->getFormFactory(), $type, $this->caractTypeManager)); | |
$builder->addEventSubscriber(new AddOwnerElementSubscriber($builder->getFormFactory())); | |
} | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Ukratio\TrouveToutBundle\Entity\Element', | |
'constraintOnValue' => false, | |
'typeOfValue' => 'name', | |
)); | |
} | |
public function getName() | |
{ | |
return 'TrouveTout_Element'; | |
} | |
public function buildView(FormView $view, FormInterface $form, array $options) | |
{ | |
} | |
} |
This file contains hidden or 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
{% block TrouveTout_Element_widget %} | |
{% spaceless %} | |
{{ parent() }} | |
<div class="enligne" id="childValueDiv"> | |
{% if form.childValue is defined%} | |
{{ form_row(form.childValue) }} | |
{% endif %} | |
</div> | |
<div class="enligne" id="valueDiv"> | |
{{ form_row(form.value) }} | |
</div> | |
<div class="enligne" id="ownerElementDiv"> | |
{% for owner in ??? %} // C’est ici que je voudrais faire ma boucle | |
{% endfor %} | |
</div> | |
<div class="enligne" id="choosePictureDiv"> | |
{{ form_row(form.choosePicture) }} | |
</div> | |
<div class="enligne" id="restDiv"> | |
{{ form_rest(form) }} | |
</div> | |
{% endspaceless %} | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment