Sylius: Plugin's template cannot be overriden by theme

Created on 15 Aug 2018  路  2Comments  路  Source: Sylius/Sylius

Sylius version affected: 1.2.3

Description

If you want to override a plugin's template (a plugin, not a bundle), and you want to use the theme for that, it's not working.

Steps to reproduce

Let's say you install the BitBagCms plugin.

Then you want to override the template @BitBagSyliusCmsPlugin/Shop/Page/show.html.twig.
And you want to do tat in your theme.

So you create this file:

app/themes/MyTheme/BitBagSyliusCmsPlugin/Shop/Page/show.html.twig

And according to everything it should work. But it doesn't.

Possible Solution

The bug is actually in https://github.com/Sylius/Sylius/blob/1.2/src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php#L65-L75.

I did that to fix the problem (very succinct):

diff --git a/src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php b/src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php
index 0d7fbb1f5b..22191df875 100644
--- a/src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php
+++ b/src/Sylius/Bundle/ThemeBundle/Templating/TemplateNameParser.php
@@ -66,8 +66,14 @@ final class TemplateNameParser implements TemplateNameParserInterface
             return $this->decoratedParser->parse($name);
         }

+        if (isset($matches[1]) && preg_match('/^.+Plugin$/', $matches[1])) {
+            $bundleName = $matches[1];
+        } else {
+            $bundleName = $matches[1] ? $matches[1] . 'Bundle' : '';
+        }
+
         $template = new TemplateReference(
-            $matches[1] ? $matches[1] . 'Bundle' : '',
+            $bundleName,
             $matches[2],
             $matches[3],
             $matches[4],

It works well since we call the templates with the Plugin prefix:

@BitBagSyliusCmsPlugin/Shop/Page/show.html.twig
                ^^^^^^
Potential Bug Roadmap

Most helpful comment

@chaenu sadly it is...

It used to work until 1.3.1

then after this commit only BUNDLES can be overriden in themes.
https://github.com/Sylius/Sylius/commit/e5a5042188b85b15f98b7ee8f9d3c1a4f9979a6b#diff-1703885a80095c3a851cfa73b366383d

After 1.3.1 neither of those declarations can be extended in theme folder:
{% include '@MyAmazingSyliusPlugin/Page/thisNeedsToBeOverridenInTheme.html.twig' %} {% include 'MyAmazingSyliusPlugin::Page/thisNeedsToBeOverridenInTheme.html.twig' %}
Maybe @pamil has some insight why it was done this way.

All 2 comments

Hello,
is this still true for version 1.3.3? I tried to overwrite templates for the CMS plugin as well, but failed. I wonder if I'm doing something wrong or if it's still this bug? Couldn't make it work with above fix either.

@chaenu sadly it is...

It used to work until 1.3.1

then after this commit only BUNDLES can be overriden in themes.
https://github.com/Sylius/Sylius/commit/e5a5042188b85b15f98b7ee8f9d3c1a4f9979a6b#diff-1703885a80095c3a851cfa73b366383d

After 1.3.1 neither of those declarations can be extended in theme folder:
{% include '@MyAmazingSyliusPlugin/Page/thisNeedsToBeOverridenInTheme.html.twig' %} {% include 'MyAmazingSyliusPlugin::Page/thisNeedsToBeOverridenInTheme.html.twig' %}
Maybe @pamil has some insight why it was done this way.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Chrysweel picture Chrysweel  路  3Comments

stefandoorn picture stefandoorn  路  3Comments

mikemix picture mikemix  路  3Comments

reyostallenberg picture reyostallenberg  路  3Comments

javiereguiluz picture javiereguiluz  路  3Comments