Shopware 5.5插件配置变量前端输出

时间:2018-10-10 11:39:46

标签: symfony smarty shopware

所以,我更新到了SW 5.5,此后我的配置变量没有出现在前端中,但是我很确定自己在代码中犯了一个错误,但是我看不到哪里..因为它在以前起作用

public function extendsFrontend(Enlight_Event_EventArgs $args)
    {
        /** @var \Enlight_Controller_Action $controller */
        $controller = $args->get('subject');
        $view = $controller->View();

        $view->addTemplateDir($this->pluginPath . '/Resources/views');

        $shop = Shopware()->Shop();
        $this->config = Shopware()->Container()->get('shopware.plugin.cached_config_reader')->getByPluginName($this->pluginName, $shop);

        $config = array(
            'height' => $this->config['height']
        );

        $view->assign($this->pluginName, $config);
    }

这是我的功能,因此我可以使用{$height}在前端显示此选项,但此方法不再起作用。如果我忘记了一些东西或您需要更多信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

您可以获取自定义插件存储配置值,例如以下代码

<?php

namespace Brandbassador\Subscriber;
use Enlight\Event\SubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class BrandbassadorEvents implements SubscriberInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;
    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Action_PostDispatch_Backend_Config' => 'extRegister',
            'Enlight_Controller_Action_PostDispatchSecure_Backend_Config' => 'extRegister'
        ];
    }

    public function extRegister(\Enlight_Event_EventArgs $event_EventArgs)
    {
        $shop = false;
        if ($this->container->initialized('shop')) {
            $shop = $this->container->get('shop');
        }
        if (!$shop) {
            $shop = $this->container->get('models')->getRepository(\Shopware\Models\Shop\Shop::class)->getActiveDefault();
        }
        $config = $this->container->get('shopware.plugin.cached_config_reader')->getByPluginName('Brandbassador', $shop);
        if ($config['authKey'] || $config['authKey'] != "") {
            $authKey = $config['authKey'];
            // error_log(print_r(array($authKey), true)."\n", 3, Shopware()->DocPath() . 'var/log/test.log');
        }
    }

}

如果您有任何疑问,请参阅此Shopware

答案 1 :(得分:0)

因此,您需要在Resources/config.xml的config.xml文件中添加配置

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/shopware/5.3/engine/Shopware/Components/Plugin/schema/config.xsd">
    <elements>
        <element required="true" type="text" scope="locale">
            <name>env</name>
            <label lang="de">Plugin Height</label>
            <label lang="en">Plugin Height</label>
            <value>10</value>
            <description lang="de">Plugin Height</description>
            <description lang="en">Plugin Height</description>
        </element>
    </elements>
</config>

以及Subscriber/Frontend.php

中的文件Frontend.php中
<?php

namespace MyPlugin\Subscriber;

use Enlight\Event\SubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Frontend implements SubscriberInterface
{
    /**
     * @var ContainerInterface
     */
    private $config;

    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->config = Shopware()->Container()->get('shopware.plugin.cached_config_reader')->getByPluginName('MyPlugin');
    }

    public function extendsFrontend(Enlight_Event_EventArgs $args)
    {
        // get the height
        $height = $this->config['height'];

        $view->assign($this->pluginName, $height);
}