Artisan Développeur

Qualité, amélioration continue, agilité.

Add choices to a Symfony formType with an event listener

If you want to add (or remove) choices from a ChoiceType in your formType, this is how to do it with an event listener.
The event you will subscribe depends on the data you have and your workflow.
I had to modify a formType to allow the edition of an entity with a particular value on one field.
The field is "mode".
"myParticularMode" is a mode not available by default, I use it only under certain conditions, the entity is programmatically created, and the user is redirected to the edit form to finish to fill the data.
I don't want to allow my users to use that mode om new I don't want to allow my users to use that mode on new entity, I need it only when I edit the entities defined with this "mode".

The code take the event "POST_SET_DATA", this way I have my data, and I know the mode.
If the "mode" is myParticularMode" I add it to the choices.

Add a choice to the choices:

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
    // get the form from the event
    $form = $event->getForm();
    if ('myParticularMode' == $form->get('mode')->getData()) {
        // get the field options
        $options = $form->get('mode')->getConfig()->getOptions();
        // add the mode to the choices array
        $options['choices']['MY_PARTICULAR_MODE'] = 'myParticularMode_display_name';
        $form->add('mode', ChoiceType::class, $options);

To replace all the choices and display only the one, don’t take the options, just override the choices array, and you can even disable it:

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
    // get the form from the event
    $form = $event->getForm();
    if ('myParticularMode' == $form->get('mode')->getData()) {
        // add the mode to the choices array
        $options['choices']['MY_PARTICULAR_MODE'] = 'myParticularMode_display_name';
        // disable the field
        $options['disabled'] = true;
        $form->add('mode', ChoiceType::class, $options);

Enjoy 🙂

Drupal 8 Vagrant, Ansible, Composer

Be ready to work on Drupal 8 fast !
I have searched for a simple VM to install Drupal 8 locally to dev, with composer installed. All the projects I have seen seems great but with to many options and possibilities.

My goal was to have, a virtual machine, with Debian 10 (Buster), php, apache and mariadb (default distribution version), composer, and a Drupal 8 installed with composer.

I did not find what I need, so I have tinkered something, it is not the coolest Ansible set you have seen, but it does the job 🙂

Don’t hesitate to raise issues, or contact me if you want, and if you find it useful, say hello !

Vagrant snapshot, safety first

When you want to test something, in my case it’s the upgrade to php7.3, I encourage you do use the really simple snapshot function of Vagrant.

First, see the VMs of your system:

vagrant global-status
id       name                        provider   state    directory                                              
0313ed0  default                     virtualbox poweroff /var/www/phpStorm/              
1407963  default                     virtualbox poweroff /var/www/phpStorm/xxx/xxxxx-com/trellis       
3879b77  default                     virtualbox running  /var/www/phpStorm/xxx-dev/xxxxxx-com/trellis   
ac34ac8  default                     virtualbox poweroff /var/www/phpStorm/xxx-dev                              
b292e76  default                     virtualbox running  /var/www/phpStorm/xxxxxxxx-com/trellis           
3fb0c96  default                     virtualbox running  /var/www/phpStorm/vagrant/xxxx                    
6e6b27f  default                     virtualbox running  /var/www/phpStorm/xxxxxx                               
2b7849a  poc-xxxxxx               virtualbox running  /var/www/phpStorm/laravel/projects/poc_xxx       
a8f3ca7  consume-channel-xxxxxx-api virtualbox running  /var/www/phpStorm/laravel/consume_channel_xxxxxx_api 

Some of these VMs are old, and not needed anymore, before I continue, I prefer to do some clean up first.

vagrant global-status --prune
id       name                        provider   state   directory                                              
6e6b27f  default                     virtualbox running /var/www/phpStorm/xxxxx                               
a8f3ca7  consume-channel-xxxxx-api virtualbox running /var/www/phpStorm/laravel/consume_channel_xxxxx_api 

A lot better ! Next the snapshot:

vagrant snapshot save 6e6b27f 2019_08_20_save_xxx_before_php_7_3
==> default: Snapshotting the machine as '2019_08_20_save_xxx_before_php_7_3'... 
==> default: Snapshot saved! You can restore the snapshot at any time by 
==> default: using `vagrant snapshot restore`. You can delete it using                                                                
==> default: `vagrant snapshot delete`.  

Really easy ! Last paranoid check before the upgrade:

vagrant snapshot list 

Now I can break it all and restore my snapshot !

happy experiments