For get email of user in shopware 6 .I used newsletter's codes like above
<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>
It is work.But by default after submit Shopware hide Form then display in panel info. I don't want the form be hidden .I want to the form info panel display above my form newsletter.How i can do it?
For solving the issue that i discued.You must define a Controller . create a controller like above src/Controller/NewsletterController.php
<?php declare(strict_types=1);
namespace AK\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\Content\ContactForm\ContactFormService;
use Shopware\Core\Content\Newsletter\NewsletterSubscriptionServiceInterface;
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Storefront\Framework\Captcha\Annotation\Captcha;
/**
* @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;
}
}
then define your controller in config/servives.xml
<services>
<service id="AK\Controller\NewsletterController" public="true">
<argument type="service" id="Shopware\Core\Content\ContactForm\ContactFormService" />
<argument type="service" id="Shopware\Core\Content\Newsletter\NewsletterSubscriptionService"/>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
</services>
</container>
and define in config/routs.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>
Now you can mange response action in your form by controller.