简体   繁体   中英

How to remove a field from a form in the Sylius admin backend?

I'm working on a Sylius application and want to remove a form in the admin backend.

In this special case it's the field on_hand (in the ProductVariant creation and update context), that shouldn't be available. (Since we get that information from suppliers directly and import it automatically into the database.) I've already found the correct template ( /vendor/sylius/sylius/src/Sylius/Bundle/AdminBundle/Resources/views/ProductVariant/Tab/_inventory.html.twig ), created a copy in my app and removed the field from the view. But its still in the form object. I could not find it in any form. It seems to be generated, but I don't see where this generation takes place and how to exclude this field from the form generation.

How to remove the field on_hand and remove it?

How to handle such cases in general and find and remove fields from forms, provided by Sylius?


The docu article " Customizing Forms " says:

You can:

  • add completely new fields,
  • modify existing fields, make them required, change their HTML class, change labels etc.,
  • remove fields that are not used.

...and shows, what to do, " if you are planning to add new fields ". But it doesn't explain, how to remove fields -- and it seems to be a bit more complicated than adding.


What I tried so far:

final class ProductVariantTypeExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            // ->add(...)
            // ...
            ->remove('onHand')
        ;
    }
    public static function getExtendedTypes(): iterable
    {
        return [BaseProductVariantType::class];
    }
}

But the remove(...) call seems to be ignored.

The code

final class ProductVariantTypeExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            // ->add(...)
            // ...
            ->remove('onHand')
        ;
    }
    public static function getExtendedTypes(): iterable
    {
        return [BaseProductVariantType::class];
    }
}

was correct, it also was not being ignored. The problem was, that at this place / point of time the Form is not completed yet: The field onHand is not there, so attempts to remove it cannot have any effect, since it's being added later. The trick is to defer the processing of the ProductVariantTypeExtension . It's easily done by setting the priority :

/config/services.yaml

...
parameters:
    ...
    services:
        ...
            App\Form\Extension\ProductVariantTypeExtension:
                tags:
                    - { name: 'form.type_extension', extended_type: 'Sylius\Bundle\ProductBundle\Form\Type\ProductVariantType', priority: -1 }

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