通过时事通讯代码获取有关购物软件 6 的电子邮件。(提交后的自定义操作)



用于在购物软件 6 中获取用户的电子邮件。我使用了上述时事通讯的代码

<div class="cms-block footer-newsletter w-100">
<div class="cms-element-form ">
<form action="{{ path('frontend.form.newsletter.register.handle') }}"
method="post"
class="d-flex nws-search py-5"
data-form-csrf-handler="true"
data-form-validation="true">
{{ sw_csrf('frontend.form.newsletter.register.handle') }}
{% set formViolations = app.request.get('errors') %}
<input type="hidden" name="option" value="subscribe"/>
<input type="submit" class="submit--hidden d-none">
<input name="email"
type="email"
id="footerNewsletterMail"
placeholder="{{ "account.personalMailPlaceholder"|trans }}{{ "general.required"|trans }}"
required="required"
value="{{ data.get('email') }}"
class="px-3 py-2 py-sm-3 {% if formViolations.getViolations('/email') %} is-invalid{% endif %}"/>
<button type="submit" class="px-4 py-2 py-sm-3">JETZT REGISTERIEREN</button>
</form>
</div>
</div> 

这是工作。但默认情况下,提交购物软件后隐藏表单,然后显示在面板信息中。 我不希望表格被隐藏.我想在我的表单新闻稿上方显示表单信息面板。我该怎么做?

为了解决我提出的问题。必须定义控制器 。 创建一个像上面这样的控制器 src/Controller/NewsletterController.php

<?php declare(strict_types=1);
namespace AKController;
use SymfonyComponentHttpFoundationJsonResponse;
use ShopwareCoreSystemSalesChannelSalesChannelContext;
use ShopwareStorefrontControllerStorefrontController;
use ShopwareCoreFrameworkValidationDataBagRequestDataBag;
use ShopwareCoreContentContactFormContactFormService;
use ShopwareCoreContentNewsletterNewsletterSubscriptionServiceInterface;
use ShopwareCoreFrameworkValidationExceptionConstraintViolationException;
use SymfonyComponentRoutingAnnotationRoute;
use ShopwareCoreFrameworkRoutingAnnotationRouteScope;
use ShopwareStorefrontFrameworkCaptchaAnnotationCaptcha;
/**
* @RouteScope(scopes={"storefront"})
*/
class NewsletterController extends StorefrontController
{
/**
* @var NewsletterSubscriptionServiceInterface
*/
private $newsletterService;
public function __construct(
ContactFormService $contactFormService,
NewsletterSubscriptionServiceInterface $newsletterService
) {
$this->contactFormService = $contactFormService;
$this->newsletterService = $newsletterService;
}
/**
* @Route("/form/newsletter/custom", name="frontend.form.newsletter.register.handle", methods={"POST"}, defaults={"XmlHttpRequest"=true})
* @Captcha
*/
public function handleNewsletter(RequestDataBag $data, SalesChannelContext $context): JsonResponse
{
$response = $this->handleSubscribe($data, $context);
return new JsonResponse($response);
}
private function handleSubscribe(RequestDataBag $data, SalesChannelContext $context): array
{
try {
$this->newsletterService->subscribe($data, $context);
$response[] = [
'type' => 'success',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'info',
'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
])
];
$response[] = [
'type' => 'info',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'info',
'list' => [$this->trans('newsletter.subscriptionPersistedInfo')],
]),
];
} catch (ConstraintViolationException $exception) {
$errors = [];
foreach ($exception->getViolations() as $error) {
$errors[] = $error->getMessage();
}
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => $errors,
]),
];
} catch (Exception $exception) {
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => [$this->trans('error.message-default')],
]),
];
}
return $response;
}
}

然后在配置/服务中定义控制器.xml

<services>
<service id="AKControllerNewsletterController" public="true">
<argument type="service" id="ShopwareCoreContentContactFormContactFormService" />
<argument type="service" id="ShopwareCoreContentNewsletterNewsletterSubscriptionService"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
</services>
</container>

并在配置/路由中定义.xml

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="../../Controller" type="annotation" />
</routes>

现在,您可以按控制器管理窗体中的响应操作。

最新更新