Sometimes we need to use different designs for separate pages or for separate menu items in a site.The most obvious way to perform this task is to set separate templates for each case in the current theme and customize CSS and JS for these pages.But there is another way, which is a less complex front-end solution. We can use a different theme.
Step for Switch theme dynamically in drupal 8 , using theme negotiators
Step-1 : Create a custom module (here : example_theme_switcher)
Step-2 : Define the custom ThemeNegotiator service
The only way Drupal is going to know that this module provides this custom code is for us to tell it so.Since this is a service, this definition goes in .
On example_theme_switcher/example_theme_switcher.services.yml:
Add:
services:
theme.negotiator.example_theme_switcher:
class: Drupal\example_theme_switcher\Theme\ExampleThemeSwitcherNegotiator
tags:
- { name: theme_negotiator, priority: 10 }
Step-3: Extend the ThemeNegotiator service class
ExampleThemeSwitcherNegotiator,
which is placed in the example_theme_switcher/src/Theme directory with the filename ExampleThemeSwitcherNegotiator.php. You'll need to be sure to include an opening <?php tag with the following:
<?php
/**
* @file
* Contains \Drupal\example_theme_switcher\Theme\ExampleThemeSwitcherNegotiator.
*/
namespace Drupal\example_theme_switcher\Theme;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Theme\ThemeNegotiatorInterface;
class ExampleThemeSwitcherNegotiator implements ThemeNegotiatorInterface {
public function applies(RouteMatchInterface $route_match) {
$applies = FALSE;
$uc = FALSE;
if(isset($_GET['content'])){
if($_GET['content']=='uc'){
$uc = true;
}
}elseif (isset($_SERVER['HTTP_X_CUA']) && $uc==false) {
if ($_SERVER['HTTP_X_CUA'] == 'mobile_version')
{
$applies = TRUE;
}
}elseif(isset($_SERVER['HTTP_IS_DESKTOP'])) {
if ($_SERVER['HTTP_IS_DESKTOP'] == 'desktop_version') {
$applies = FALSE;
}
}
if(isset($_GET['desktop'])){
$applies = FALSE;
}elseif(isset($_GET['theme'])){
$applies = TRUE;
}
return $applies;
}
/**
* {@inheritdoc}
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
return 'custom_theme_name';
}
}
?>
No comments:
Post a Comment