Theme not being loaded


#1
Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? no
Sylius version 1.3

Hi … I’m currently trying to create a custom theme after reading the official documentation on how to do it. I created an EventListener that I registered in my Kernel so that when the site loads I can select which theme I want to display. The problem is that the new theme is not registering itself in the Sylius system, I’m using the full stack Sylius version, not a Symfony install that uses Sylius’ bundles.

These are the files:
services.yml:

    Redbridge\EventListener\ThemeRequestListener:
        public: true
        autowire: true
        autoconfigure: true
        class: 'Redbridge\EventListener\ThemeRequestListener'
        arguments:
          - '@sylius.repository.theme'
          - '@sylius.theme.context.settable'
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

src/EventListener/ThemeRequestListener.php:

<?php

namespace Redbridge\EventListener;

use Sylius\Bundle\ThemeBundle\Context\SettableThemeContext;
use Sylius\Bundle\ThemeBundle\Repository\InMemoryThemeRepository;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class ThemeRequestListener
{
    /**
     * @var ThemeRepositoryInterface
     */
    private $themeRepository;

    /**
     * @var SettableThemeContext
     */
    private $themeContext;

    /**
     * @param ThemeRepositoryInterface $themeRepository
     * @param SettableThemeContext $themeContext
     */
    public function __construct( ThemeRepositoryInterface $themeRepository, SettableThemeContext $themeContext )
    {
        $this->themeRepository = $themeRepository;
        $this->themeContext = $themeContext;
    }

    /**
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
            // don't do anything if it's not the master request
            return;
        }

        $this->themeContext->setTheme( $this->themeRepository->findOneByName('first_template') );
    }
}

app/themes/MyNewTheme/composer.json:

{
  "name": "first_template",
  "title": "A first Redbridge Template",
  "description": "A first Redbridge Template",
  "authors": [
    {
      "name": "Roberto Espi",
      "email": "teratux@gmail.com",
      "homepage": "http://kamil.kokot.me",
      "role": "Developer"
    }
  ]
}

config.yml:

sylius_theme:
    sources:
        filesystem: ~

When I run the command: $ bin/console sylius:theme:list I get:
There are no themes.

I posted this already in GitHub and was advised to post here and to add the sylius-theme in the theme’s composer.json file.

I already added that section, cleared the cache once more and still nothing.

Any help would be appreciated
Thanks …


#2

Interesting, I managed to solve the problem making changes in the order I included my services. This is the original

    - { resource: "@SyliusCoreBundle/Resources/config/app/config.yml" }
    - { resource: "@SyliusAdminBundle/Resources/config/app/config.yml" }
    - { resource: "@SyliusShopBundle/Resources/config/app/config.yml" }
    - { resource: "@SyliusAdminApiBundle/Resources/config/app/config.yml" }
    - { resource: "services.yml" }
    - { resource: "parameters.yml" }
    - { resource: "security.yml" }

and the changed config.yml:

    - { resource: "services.yml" }
    - { resource: "@SyliusCoreBundle/Resources/config/app/config.yml" }
    - { resource: "@SyliusAdminBundle/Resources/config/app/config.yml" }
    - { resource: "@SyliusShopBundle/Resources/config/app/config.yml" }
    - { resource: "@SyliusAdminApiBundle/Resources/config/app/config.yml" }
    - { resource: "parameters.yml" }
    - { resource: "security.yml" }

Now Sylius complained that the name for the themes don’t match the regex expression for their title. Changed them, and all done. Any idea on what happened here?


#3

It happens when you have a glitch in the backend coding. Follow the simple rules given on https://babasupport.org/apple/iphone-error-4005/ to avoid and remove these small bugs.