Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic loading of themes / Enable & add themes with PHP #110

Open
ghost opened this issue Dec 14, 2014 · 12 comments
Open

Dynamic loading of themes / Enable & add themes with PHP #110

ghost opened this issue Dec 14, 2014 · 12 comments

Comments

@ghost
Copy link

ghost commented Dec 14, 2014

How may I add a theme to the theme-list with PHP?
I like to dynamically add the theme by an event listener.

I can set the active theme by PHP:

$activeTheme = $this->container->get('liip_theme.active_theme');
$activeTheme->setName('blue');

=> The active theme "blue" must be in the themes list ().

How to add any theme name to the "theme list" ?

@ghost
Copy link
Author

ghost commented Dec 14, 2014

The solution is:

$activeTheme->setThemes(array('blue'));

Maybe this should be added to the documentation.

I think it would be good if anybody could add this function to ActiveTheme class:

public function addTheme($theme) {
    if(is_array($theme)) {
        $this->themes = array_merge($theme, $this->themes);
    } else {
    array_push($this->themes, $theme);
    }
}

or:

public function addTheme($theme) {
    array_push($this->themes, $theme);
}

public function addThemes(array $themes) {
    $this->themes = array_merge($themes, $this->themes);
}

@oleg-andreyev
Copy link
Contributor

Why do you want do that? To me theme list should be defined permanently in configs, otherwise it's something that you don't control.

@ghost
Copy link
Author

ghost commented Dec 15, 2014

We are using a dynamic system to upload, enable and disable themes. All settings should be saved in the database. Is there any way to disable the "it must be in the array (theme-list)"-feature? It would be great if the system would just try to find the theme based on the name (directory name = theme name).

@lsmith77
Copy link
Contributor

I do agree that dynamic loading of themes is a legitimate use case.
Can you open a PR with the code, doc and test changes to add this?

@oleg-andreyev
Copy link
Contributor

@JHGitty now it makes sense, I'm also +1 for idea.

@ghost
Copy link
Author

ghost commented Dec 23, 2014

Assetic problems in combination with dynamically add/enable themes.

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "_assetic_xxxxxxx_x" as such route does not exist.") in (bla)/Resources/themes/blue/layout.html.twig at line xxx.

I tried to dynamically add and set the theme with an event listener for kernel.controller

public function onKernelController(FilterControllerEvent $event) {
    /* @var Controller $controller */
    $controller = $event->getController();

    // ...

    if ($controller[0] instanceof BaseController) {
      // Set theme
      $activeTheme = $this->container->get('liip_theme.active_theme');
      $activeTheme->setThemes(array('blue'));
      $activeTheme->setName('blue');
    }
}

If I add and enable the theme in config.yml this problem seems to be fixed. But I need to dynamically add and enable themes.

This problem appears because I use an eventlistener which will be called after TemplateResourcesPass (https://github.com/liip/LiipThemeBundle/blob/master/DependencyInjection/Compiler/TemplateResourcesPass.php)

Same problem as Issue #25.

How should I set the theme with PHP to avoid this problem?

@ghost ghost changed the title Add theme to theme-list with PHP Dynamic loading of themes / Enable and add themes with PHP Dec 23, 2014
@ghost ghost changed the title Dynamic loading of themes / Enable and add themes with PHP Dynamic loading of themes / Enable & add themes with PHP Dec 23, 2014
@lsmith77
Copy link
Contributor

long time since I worked on that part of the code. we "inject" the theme directories into assetic here from my reading:
https://github.com/liip/LiipThemeBundle/blob/master/DependencyInjection/Compiler/TemplateResourcesPass.php#L68

now I am not sure if the assetic API allows for dynamically adding more directories but I doubt that this would be at all useful, since assetic's CLI dump command would then never know about those directories. meaning you would also have to do something to the dump process.

in the end, I would say its easier to just keep assetic resource introduction out of theme templates.

@lsmith77
Copy link
Contributor

lsmith77 commented May 9, 2015

ping

@ghost
Copy link
Author

ghost commented May 9, 2015

I am not working on this. I just created the issue. I hope anybody else will do it.

@lsmith77 lsmith77 added this to the 1.3.0 milestone May 10, 2015
@lsmith77 lsmith77 modified the milestones: 1.3.0, 1.4.0 Aug 28, 2015
@dbu dbu removed this from the 1.4.0 milestone Oct 16, 2015
@KoriSeng
Copy link

KoriSeng commented Dec 8, 2017

    public function prepend(ContainerBuilder $container)
    {
        $bundles = $container->getParameter('kernel.bundles');

        if (isset($bundles['LiipThemeBundle'])) {
            $themes = [];
            $finder = new Finder();
            foreach($finder->path('/\/Resources\/themes\/.*$/')->directories()->in($container->getParameter('kernel.root_dir').'/..') as $directory)
            {
                if(strstr($directory->getPath(), "Tests") === false)
                {
                    if(!in_array($directory->getFilename(), $themes))
                        $themes[] = $directory->getFilename();
                }
            }
            $config = [
                "assetic_integration" => true,
                "themes" => $themes,
                "active_theme" => 'default',
                'path_patterns' => [
                    'app_resource' => [
                        __DIR__.'/../Resources/themes/default/%%template%%'
                    ]
                ]
            ];

            $container->prependExtensionConfig("liip_theme", $config);

        }

    }

Actually i made the bundle dynamically search for the themes.

removal of the marked bracket will detect the foobar theme from tests.

@lsmith77
Copy link
Contributor

lsmith77 commented Dec 8, 2017

feel free to submit this as a PR .. I guess it would be configured as a single or multiple glob statements?

@KoriSeng
Copy link

KoriSeng commented Dec 9, 2017

sorry i just realised that i missed a chunk of code.
no, this just detect the themes folder and builds the array.

eg: ["theme1","theme2", "theme3"]

after which it will set the configuration of liip_theme:themes as the array after which the system will kick in and do the rest as follows

given that that this will dynamically fetch all the theme name and build the array there are a couple of options.
1: remove the configuration of liip_themes:theme and use finder to locate the themes:
2: add on to it, check if finder bundle exists, if it does and liip_themes:theme is not configured detect and build the array

which one would you like to see implemented?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants