2016-12-02 11 views
0

私は製品のSKUコードを実装する必要があります。私はこれを行うための最善の方法について誰も考えていません。 SKUは作成後に編集可能である必要があります。シリウス編集可能商品コード(SKU)

私はいくつかの方法を持っている感じ:

  1. (Idealy)私はProduct.Codeを使用したいが、これは、製品の作成後に編集可能なフィールドではありません。 AddCodeFormSubscriber()を使用しないようにProductType#buildFormクラス/メソッドをオーバーライドする必要があるようです。私は、システムに別のフォームを使用させる方法を理解していないようですが。

  2. SKUをProductモデルに追加し、ProductTypeフォームに追加する方法を理解し、別のフォームの使用方法を再度試してみてください。

私は正しい方法を実行するための提案をしています。

なぜコードフィールドを編集不可能にすることにしたのか、Syliusの開発者は心配していますか?

+0

どちらの方法でもフォームをオーバーライドする必要があります。オプション2は、ベスト。ドキュメンテーション - http://docs.sylius.org/en/latest/customization/form.htmlにフォームをオーバーライドするページがあります。バリアントとプロダクトの両方をオーバーライドする必要があるかもしれませんが、おそらく1つだけかもしれません。私はしばらくコードをチェックアウトしていない – Brett

答えて

0

Sylius Beta.1で編集可能なフィールドとして製品コードを使用する場合は、現在の製品タイプにProductType拡張を作成し、コードフィールドを編集可能にするカスタムサブスクライバを追加できます。私は私のバンドルにこれをしなかったし、それが動作します:

namespace App\Bundle\Form\EventListener; 

/* add required namespaces */ 

/** 
* Custom code subscriber 
*/ 
class CustomCodeFormSubscriber implements EventSubscriberInterface 
{ 

    private $type; 
    private $label; 

    /** 
    * @param string $type 
    * @param string $label 
    */ 
    public function __construct($type = TextType::class, $label = 'sylius.ui.code') 
    { 
     $this->type = $type; 
     $this->label = $label; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public static function getSubscribedEvents() 
    { 
     return [ 
      FormEvents::PRE_SET_DATA => 'preSetData', 
     ]; 
    } 

    /** 
    * @param FormEvent $event 
    */ 
    public function preSetData(FormEvent $event) 
    { 
     $disabled = false; 

     $form = $event->getForm(); 
     $form->add('code', $this->type, ['label' => $this->label, 'disabled' => $disabled]); 
    } 

} 

フォームの拡張機能を作成し、カスタム加入者を使用します:

はfalseに無効状態を変更しますwchich加入者クラスを作成

namespace App\Bundle\Form\Extension; 

use App\Bundle\Form\EventListener\CustomCodeFormSubscriber; 
use Symfony\Component\Form\AbstractTypeExtension; 
use Symfony\Component\Form\FormBuilderInterface; 
use Sylius\Bundle\ProductBundle\Form\Type\ProductType; 

/* use other required namespaces etc */ 

/** 
* Extended Product form type 
*/ 
class ProductTypeExtension extends AbstractTypeExtension 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     /* custom stuff for ur form */ 
     $builder->addEventSubscriber(new CustomCodeFormSubscriber()); 
    } 

    public function getExtendedType() 
    { 
     return ProductType::class; 
    } 
} 

フォームを登録しますサービスとしての拡張:

app.form.extension.type.product: 
    class: App\Bundle\Form\Extension\ProductTypeExtension 
    tags: 
     - { name: form.type_extension, priority: -1, extended_type: Sylius\Bundle\ProductBundle\Form\Type\ProductType } 
関連する問題