Search code examples
phpsmartyshopware

Shopware 5.2.11 include JavaScript in my plugin


I want to add a JavaScript file in my plugin. I was following this tutorial and was able to successfully get the Slogan of the Day on my installation (vagrant).

Additionally I want to load a JavaScript file. I changed the of index.tpl and added the following lines:

   {* Include own Javascript Code *}
    {block name="frontend_index_header_javascript_jquery_lib"}
      {debug}
      {$smarty.block.parent}
      {if $myVariable}<script type="text/javascript" src="{link   file='frontend/_public/src/js/myFile.js'}"></script>{/if}
    {/block}

Taking a look at the source code of the generated html the script tag is successfully loaded. However, my JavaScript file won't be loaded at all. Where do I have to put the file in my plugin folder ?

Currently I have this structure

NameOfZipfile.zip
└──Frontend
   ├─MyPlugin
   │ └─Views
   │   ├─_public
   │   │ └─src
   │   │   └─js
   │   │     └─myFile.js
   │   └─frontend
   │     └─index
   │       └─index.tpl
   └─Bootstrap.php

I am using shopware 5.2.11. I don't want to add inline-script. What am I doing wrong?


Solution

  • (Posted on behalf of the OP).

    Solution, I added following to the Bootstrap.php install method:

    $this->subscribeEvent('Theme_Compiler_Collect_Plugin_Javascript', 'addJsFiles');
    

    And this function:

     public function addJsFiles(Enlight_Event_EventArgs $args){
        $jsFiles = array(__DIR__ . '/Views/_public/src/js/myFile.js');
    
        return new Doctrine\Common\Collections\ArrayCollection($jsFiles);
    }