简体   繁体   中英

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',
                    ],
                ]
            )
        );
    }
}

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'
                    ]
                ]
            ],
        ],
    ]);
}

I don't know about the possibility to override entity config metadata using the YAML file. If there is - please share the documentation you used to implement it in the comments.

But for sure, you can manage the same using the schema migration, like in this example:

class UpdateOpportunityRelationFormType implements Migration
{
    /**
     * {@inheritdoc}
     */
    public function up(Schema $schema, QueryBag $queries)
    {
        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                Quote::class,
                'opportunity',
                'form',
                'form_type',
                OpportunitySelectType::class
            )
        );

        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                Quote::class,
                'opportunity',
                'form',
                'form_options',
                ['attr' => ['readonly' => true]]
            )
        );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM