Problem/Motivation
There is no way to access the default theme for the active domain. In the Domain Theme module, this was achieved by the `domain_theme_lookup` function, however, there is nothing in Domain Theme Switch to provide similar functionality.
One use-case is the Mailsystem module’s domain option for email themes.
How to reproduce
When we try to open /admin/config got the following error:
The website encountered an unexpected error. Please try again later.
TypeError: Argument 4 passed to Drupal\domain_theme_switch\Theme\ThemeSwitchNegotiator::__construct() must be an instance of Drupal\domain_theme_switch\DomainThemeLookup, instance of Drupal\Core\Config\ConfigFactory given, called in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 281 in Drupal\domain_theme_switch\Theme\ThemeSwitchNegotiator->__construct() (line 72 of modules/contrib/domain_theme_switch/src/Theme/ThemeSwitchNegotiator.php).
Drupal\domain_theme_switch\Theme\ThemeSwitchNegotiator->__construct(Object, Object, Object, Object) (Line: 281)
Drupal\Component\DependencyInjection\Container->createService(Array, 'theme.negotiator.domain_theme_switch') (Line: 173)
Drupal\Component\DependencyInjection\Container->get('theme.negotiator.domain_theme_switch') (Line: 20)
Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition('theme.negotiator.domain_theme_switch') (Line: 65)
Drupal\Core\Theme\ThemeNegotiator->determineActiveTheme(Object) (Line: 405)
Drupal\Core\Theme\ThemeManager->initTheme(Object) (Line: 96)
Drupal\Core\Theme\ThemeManager->getActiveTheme() (Line: 41)
Drupal\domain_config\DomainConfigLibraryDiscoveryCollector->getCid() (Line: 340)
Drupal\Core\Cache\CacheCollector->lazyLoadCache() (Line: 144)
Drupal\Core\Cache\CacheCollector->get('select2') (Line: 44)
Drupal\Core\Asset\LibraryDiscovery->getLibrariesByExtension('select2') (Line: 58)
Drupal\Core\Asset\LibraryDiscovery->getLibraryByName('select2', 'select2.min') (Line: 32)
select2_requirements('runtime')
call_user_func_array('select2_requirements', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('requirements', Array) (Line: 111)
Drupal\system\SystemManager->listRequirements() (Line: 95)
Drupal\system\SystemManager->checkRequirements() (Line: 102)
Drupal\system\Controller\SystemController->overview('system.admin_config')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 91)
Drupal\shield\ShieldMiddleware->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Proposed resolution
I propose adding a getter for the ThemeSwitchNegotiator’s `defaultTheme` variable.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | domain-theme-switch-getDefaultTheme-3014053-17.patch | 5.83 KB | qymanab |
| #17 | interdiff_14-17.txt | 2.34 KB | qymanab |
Comments
Comment #2
josephdpurcell commentedOverall this makes sense to me. I do not see tests, but there are none for the module. I did not manually test this either.
One minor change:
Method should have a title
Comment #3
qymanab commentedComment #4
qymanab commentedComment #5
josephdpurcell commentedThis looks correct and it applies cleanly to the latest branch and I see no code style violations.
Setting to RTBC to get visibility to this proposal. Resolution to this blocks #2941723: Reimplement the domain option in theme settings.
Comment #6
abrammHi,
The defaultTheme is set in applies() method and thus may not be available before this method is called. The variable is only meant to be used internally.
I'd suggest either:
1) Fetching the default theme right from the config:
or
2) Refactoring ThemeSwitchNegotiator. Both defaultTheme and adminTheme properties could be removed. Instead, a new service may be implemented to return default & admin themes.
Comment #7
qymanab commentedChanges:
- adds a DomainThemeLookup service
- refactors ThemeSwitchNegotiator to use the service
Comment #8
qymanab commentedComment #9
josephdpurcell commentedThree requested changes:
Because this module is so lighteweight, I think this can simply be put at Drupal\domain_theme_switch\DomainThemeLookup
Irrelevant change? No need to remove indentation here
Pass this in as a dependency via the __constructor
Comment #10
qymanab commentedComment #11
qymanab commentedComment #12
josephdpurcell commentedOverall this looks good. A few code style things:
Comments end in a period
Perhaps elaborate here--what is the point of this class?
A service that retrieves theme settings based on the domain.
This line is not needed, see https://www.drupal.org/node/1354#var
Consider instead:
$this->config = $config_factory->get('domain_theme_switch.settings'). This prevents needing to get the config bucket with every call in this class
Typically "id" is written as "ID", see https://www.drupal.org/docs/develop/standards/coding-standards#naming
Avoid extra whitespace after opening parenthesis
Avoid unnecessary whitespace
Comment #13
abrammThank you guys! I like the progress on this issue.
The only concern I have is the DomainThemeLookup class; can we please add an interface for it and refer the interface instead? Just in case someone wants to decorate the service.
Comment #14
qymanab commentedComment #15
qymanab commentedComment #16
josephdpurcell commentedThis goes above the @var, see https://www.drupal.org/node/1354#var
Taking code style strictly, this requires a description, see https://www.drupal.org/node/1354#param
And this requires a description, see https://www.drupal.org/node/1354#return
Same
Same
Remove extra whitespace
Comment #17
qymanab commentedComment #18
josephdpurcell commentedThis looks great! I see no code style violations. I did not see tests nor did I manually test.
Setting to RTBC.
Comment #19
abrammThanks for the patch.
I'm a bit busy this week but I'll try to have a look.
Comment #20
vuilComment #21
vuilThe patch #17 works for us. Thanks a lot.
Comment #22
abrammVerified the patch, committing to 8.x-1.x.
Comment #24
abramm