symfonyorocrmorocommerce

OroPlatform: Override core entity form builder


Context

I'm trying to change the form type of one field on one of the core entity: Business Unit

The default form field is TextField and I want to change it to ChoiceType.

Here is my custom field on Business Unit entity created with migration :

$table->addColumn('periodicite', 'string', [
    'oro_options' => [
        'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
        'entity' => ['label' => 'Périodicité'],
    ],
]);

Issue

I've seen on the Oro documentation that entity_config.yml could solve my problem. I've tried to put these lines but it doesn't work :

entity_config:
    business_unit:
          entity:
              items:
                  periodicite:
                      form:
                          type: Symfony\Component\Form\Extension\Core\Type\ChoiceType
                          options:
                            choices:
                              Mensuel: Mensuel
                              Trimestriel: Trimestriel
                            placeholder: false
                            required: true
                            label: "Périodicite"

I have also tried to create a new migration to change the field type on my custom field but it doesn't work

<?php

namespace Baltimore\Bundle\AppBundle\Migrations\Schema\v1_1;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityConfigBundle\Migration\UpdateEntityConfigFieldValueQuery;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
use Oro\Bundle\OrganizationBundle\Entity\BusinessUnit;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class UpdateBusinessUnitField implements Migration, ExtendExtensionAwareInterface
{
    /** @var ExtendExtension */
    protected $extendExtension;

    /**
     * @inheritdoc
     */
    public function setExtendExtension(ExtendExtension $extendExtension)
    {
        $this->extendExtension = $extendExtension;
    }

    public function up(Schema $schema, QueryBag $queries)
    {
        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                BusinessUnit::class,
                'periodicite',
                'form',
                'form_type',
                ChoiceType::class
            )
        );

        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                BusinessUnit::class,
                'periodicite',
                'form',
                'form_options',
                [
                    'choices' => [
                        'Mensuel' => 'Mensuel',
                        'Trimestriel' => 'Trimestriel',
                        'Annuel' => 'Annuel',
                    ],
                ]
            )
        );
    }
}

Solution

  • I have found a solution with the changeColumn method in my migration file and it works like a charm.

    By the way, these properties works also with the addColumn method.

    public function up(Schema $schema, QueryBag $queries)
    {
        $table = $schema->getTable('oro_business_unit');
    
        $table->changeColumn('periodicite', [
            'oro_options' => [
                'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
                'entity' => ['label' => 'Périodicité'],
                'form' => [
                    'form_type' => ChoiceType::class,
                    'form_options' => [
                        'choices' => [
                            'Mensuel' => 'Mensuel',
                            'Trimestriel' => 'Trimestriel',
                            'Semestriel' => 'Semestriel',
                            'Annuel' => 'Annuel'
                        ]
                    ]
                ],
            ],
        ]);
    }