我需要为产品实现SKU代码,我只是想知道有没有人想过最好的方法来做到这一点。创建后,SKU需要可编辑。
我觉得我有两种方法:
(Idealy)我想使用Product.Code,但这不是产品创建后的可编辑字段。我似乎需要覆盖ProductType#buildForm类/方法以不使用AddCodeFormSubscriber()。虽然我似乎无法弄清楚如何让系统使用不同的形式。
将SKU添加到Product的模型中,并弄清楚如何将其添加到ProductType表单中,然后再次尝试找出如何使用其他表单。
我愿意接受有关如何以正确方式行事的建议。
是否有任何Sylius开发人员都会详细说明为什么他们决定使代码字段不可编辑?
答案 0 :(得分: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 }