* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace App\Form\Type; use App\Form\DataTransformer\TagArrayToStringTransformer; use App\Repository\TagRepository; use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; /** * Defines the custom form field type used to manipulate tags values across * Bootstrap-tagsinput javascript plugin. * * See https://symfony.com/doc/current/form/create_custom_field_type.html * * @author Yonel Ceruto */ class TagsInputType extends AbstractType { private $tags; public function __construct(TagRepository $tags) { $this->tags = $tags; } /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder // The Tag collection must be transformed into a comma separated string. // We could create a custom transformer to do Collection <-> string in one step, // but here we're doing the transformation in two steps (Collection <-> array <-> string) // and reuse the existing CollectionToArrayTransformer. ->addModelTransformer(new CollectionToArrayTransformer(), true) ->addModelTransformer(new TagArrayToStringTransformer($this->tags), true) ; } /** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options): void { $view->vars['tags'] = $this->tags->findAll(); } /** * {@inheritdoc} */ public function getParent(): ?string { return TextType::class; } }