2016-12-02 55 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 }