Customizing Product Model and Admin form


#1

Hello,

I’m trying to customize/extend the Product base model without success.

I’ve already done:

  • Extend model/entity and add “warehouse” field.
  • Update database schema (doctrine)
  • Extend ProductType Form from ProductBundle and added field

My problem is when I go to the admin -> Product -> Create. I cant see the new fields…
I saw somewhere in the docs that is needed to override the form templates but in the original Admin Product form templates there are a lot of menu/sections functions with the KnpBundle and I don’t want to re-build entirely the form view. So, It’s supossed if I add new fields to the form it should be directly rendered in the view.

Form Extension:

namespace App\Form\Extension;

Use Sylius\Bundle\ProductBundle\Form\Type\ProductType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class ProductTypeExtension extends AbstractTypeExtension
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('warehouse', TextType::class, [
                'label' => 'app.form.product.warehouse',
            ])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getExtendedType(): string
    {
        return \Sylius\Bundle\CoreBundle\Form\Extension\ProductTypeExtension::class;
    }
}

services.yaml:

app.form.extension.type.product:
    class: App\Form\Extension\ProductTypeExtension
    tags:
        - { name: form.type_extension, extended_type: Sylius\Bundle\ProductBundle\Form\Type\ProductType, priority: -5 }

_sylius.yaml

sylius_product:
    driver: doctrine/orm
    resources:
        product:
            classes:
                model: App\Entity\Product

What i’m doing wrong?

Thank you.


#2

You have to add the field to the template(s).
Check SyliusAdminBundle/views/Product/Tab/_details.html.twig and SyliusAdminBundle/views/ProductVariant/Tab/_details.html.twig.


#3

Thank you very much! this is what I was looking for.