phptwigshopware6

Set template priority for 3rd party plugins


There is a new feature since Shopware 6.4.13.0 where you can set getTemplatePriority() in the bundle class to influnce the template loading order.

We currently have a ordered list of how we want the plugins to be loaded and set the installed_at date. It contains own plugins (custom/ folder) as well as 3rd party plugins.

Is there a way to port this to the new functionality (seems necessary according to https://stackoverflow.com/a/74139837/288568) ?

As I understand it only the plugin it self can set the setTemplatePriority() ? How do we inject / change such a priority for 3rd party plugins?

It might be possible to decorate the bundle class?

Here is the current situation:

    'DmfManuTheme',
    'CustomImporter',
    'OwnMoreCustomFields',
    'CustomListingHeader',
    'CustomPrintLayout',
    'DevertPdfExport',
    'TonurSeoFilterLandingpages6', // must be before CustomTheme/CustomProductListing as it extends listing.html.twig
    'CustomApplicationManagement',
    'CustomProductListing',
    'FroshProductCompare',
    'CustomRegistrationProcess',
    'mmeesRangeSliderPro',
    'CustomProductFilter', // must be after mmeesRangeSliderPro
    'CustomContactBanner',
    'OwnRadioPropertyFilter',
    'OwnHierarchicalAttributes',
    'CustomLocalMerchant',
    'MoorlFoundation',
    'MoorlMerchantFinder',
    'MoorlMerchantPicker',
    'CustomTheme', // must be after the MoorlMerchant plugins
    'CustomProductDownloads',
    'CustomFreeSampleOrder',
    'NetzpBlog6',
    'CrswCleverReachOfficial',
    'MoorlFormBuilder',
    'NetzpSearchAdvanced6',
    'CustomProductNotification',
    'CogiEtracker',
    'ApplifactionGoogleMapsPlugin',

Yes, this is quite some amount of plugins. We try to make one small plugin for each customization instead of big monolithique custom-plugins.

Own + Custom are our namespaces and there we could set the template priority. But I believe the full problem can not be solved that way.

EDIT:

Is it maybe possible to decorate \Shopware\Core\Framework\Bundle - I doubt it. Or would a compiler pass help?


Solution

  • There is the BundleHierarchyBuilder service you could decorate. Implement the method buildNamespaceHierarchy and get the return value by calling it on the decorated service. That should give you key value pairs with the key being the bundle name and the value the priority received from the bundle. You can then set the priority for each bundle and re-sort. Note that if you use template-extending apps (not bundles), you might have to completely re-implement the method in the decorator as their priority is overwritten by the version after the initial sort.

    public function buildNamespaceHierarchy(array $namespaceHierarchy): array
    {
        $hierarchy = $this->decorated->buildNamespaceHierarchy($namespaceHierarchy);
        $extensions = array_diff_key($hierarchy, $namespaceHierarchy);
    
        $extensions['MoorlMerchantFinder'] = 5;
        $extensions['TonurSeoFilterLandingpages6'] = 3;
        // ...
    
        asort($extensions);
    
        return array_merge(
            $extensions,
            $namespaceHierarchy
        );
    }