Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override bundle template from another bundle in Symfony 4/5

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
  • BarBundle

And I haven't problem with overriding templates in Application. But I need to override templates from BarBundle in FooBundle. It was so easy with bundle inheritance but I'm lost now :)

I tried twig namespaces - no success, but I configured it in wrong way maybe. My goal is to have base templates in BarBundle that I can override in FooBundle or Application or both. (it is because BarBundle is third-party bundle and FooBundle is my bundle used in many projects).

Is it still possible without bundle inheritance and how?

Thanks.

like image 678
brardong Avatar asked May 30 '18 13:05

brardong


People also ask

How to override bundle Symfony?

The easiest way to "override" a bundle's routing is to never import it at all. Instead of importing a third-party bundle's routing, copy that routing file into your application, modify it, and import it instead.

What is a bundle in Symfony?

A Symfony bundle is a collection of files and folders organized in a specific structure. The bundles are modeled in such a way that it can be reused in multiple applications. The main application itself is packaged as a bundle and it is generally called AppBundle.


2 Answers

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',
            ]
        ]);
like image 177
M. Kebza Avatar answered Sep 28 '22 03:09

M. Kebza


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.

like image 43
Taylan Avatar answered Sep 28 '22 03:09

Taylan