From 422acf3565796d49b63d6aa2f238f160b7e57b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20ALFAIATE?= Date: Thu, 20 Apr 2017 11:59:58 +0200 Subject: [PATCH 1/3] Revert #80 --- Form/Type/Select2EntityType.php | 36 --------------------------------- 1 file changed, 36 deletions(-) diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index b338a16..b9d2476 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; @@ -108,41 +107,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'] .= '[]'; } From 2e45b07c881ffb30d475ed005e1890e5835bd27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20ALFAIATE?= Date: Thu, 20 Apr 2017 14:51:33 +0200 Subject: [PATCH 2/3] Fix resubmit for new tagged entities --- .../EntitiesToPropertyTransformer.php | 16 +++++++++++++--- .../EntityToPropertyTransformer.php | 16 +++++++++++++--- Form/Type/Select2EntityType.php | 11 ++++------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Form/DataTransformer/EntitiesToPropertyTransformer.php b/Form/DataTransformer/EntitiesToPropertyTransformer.php index ce28418..860aa15 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..3bd3f1c 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 b9d2476..ea0e20f 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -70,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); From 5bd756c07153b1e1fb9ae2b9d88c2c4821ae1a2a Mon Sep 17 00:00:00 2001 From: Ross Keatinge Date: Fri, 21 Apr 2017 21:15:23 -0400 Subject: [PATCH 3/3] Coding style --- Form/DataTransformer/EntitiesToPropertyTransformer.php | 2 +- Form/DataTransformer/EntityToPropertyTransformer.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Form/DataTransformer/EntitiesToPropertyTransformer.php b/Form/DataTransformer/EntitiesToPropertyTransformer.php index 860aa15..38272f9 100644 --- a/Form/DataTransformer/EntitiesToPropertyTransformer.php +++ b/Form/DataTransformer/EntitiesToPropertyTransformer.php @@ -71,7 +71,7 @@ public function transform($entities) if ($this->em->contains($entity)) { $value = $this->accessor->getValue($entity, $this->primaryKey); } else { - $value = $this->newTagPrefix.$text; + $value = $this->newTagPrefix . $text; $text = $text.$this->newTagText; } diff --git a/Form/DataTransformer/EntityToPropertyTransformer.php b/Form/DataTransformer/EntityToPropertyTransformer.php index 3bd3f1c..41232dd 100644 --- a/Form/DataTransformer/EntityToPropertyTransformer.php +++ b/Form/DataTransformer/EntityToPropertyTransformer.php @@ -70,7 +70,7 @@ public function transform($entity) if ($this->em->contains($entity)) { $value = $this->accessor->getValue($entity, $this->primaryKey); } else { - $value = $this->newTagPrefix.$text; + $value = $this->newTagPrefix . $text; $text = $text.$this->newTagText; }