Skip to content

Instantly share code, notes, and snippets.

@leftdevel
Created May 20, 2012 18:16
Show Gist options
  • Save leftdevel/2759012 to your computer and use it in GitHub Desktop.
Save leftdevel/2759012 to your computer and use it in GitHub Desktop.
<?php
namespace NicaStaff\IQAHBundle\Form\EventListener;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
use NicaStaff\IQCoreBundle\Entity;
class AddPositionLabelSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
// Tells the dispatcher that we want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(DataEvent $event)
{
$form = $event->getForm();
$data = $event->getData(); // This is returning null
if (!$data instanceof Entity\DisplayableReportField) {
throw new \Exception(
'AddPositionLabelSubscriber::data must be an instance of Entity\DisplayableReportField'
);
} elseif (!$data->getField() instanceof Entity\Field) {
throw new \Exception(
'AddPositionLabelSubscriber::data::Field must be an instance of Entity\Field'
);
}
$field = $data->getField();
$form->add($this->factory->createNamed('text', 'position', null, array('label' => $field->getName())));
}
}
<?php
namespace NicaStaff\IQAHBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use NicaStaff\IQCoreBundle\Repo\FieldRepo;
use NicaStaff\IQCoreBundle\Entity;
use NicaStaff\IQAHBundle\Form\EventListener\AddPositionLabelSubscriber;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormEvents;
class DisplayableReportFieldType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
switch ($options['mode']) {
case 'step2':
$subscriber = new AddPositionLabelSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);
/*
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use ($builder)
{
$form = $event->getForm();
$data = $event->getData();
// Check we're looking at the right data/form
if ($data instanceof Entity\DisplayableReportField)
{
$label = $data->getField()->getName();
$form->add($builder->getFormFactory()->createNamed(
'text',
'position',
null,
array('label' => $label)
));
}
});
$builder->add('position'); // this is required in order to show 'position' widget properly
*/
break;
case 'step3':
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use ($builder)
{
$form = $event->getForm();
$data = $event->getData();
/* Check we're looking at the right data/form */
if ($data instanceof Entity\DisplayableReportField)
{
$label = $data->getField()->getName();
$form->add($builder->getFormFactory()->createNamed(
'text',
'label',
null,
array('label' => $label)
));
}
});
$builder->add('label'); // this is required in order to show 'label' widget properly
}
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'NicaStaff\IQCoreBundle\Entity\DisplayableReportField',
'validation_groups' => array('default'),
'table' => null,
'mode' => 'default',
);
}
public function getName()
{
return 'nicastaff_iqahbundle_displayreportfieldtype';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment