Override bundle template from another bundle in Symfony 4/5

前端 未结 3 1974
轻奢々
轻奢々 2021-02-09 07:03

because bundle inheritance is deprecated since Symfony 3.4 and will be removed in 4.0, I\'m finding a new solution. I have:

  • Application
  • FooBundle
相关标签:
3条回答
  • 2021-02-09 07:39

    Here's a more comprehensible example

    Let's start with making some assumptions

    Your bundle's name: AcmeBundle

    Bundle you want to override: FOSUserBundle

    Run command php bin/console debug:twig and find the namespace of the bundle you want to override. In this case it's @FOSUser.

    Your bundle extension should look like this

    <?php // src/AcmeBundle/DependencyInjection/AcmeExtension.php
    
    namespace AcmeBundle\DependencyInjection;
    
    // ...
    
    use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
    
    class AcmeExtension extends Extension implements PrependExtensionInterface
    {
        // ...
    
        public function prepend(ContainerBuilder $container)
        {
            $container->loadFromExtension('twig', array(
                'paths' => array(
                    '%kernel.project_dir%/src/AcmeBundle/Resources/FOSUserBundle/views' => 'FOSUser', // You use the namespace you found earlier here. Discard the `@` symbol.
                ),
            ));
        }
    }
    

    Now you can create src/AcmeBundle/Resources/FOSUserBundle/views/Security/login.html.twig to override the login template of FOSUserBundle.

    This was just an example for FOSUserBundle. You can change bundle names depending on what you're trying to override.

    0 讨论(0)
  • 2021-02-09 07:44

    So I have recently needed same functionality and with help from comment @NicoHasse I managed to make working example

    In your bundle bundle extension class you need to implement PrependExtensionInterface and then you can modify twig paths. Then you need to know original namespace your you need to override (php bin/console debug:twig).

    You can confirm its working with twig debug command where you should see your path at first place of that namespace.

    class YourExtensionClass extends Extension implements PrependExtensionInterface
    {
        public function prepend(ContainerBuilder $container)
        {
            $container->loadFromExtension('twig', [
                'paths' => [
                    '%kernel.project_dir%/vendor/xx/yy/zzz' => 'OriginalVNamespace',
                ]
            ]);
    
    0 讨论(0)
  • 2021-02-09 07:47

    I will show how it looks in my case(Symfony 5.1). To overwrite views for bundles JMoseCommandSchedulerBudnle and TwigBundle:

    1. Create your own directory with custom third party bundle templates(I put them In Resources/views/bundles):

    2. Create folders for each bundle where you want to override templates:

    3. Create custom templates(for example to create custom Twig Error Pages):

    4. Implement PrependExtensionInterface and add custom paths to the Twig Extension configuration:

    class AppngCmsExtension extends Extension implements PrependExtensionInterface {
        public function load(array $configs, ContainerBuilder $container)
        {
            $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
            $loader->load('services.yaml');
        }
    
        public function prepend(ContainerBuilder $container)
        {
            // I recommend using FileLocator here 
            $thirdPartyBundlesViewFileLocator = (new FileLocator(__DIR__ . '/../Resources/views/bundles'));
    
            $container->loadFromExtension('twig', [
                'paths' => [
                    $thirdPartyBundlesViewFileLocator->locate('JMoseCommandSchedulerBundle') => 'JMoseCommandScheduler',
                    $thirdPartyBundlesViewFileLocator->locate('TwigBundle') => 'Twig',
                ],
            ]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题