英文:
Shopware6 Plugin Criteria all Countries
问题
My Shopware 6 plugin 应该读取所有 Sales Channel 的国家,然后将它们传递到前端。这些值已经成功传递到前端,然而数据检索不起作用。以下是文件:
<?php declare(strict_types=1);
namespace MyWave\Service;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
class AddDataToPage implements EventSubscriberInterface
{
private EntityRepositoryInterface $countryRepository;
public function __construct(
EntityRepositoryInterface $countryRepository
)
{
$this->countryRepository = $countryRepository;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'addAllCountries'
];
}
public function addAllCountries(FooterPageletLoadedEvent $event, SalesChannelContext $context): void
{
//占位符
$countries = "countries";
$criteria = new Criteria();
$country_results = $this->countryRepository->search($criteria, $context->getContext())->getEntities();
$country_results_encode = json_encode($country_results, true);
$country_results_decode = json_decode($country_results_encode, true);
$event->getPagelet()->assign(['allcountries' => $country_results_decode]);
}
}
和 XML 文件:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="MyWave\Service\AddDataToPage" >
<argument type="service" id="country.repository"/>
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
出现以下错误:
MyWave\Service\AddDataToPage::addAllCountries():
Argument #2 ($context) must be of type Shopware\Core\System\SalesChannel\SalesChannelContext,
string given, called in /var/www/clients/client1/web2/web/projekte/shopware-demo/
vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 117
请问我的错误在哪里,有人能帮我吗?
如上所述,我期望检索所有国家并将它们传递到前端以变量形式。使用演示字符串 "countries" 时可以正常工作。
英文:
My Shopware 6 plugin is supposed to read all countries from the Sales Channel and then pass them on to the frontend. The values are successfully passed on to the frontend, however, the data retrieval is not working. Here are the files:
<?php declare(strict_types=1);
namespace MyWave\Service;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
class AddDataToPage implements EventSubscriberInterface
{
private EntityRepositoryInterface $countryRepository;
public function __construct(
EntityRepositoryInterface $countryRepository
)
{
$this->countryRepository = $countryRepository;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'addAllCountries'
];
}
public function addAllCountries(FooterPageletLoadedEvent $event, SalesChannelContext $context): void
{
//Placeholder
$countries = "countries";
$criteria = new Criteria();
$country_results = $this->countryRepository->search($criteria, $context->getContext())->getEntities();
$country_results_encode = json_encode($country_results, true);
$country_results_decode = json_decode($country_results_encode, true);
$event->getPagelet()->assign( ['allcountries' => $country_results_decode]);
}
}
And xml:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="MyWave\Service\AddDataToPage" >
<argument type="service" id="country.repository"/>
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
The following error occurs:
MyWave\Service\AddDataToPage::addAllCountries():
Argument #2 ($context) must be of type Shopware\Core\System\SalesChannel\SalesChannelContext,
string given, called in /var/www/clients/client1/web2/web/projekte/shopware-demo/
vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 117
Where is my mistake, can someone help me?
As described above, I expected to retrieve all countries and pass them on to the frontend with the variable. With the demo string: countries, it worked.
答案1
得分: 2
以下是您要翻译的内容:
The event is dispatched like this in the core:
$this->eventDispatcher->dispatch(
new FooterPageletLoadedEvent($pagelet, $salesChannelContext, $request)
);
But this does not mean, that the listener is called with the same parameters.
The salesChannelContext
is contained in the event object and must be fetched from there using getSalesChannelContext
:
public function addAllCountries(FooterPageletLoadedEvent $event): void
{
$context = $event->getSalesChannelContext();
You also get the context directly:
$country_results = $this->countryRepository->search($criteria, $event->getContext())->getEntities();
英文:
The event is dispatched like this in the core:
$this->eventDispatcher->dispatch(
new FooterPageletLoadedEvent($pagelet, $salesChannelContext, $request)
);
But this does not mean, that the listener is called with the same parameters.
The salesChannelContext
is contained in the event object and must be fetched from there using getSalesChannelContext
:
public function addAllCountries(FooterPageletLoadedEvent $event): void
{
$context = $event->getSalesChannelContext();
You also get the context directly:
$country_results = $this->countryRepository->search($criteria, $event->getContext())->getEntities();
答案2
得分: 1
首先,只有事件对象被传递给监听方法。您可以通过以下方式获取SalesChannelContext
:
$event->getSalesChannelContext()
然后,您可以获取Context
:
$event->getSalesChannelContext()->getContext()
其次,在从存储库获取实体时,为什么要对数据进行编码然后解码呢?这些数据是实体的集合。集合有一个toArray()
方法或类似的方法,您可以直接使用它。
英文:
Firstly only event object is passed to listener method. You can get SalesChannelContext
by
$event->getSalesChannelContext()
And you can get Context
by
$event->getSalesChannelContext()->getContext()
Secondly, when fetching entities from repository why do u encode and then decode the data? The data is collection of entities. Collection has method toArray()
or sth like that. You can just use it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论