Ich versuche die saveRegisterAction von Register controller und AddressFormType von AccountBundle in meinem eingenen Plugin zu überschreiben.
Als erstes habe ich das subscriber für saveRegisterAction erstellt, danach habe ich ein eigenes AddressFormType mit dieser Service-Konfiguration erstellt:
;
Leider der Service-Container sieht mein Service nicht shopware_account.form.addressform.register_plugin. Wenn die Formtypklasse (Form Type Class) automatisch erstellt wird, gibt es keine Objekte (Shopware_Components_Config and ModelManager). Kann jemand helfen?
Darüber hinaus müsste der Controller Optionen übergeben (public function buildForm(FormBuilderInterface $builder, array $options)) 3 Variablen: isShippingProvided, AddressType und companyConfirmation.
Momentan habe ich einen solchen Fehler:
FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance. in vendor/symfony/form/FormConfigBuilder.php on line 211
Ich habe es auf folgende Weise versucht:
public static function getSubscribedEvents()
{
return [
'Shopware_Form_Builder' => 'onAddressFormBuild',
];
}
public function onAddressFormBuild(\Enlight_Event_EventArgs $event)
{
if ($event->getReference() !== BaseAddressFormType::class) {
return;
}
$builder = $event->getBuilder();
$formBuilder = $builder->getConfig();
$this->addVatIdValidation($formBuilder);
if ($this->addressType == self::ADDRESS_TYPE_BILLING) {
$this->addCompanyConfirmationValidation($builder);
}
}
/**
* @param FormBuilderInterface $builder
*/
private function addVatIdValidation(FormBuilderInterface $builder)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
/** @var Address $data */
$data = $form->getData();
$customerType = $form->get('additional')->get('customer_type')->getData();
if ($customerType !== Customer::CUSTOMER_TYPE_BUSINESS || !empty($data->getVatId())) {
return;
}
if ($this->addressType == self::ADDRESS_TYPE_SHIPPING && !$this->isShippingProvided) {
return;
}
if (EUStates::isEUCountry($data->getCountry()->getIso())) {
$notBlank = new NotBlank(['message' => null]);
$error = new FormError($notBlank->message);
$error->setOrigin($form->get('vatId'));
$form->addError($error);
}
});
}
/**
* @param FormBuilderInterface $builder
*/
private function addCompanyConfirmationValidation(FormBuilderInterface $builder)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
/** @var Address $data */
$data = $form->getData();
if (!$this->companyConfirmation && !EUStates::isEUCountry($data->getCountry()->getIso())) {
$message = 'Please confirm you place your order for a business entity by checking the box below. The products on this site are available for commercial customers only.';
$error = new FormError($message);
$error->setOrigin($form->get('additional')->get('companyConfirmation'));
$form->addError($error);
}
});
}