diff --git a/Form/DataTransformer/EntitiesToPropertyTransformer.php b/Form/DataTransformer/EntitiesToPropertyTransformer.php index ce28418..38272f9 100644 --- a/Form/DataTransformer/EntitiesToPropertyTransformer.php +++ b/Form/DataTransformer/EntitiesToPropertyTransformer.php @@ -26,6 +26,8 @@ class EntitiesToPropertyTransformer implements DataTransformerInterface protected $primaryKey; /** @var string */ protected $newTaxPrefix; + /** @var string */ + protected $newTagText; /** @var PropertyAccessor */ protected $accessor; @@ -36,13 +38,14 @@ class EntitiesToPropertyTransformer implements DataTransformerInterface * @param string $primaryKey * @param string $newTagPrefix */ - public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__') + public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__', $newTagText = ' (NEW)') { $this->em = $em; $this->className = $class; $this->textProperty = $textProperty; $this->primaryKey = $primaryKey; $this->newTagPrefix = $newTagPrefix; + $this->newTagText = $newTagText; $this->accessor = PropertyAccess::createPropertyAccessor(); } @@ -62,10 +65,17 @@ public function transform($entities) foreach ($entities as $entity) { $text = is_null($this->textProperty) - ? (string)$entity + ? (string) $entity : $this->accessor->getValue($entity, $this->textProperty); - $data[$this->accessor->getValue($entity, $this->primaryKey)] = $text; + if ($this->em->contains($entity)) { + $value = $this->accessor->getValue($entity, $this->primaryKey); + } else { + $value = $this->newTagPrefix . $text; + $text = $text.$this->newTagText; + } + + $data[$value] = $text; } return $data; diff --git a/Form/DataTransformer/EntityToPropertyTransformer.php b/Form/DataTransformer/EntityToPropertyTransformer.php index 0fb63f3..41232dd 100644 --- a/Form/DataTransformer/EntityToPropertyTransformer.php +++ b/Form/DataTransformer/EntityToPropertyTransformer.php @@ -27,6 +27,8 @@ class EntityToPropertyTransformer implements DataTransformerInterface protected $primaryKey; /** @var string */ protected $newTagPrefix; + /** @var string */ + protected $newTagText; /** @var PropertyAccessor */ protected $accessor; @@ -37,13 +39,14 @@ class EntityToPropertyTransformer implements DataTransformerInterface * @param string $primaryKey * @param string $newTagPrefix */ - public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__') + public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__', $newTagText = ' (NEW)') { $this->em = $em; $this->className = $class; $this->textProperty = $textProperty; $this->primaryKey = $primaryKey; $this->newTagPrefix = $newTagPrefix; + $this->newTagText = $newTagText; $this->accessor = PropertyAccess::createPropertyAccessor(); } @@ -61,10 +64,17 @@ public function transform($entity) } $text = is_null($this->textProperty) - ? (string)$entity + ? (string) $entity : $this->accessor->getValue($entity, $this->textProperty); - $data[$this->accessor->getValue($entity, $this->primaryKey)] = $text; + if ($this->em->contains($entity)) { + $value = $this->accessor->getValue($entity, $this->primaryKey); + } else { + $value = $this->newTagPrefix . $text; + $text = $text.$this->newTagText; + } + + $data[$value] = $text; return $data; } diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index b338a16..ea0e20f 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -10,7 +10,6 @@ use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolverInterface; -use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\Routing\RouterInterface; use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer; use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer; @@ -71,15 +70,12 @@ public function buildForm(FormBuilderInterface $builder, array $options) // add the default data transformer } else { - if (isset($options['allow_add']['new_tag_prefix'])) { - $newTagPrefix = $options['allow_add']['new_tag_prefix']; - } else { - $newTagPrefix = $this->config['allow_add']['new_tag_prefix']; - } + $newTagPrefix = isset($options['allow_add']['new_tag_prefix']) ? $options['allow_add']['new_tag_prefix'] : $this->config['allow_add']['new_tag_prefix']; + $newTagText = isset($options['allow_add']['new_tag_text']) ? $options['allow_add']['new_tag_text'] : $this->config['allow_add']['new_tag_text']; $transformer = $options['multiple'] - ? new EntitiesToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix) - : new EntityToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix); + ? new EntitiesToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix, $newTagText) + : new EntityToPropertyTransformer($this->em, $options['class'], $options['text_property'], $options['primary_key'], $newTagPrefix, $newTagText); } $builder->addViewTransformer($transformer, true); @@ -108,41 +104,6 @@ public function finishView(FormView $view, FormInterface $form, array $options) } } - if ($view->vars['allow_add']['enabled'] && $form->isSubmitted()) { - // Form is being displayed again after a submit that failed validation. - // $view->vars['value'] needs to be rebuilt to handle new entities added with a tag. - - // get entities as iterable collection or array. - $entities = $options['multiple'] ? $form->getData() : [$form->getData()]; - - $accessor = PropertyAccess::createPropertyAccessor(); - $textProperty = isset($options['text_property']) ? $options['text_property'] : null; - $newTagPrefix = $view->vars['allow_add']['new_tag_prefix']; - - $view->vars['value'] = []; - - foreach ($entities as $entity) { - // Get text value - $text = is_null($textProperty) - ? (string) $entity - : $accessor->getValue($entity, $textProperty); - - // Get choice field (primary key). - $choiceFieldValue = $accessor->getValue($entity, $options['primary_key']); - - if (!$this->em->contains($entity)) { - // This is a new entity, added via a tag, not persisted yet. - // A new entity may or may not already have a non-null choice field. - // If the new entity already has a choice field, use it, otherwise use the text field. - $choiceFieldValue = $newTagPrefix . ($choiceFieldValue ? : $text); - - $text .= $view->vars['allow_add']['new_tag_text']; - } - - $view->vars['value'][$choiceFieldValue] = $text; - } - } - if ($options['multiple']) { $view->vars['full_name'] .= '[]'; }