OroPlatform:覆盖核心实体表单构建器

OroPlatform: Override core entity form builder

上下文

我正在尝试更改核心实体之一的一个字段的表单类型:Business Unit

默认的表单字段是TextField,我想把它改成ChoiceType

这是我在迁移时创建的业务单位实体的自定义字段:

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

问题

我在 Oro 文档中看到 entity_config.yml 可以解决我的问题。我试过添加这些行,但它不起作用:

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"

我也尝试创建一个新的迁移来更改自定义字段的字段类型,但它不起作用

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

我不知道是否可以使用 YAML 文件覆盖实体配置元数据。如果有 - 请在评论中分享您用于实现它的文档。

但可以肯定的是,您可以使用架构迁移来管理相同的内容,如本例所示:

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

我在我的迁移文件中找到了一个使用 changeColumn 方法的解决方案,它非常有效。

顺便说一句,这些属性也适用于 addColumn 方法。

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