Skip to content

Commit

Permalink
Merge branch 'Seb33300-resubmit-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
tetranz committed Apr 22, 2017
2 parents c22c34c + 5bd756c commit b69a8d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
16 changes: 13 additions & 3 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class EntitiesToPropertyTransformer implements DataTransformerInterface
protected $primaryKey;
/** @var string */
protected $newTaxPrefix;
/** @var string */
protected $newTagText;
/** @var PropertyAccessor */
protected $accessor;

Expand All @@ -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();
}

Expand All @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class EntityToPropertyTransformer implements DataTransformerInterface
protected $primaryKey;
/** @var string */
protected $newTagPrefix;
/** @var string */
protected $newTagText;
/** @var PropertyAccessor */
protected $accessor;

Expand All @@ -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();
}

Expand All @@ -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;
}
Expand Down
47 changes: 4 additions & 43 deletions Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'] .= '[]';
}
Expand Down

0 comments on commit b69a8d9

Please sign in to comment.