简体   繁体   中英

Sylius Product customized model : fields not showed in form

I started to learn Sylius few days ago. I need an ecommerce module within my Symfony 3 project. So I gonna extend my project with Sylius, I'll just use the Core/Components stack.

  1. I started with the installation of the Sylius ProductBundle (cf. http://docs.sylius.org/en/latest/bundles/SyliusProductBundle/installation.html )

Seems simple but didn't work at first because Composer installed a stable version (0.19) which is not supported anymore... Anyway, I finally installed the sylius/sylius ~1.0@dev package so my composer.json looks like that :

"minimum-stability" : "dev",
"prefer-stable" : true,
"require" : {
    "php" : "^7.0",
    "sylius/sylius" : "^1.0@beta",
    "friendsofsymfony/user-bundle" : "~2.0@dev",
    "symfony/assetic-bundle" : "^2.8",
    "gedmo/doctrine-extensions" : "^2.4"
},

=> First question : is this the right way ???

  1. Then I created a SyliusBundle within my project to decouple the entire Sylius configuration from the rest of my project.

In SyliusBundle/app/config.yml :

imports:
    - { resource: "@SyliusProductBundle/Resources/config/app/config.yml" }

# Doctrine
stof_doctrine_extensions:
    default_locale: "%locale%"
    orm:
        default:
            tree: true
            sluggable: true
            timestampable: true
            loggable: false
            sortable: true

# Product
sylius_product:
    driver: doctrine/orm
    resources:
        product:
            classes:
                model: OfferBundle\Entity\Offer
                repository: OfferBundle\Entity\OfferRepository
                form: OfferBundle\Form\OfferType

I followed the Sylius guideline to customize my Offer model inherited from Sylius Product model (quite simple)

in OfferBundle/Entity/Offer.php :

<?php
namespace OfferBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Product as BaseProduct;


/**
 * @ORM\Table(name="offer")
 * @ORM\Entity(repositoryClass="OfferRepository")
 */
class Offer extends BaseProduct
{

=> question 2 : sounds good to you ?

  1. Then I customized the form extended my Offer form with the Sylius Product form (cf. http://docs.sylius.org/en/latest/customization/form.html )

in OfferBundle/Form/OfferType.php :

<?php

namespace OfferBundle\Form;

use Symfony\Component\OptionsResolver\OptionsResolver;
use OfferBundle\Entity\Offer;
use Symfony\Component\Form\AbstractType;


class OfferType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['data_class' => Offer::class]);
    }


    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'offer';
    }


    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return \Sylius\Bundle\ProductBundle\Form\Type\ProductType::class;
    }
}

In OfferBundle/Form/Extension/OfferTypeExtension.php :

<?php

namespace OfferBundle\Form\Extension;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Form\Type\WysiwygType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use OfferBundle\Form\Type\TagsType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use OfferBundle\Entity\Offer;
use MediaBundle\Form\MediaFileType;
use MediaBundle\Form\MediaVideoType;
use CompanyBundle\Entity\Company;
use Tetranz\Select2EntityBundle\Form\Type\Select2EntityType;
use Admin\CompanyBundle\Controller\CompanyController;
use Symfony\Component\Form\AbstractTypeExtension;
use OfferBundle\Form\ContactType;
use OfferBundle\Form\PriceType;
use OfferBundle\Form\OfferType;


final class OfferTypeExtension extends AbstractTypeExtension
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('advantage', WysiwygType::class)
            ->add('ecommerce')
            ->add('enabled')
            ->add('bestseller')
            ->add('company', Select2EntityType::class, [
                'multiple' => false,
                'remote_route' => 'admin_company_company_autocomplete',
                'class' => Company::class,
                'primary_key' => 'id',
                'text_property' => 'name',
                'minimum_input_length' => 0,
                'page_limit' => CompanyController::NB_PER_PAGE,
                'allow_clear' => false,
                'delay' => 250,
                'cache' => false,
                'cache_timeout' => 60000,
                'language' => 'fr',
                'placeholder' => 'form.choose',
                'required' => true,
            ])
            ->add('state', ChoiceType::class, [
                'choices' => Offer::getStates(),
                'placeholder' => 'form.choose',
            ])
...

And the form extension is loaded by service. in OfferBundle/Resources/config/services.yml :

offer.form.extension.type.offer:
        class: OfferBundle\Form\Extension\OfferTypeExtension
        tags:
            - { name: form.type_extension, extended_type: OfferBundle\Form\OfferType }

Question 3 : It works BUT in see that Sylius\\Bundle\\CoreBundle\\Form\\Extension\\ProductTypeExtension contains additionnal fields like $images, $variantSelectionMethod... But it doesn't appear in my form. So I suppose that a service.yml somewhere is not loaded ?

Hope somebody can help me on that !

I think the problem is because you have overridden the form, and not extended it. So you only see your form fields, and not the Product form fields (as this form is not used anymore). If you want to have the Product form with some changes, you only have to make a form extension to modify the existing Product form.

http://docs.sylius.org/en/latest/customization/form.html

Hope this helps ! :)

您可能还需要覆盖表单模板才能显示那些添加的字段。

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