根据最新的文档,向Shopware 6页面添加自定义数据可以通过调用addExtension
来完成
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
public function addActiveProductCount(FooterPageletLoadedEvent $event): void
{
$event->getPagelet()->addExtension('product_count', $productCountResult);
但是,尽管发布说明升级说明没有提到对addExtension
的反对,但我的IDE也是如此:
不推荐使用“addExtension”方法
public function ExtendableTrait::addExtension(string $name, ?Struct $extension) void
将新的扩展结构添加到类存储中。传递的名称用作唯一标识符,也必须存储。 被否决: 5.6.0 工具:ExtendableInterface::addExtension
声明为:Shopware\Core\Framework\Struct\ExtendableTrait
来源:development/vendor/shopware/platform/src/Core/Framework/Struct/ExtendableTrait.php
更新:根据@cuong-huynh的发现,第二个参数($extension
)必须不再是null
,我添加了一个检查,以确保我们永远不会通过null
,但是在我的IDE中,这个方法仍然被标记为不推荐使用。
if ($productCountResult) {
$event->getPagelet()->addExtension('product_count', $productCountResult);
上面的代码显示addExtension
是不推荐的,尽管现在应该保证$productCountResult
永远不会成为null
,这要归功于以前的if
检查。
有人能告诉我应该使用哪种方法,或者在哪里找到相关的文档吗?或者,如果addExtension
被错误地标记为弃用(最近类似于EntityRepository ),则可以还原弃用标志。
发布于 2022-07-13 07:48:52
根据被反对的注释,@deprecated tag:v6.5.0 - second param $extension will not allow null anymore
,因为我知道我们仍然使用该函数,但是第二个param不能是null。
https://stackoverflow.com/questions/72968625
复制