twigbolt-cms

Overwrite backend template in bolt.cms


I am trying to overwrite a template file located in vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig (I want to replace the "block selection" dropdown). Regarding to #1173, #1269, #5588, #3768 and #5102 this is not supported by default so I have to write a extension for this. So I tried this:

BackendBlockSelectionExtension:

namespace Bundle\Site;

use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Bolt\Extension\SimpleExtension;

class BackendBlockSelectionExtension extends SimpleExtension
{
    public function getServiceProviders()
    {
        return [
            $this,
            new BackendBlockSelectionProvider(),
        ];
    }
}

BackendBlockSelectionProvider:

namespace Bundle\Site;

use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Silex\ServiceProviderInterface;

class BackendBlockSelectionProvider implements ServiceProviderInterface
{
    public function register(Application $app)
    {
        $side = $app['config']->getWhichEnd();

        if ($side == 'backend') {
            $path       = __DIR__ . '/App/templates/Backend';
            $filesystem = $app['filesystem'];

            $filesystem->mountFilesystem('bolt', new Filesystem(new Local($path)));

            $app['twig.loader.bolt_filesystem'] = $app->share(
                $app->extend(
                    'twig.loader.bolt_filesystem',
                    function ($filesystem, $app) {
                        $path = __DIR__ . 'src/App/templates/Backend/';

                        $filesystem->prependPath($path, 'bolt');

                        return $filesystem;
                    }
                )
            );
        }
    }

    public function boot(Application $app)
    {
    }
}

This seems to do the job, but I got an error I don't understand at all: The "bolt://app/theme_defaults" directory does not exist.

the error I am stuck with

So my final question is: Does anyone have some example code how to overwrite/modify vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig without touching the vendor folder?


Solution

  • This should be much simplier than this.

    In your extension class overwrite protected function registerTwigPaths() function like this:

    protected function registerTwigPaths()
    {
        if ($this->getEnd() == 'backend') {
            return [
                'view' => ['position' => 'prepend', 'namespace' => 'bolt']
            ];
        }
        return [];
    }
    
    private function getEnd()
    {
        $backendPrefix = $this->container['config']->get('general/branding/path');
        $end = $this->container['config']->getWhichEnd();
    
        switch ($end) {
            case 'backend':
                return 'backend';
            case 'async':
                // we have async request
                // if the request begin with "/admin" (general/branding/path)
                // it has been made on backend else somewhere else
                $url = '/' . ltrim($_SERVER['REQUEST_URI'], $this->container['paths']['root']);
                $adminUrl = '/' . trim($backendPrefix, '/');
                if (strpos($url, $adminUrl) === 0) {
                    return 'backend';
                }
            default:
                return $end;
        }
    }
    

    Now you can crete a view directory in your extensions directory in which you can define templates in structure like in Bolt's default. I would start with copy and overwrite.