2016-12-02 42 views
0

我需要爲產品實現SKU代碼,我只是想知道有沒有人想到這樣做的最佳方式。 SKU需要在創建後進行編輯。Sylius - 可編輯的產品代碼(SKU)

我覺得有幾個方面:

  1. (Idealy)我想用Product.Code,但這不是產品創新後可編輯字段。我似乎我需要重寫ProductType#buildForm類/方法不使用AddCodeFormSubscriber()。雖然我似乎無法弄清楚如何讓系統使用不同的表單。

  2. 將SKU添加到產品模型中,並找出如何將其添加到ProductType表單,並再次嘗試並找出如何使用不同的表單。

我很樂意提供關於如何以正確方式進行操作的建議。

Sylius的開發人員是否願意詳細說明他們爲什麼決定讓Code字段不可編輯?

+0

無論哪種方式,你需要重寫窗體,所以選項2似乎是最好。在文檔中有覆蓋表單的頁面 - http://docs.sylius.org/en/latest/customization/form.html。您可能需要覆蓋變體和產品,但可能只有一個。我還沒有檢查過一段時間的代碼 – Brett

回答

0

如果您希望使用產品代碼作爲Sylius Beta.1中的可編輯字段,您可以創建ProductType擴展到當前產品類型並添加您的自定義訂閱者,這將使代碼字段可編輯。我做這在我的包,它的工作原理:

創建用戶類wchich將禁用狀態更改爲false:

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

} 

創建形式擴展,使用自定義用戶:

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 } 
相關問題