2

I try the example program "Slogan of the day" on shopware 5.2.24

But the code doesn't work.

In the file Bootstrap.php, I defined the 3 important functions. In the function install, the callback function "onFrontendPostDispatch" will be called.

public function install()
{
    $this->subscribeEvent(
        'Enlight_Controller_Action_PostDispatchSecure_Frontend',
        'onFrontendPostDispatch'
    );

    $this->createConfig();

    return true;
}

private function createConfig()
{
    $this->Form()->setElement(
        'select',
        'font-size',
        array(
            'label' => 'Font size',
            'store' => array(
                array(12, '12px'),
                array(18, '18px'),
                array(25, '25px')
            ),
            'value' => 12
        )
    );

    $this->Form()->setElement('boolean', 'italic', array(
        'value' => true,
        'label' => 'Italic'
    ));
}

In this callback function, I definded the parameters for the new tpl file and the location of the new tpl file: [ _ DIR _ . '/Views' ]

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

    $view->addTemplateDir(
        __DIR__ . '/Views'
    );

    $view->assign('sloganSize', $this->Config()->get('font-size'));
    $view->assign('italic', $this->Config()->get('italic'));
    $view->assign('slogan', $this->getSlogan());

}

public function getSlogan()
{
    return array_rand(
        array_flip(
            array(
                'My Slogan Number 1',
                'My Slogan Number 2',
                'My Slogan Number 3',
            )
        )
    );
}

The new tpl file is:

{extends file="parent:frontend/index/index.tpl"}

{block name="frontend_index_navigation_categories_top_include"}

<style>
    .slogan-box {
        width:100%;
        text-align:center;
    }
    .slogan {
        {if $italic}font-style:italic;{/if}
        font-size:{$sloganSize}px;
    }
</style>

<div class="slogan-box">
    <span class="slogan">{$slogan}</span>
</div>

{$smarty.block.parent}

{/block}

The location of new tpl file is:

enter image description here

But in the Homepage, I can't see the slogan... It doesn't work.

The file Bootstrap.php works fine. But the slogan can't be seen on the homepage.

Is the connection between the Bootstrap.php und index.tpl wrong?

Does anyone know where i have gone wrong? Thans a lot!!!

Machavity
  • 30,841
  • 27
  • 92
  • 100
Ying Style
  • 752
  • 2
  • 7
  • 25
  • did you find out what the problem was in the end? I'm having the same issue, and i know that the view gets the slogan variable passed, because if i edit the original template under themes/Frontend/Bare/frontend/index/index.tpl and addthe {$slogan} somewhere, the slogan gets displayed. So the question is why is the index.tpl file from the plugin not in effect – flynorc Jul 11 '17 at 14:26
  • I have fresh 5.7.7 not working either. Windows 11 + Wampserver 3.2.7 - 64 bit, PHP 8.0.14. however I have an apache warning: PHP in PATH. I'm not sure if it has something to do with the problem. – user2511599 Mar 11 '22 at 23:26

3 Answers3

0

It seems, createConfig function is undefined. Either define the function or comment out function call $this->createConfig(); inside install() function.
Now re-install your plugin from backend Plugin Manager. It should work.

gins t
  • 483
  • 6
  • 16
  • I have defined the function "createConfig". Actually I downloaded the example plugin from here: https://developers.shopware.com/developers-guide/plugin-quick-start/ But It doesn't work... – Ying Style Jun 20 '17 at 08:39
  • I did, and the permissions for plugin file is write- & read-access for all users. And I am sure, the Bootstrap.php in the Plugin works. When I write the code e.g. [echo "Test"; die;], the code works. – Ying Style Jun 20 '17 at 19:54
0

Plugin looks well, don't see any problem.

Did you clean template cache after install and activate plugin? In backend manu: Configuration->Cache/Perfomance, then go to Che cache tab in Perfomance window -> Select all -> Clear, then confirm theme compiling.

Or you can add next code to the plugin bootstrap SwagSloganOfTheDay/Bootstrap.php, so you will got theme compiling propose all the time when you try to enable plugin:

public function enable() {
    if(parent::enable())
        return array(
            'success' => true,
            'message' => 'enabled',
            'invalidateCache' => array('config', 'template', 'theme')
        );
}
Alexey Palamar
  • 1,440
  • 1
  • 10
  • 16
0

You need to register your template first. Add a php file in Subscriber/Frontend:

/**
 * @inheritdoc
 */
public static function getSubscribedEvents()
{
    return [
        'Theme_Inheritance_Template_Directories_Collected' => 'onCollectTemplateDir'
    ];
}

    /**
     * @param \Enlight_Event_EventArgs $args
     *
     * Since 2.3.1 - added for template security
     */
    public function onCollectTemplateDir(\Enlight_Event_EventArgs $args)
    {
    
        $dirs = $args->getReturn();
        $dirs[] = $this->pluginDirectory . '/Resources/views';
        $args->setReturn($dirs);
    }