Skip to content

Commit

Permalink
EZP-28656: Author fieldtype is not filtering out empty rows (ezsystem…
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Dec 21, 2017
1 parent 81f2d2f commit 6018f4a
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib/Form/Type/FieldType/AuthorFieldType.php
Expand Up @@ -13,6 +13,8 @@
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -34,7 +36,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('authors', AuthorCollectionType::class, [])
->addViewTransformer($this->getViewTransformer());
->addViewTransformer($this->getViewTransformer())
->addEventListener(FormEvents::POST_SUBMIT, [$this, 'filterOutEmptyAuthors']);
}

public function configureOptions(OptionsResolver $resolver)
Expand All @@ -56,16 +59,24 @@ public function getViewTransformer(): DataTransformerInterface

return $value;
}, function (Value $value) {
$value->authors->exchangeArray(
array_filter(
$value->authors->getArrayCopy(),
function (Author $author) {
return !empty($author->email);
}
)
);

return $value;
});
}

/**
* @param FormEvent $event
*/
public function filterOutEmptyAuthors(FormEvent $event)
{
$value = $event->getData();

$value->authors->exchangeArray(
array_filter(
$value->authors->getArrayCopy(),
function (Author $author) {
return !empty($author->email) || !empty($author->name);
}
)
);
}
}

0 comments on commit 6018f4a

Please sign in to comment.