{"id":42297,"date":"2016-11-24T10:58:11","date_gmt":"2016-11-24T05:28:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=42297"},"modified":"2016-12-05T17:24:06","modified_gmt":"2016-12-05T11:54:06","slug":"how-to-change-theme-by-role-in-drupal-8-x","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-change-theme-by-role-in-drupal-8-x\/","title":{"rendered":"How to change theme by Role in Drupal 8.x"},"content":{"rendered":"<p><strong>How to change theme by Role in Drupal 8.x<\/strong><\/p>\n<p><span style=\"font-weight: 400\">Even before I start, I would like to shed some light on what are Themes in Drupal. A theme is a bunch of files that defines the presentation layer. There are many contributed themes in <a title=\"Drupal Development\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\" target=\"_blank\">Drupal<\/a> project that can be found under \u2018Themes\u2019. For a programmer, there are 2 kinds of themes &#8211; Contributed &amp; Custom Themes.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">A contributed theme is what you download from Drupal.org and install in your Drupal package.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">A custom theme is what you build your own either from a base or custom theme.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">So the question arises what\u2019s the relation between roles &amp; themes and why there is a need to switch themes for different roles. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Role in Drupal is used to manage user permissions on the website. A set of permissions is assigned to a role and these roles can be assigned to users. \u00a0So basically, different roles mean\u00a0different permissions related to that role.<\/span><\/p>\n<p><span style=\"font-weight: 400\">This is actually a feature request that arises on demand\/need. Also if you are familiar with <a title=\"Drupal 7 to Drupal 8 Migration Services\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\" target=\"_blank\">Drupal 7\/8<\/a> then you must have seen a\u00a0different theme for admin role (theme name used is seven) &amp; for roles anonymous or authenticated (theme name used is Bartik). Now let\u2019s take an example of having 9-10 different roles on a Drupal site, all are sharing the same theme i.e Bartik. If in such a scenario a need arises to have different themes (layers\/presentations) for different roles, a<\/span>\u00a0user with role A having theme Bartik (Drupal core theme name) and User with Role B needs to have classy (Drupal core theme name) theme. Then, for achieving the above state, the process explained below in the article can be used.<\/p>\n<p><span style=\"font-weight: 400\"> Earlier in Drupal 7, there were only two files i.e module_name.info and module_name.module that were easy to understand and configure. However, with the launch of Drupal 8 \u00a0in November 2015, the architecture is changed from procedural to MVC.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Now, with new PHP architecture in <a title=\"Drupal 8 Development Services\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\" target=\"_blank\">Drupal 8<\/a> which is based on symphony allows us to learn new things. For Instance, the new twig system in theming\u00a0restricts writing custom PHP scripts in templates which was earlier possible with Drupal 7. <\/span><\/p>\n<p><span style=\"font-weight: 400\">OOPs concepts, classes etc.have also been introduced in Drupal 8.\u00a0<\/span><span style=\"font-size: 1rem\">However to develop a new module or theme in Drupal. There are some key points that a developer should keep in mind.<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400\"> There are many contributed modules\/themes that are present in Drupal. So, there is no need to create a new one. Also, t<\/span>here is a module that is present for only Drupal 7 version but, not for Drupal 8. And also contributing to open source platforms like Drupal enhances your company&#8217;s profile on Drupal.org.<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400\"> What was the process in Drupal 7 ?\u00a0<\/span>The approach to changing theme dynamically on runtime needs to change completely as the architecture of Drupal 8 has changed a lot. Also, the hook system will be dropped entirely in Drupal 9. So, keeping in mind this whole thing. There is a need to search a new approach on Drupal.org. &#8216;<strong>Change in Records<\/strong>&#8216;\u00a0<span style=\"font-weight: 400\">is where you can explore the changes in previous versions and the new things introduced.<\/span><\/li>\n<\/ol>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400\"> Searching in <\/span><b>Change in Records:\u00a0<\/b>By searching for new changes in Drupal 8, I found some useful links like <b>hook_custom_theme<\/b><span style=\"font-weight: 400\"> is <\/span><b>replaced by theme callback. Now, that I had a starting point so, I began my search for theme negotiators<\/b><span style=\"font-weight: 400\">. Theme Negotiators are responsible for any alterations on themes.<\/span><\/li>\n<\/ol>\n<p><b>CODE:<\/b><\/p>\n<ol>\n<li><span style=\"font-weight: 400\"> Build the module architecture first<\/span><\/li>\n<li><span style=\"font-weight: 400\"> Create a module folder inside ROOT\/modules\/custom\/role_based_theme_switcher<\/span><\/li>\n<li><span style=\"font-weight: 400\"> Create a role_based_theme_switcher.info.yml file, the contents in the file are as follows:<\/span><\/li>\n<\/ol>\n<p>[java]<br \/>\n\tname: Role Based Theme Switcher<br \/>\n\tdescription: This module is for switching the theme according to Role<br \/>\n\tpackage: Custom<\/p>\n<p>\ttype: module<br \/>\n\tcore: 8.x<br \/>\n\t[\/java]<\/p>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400\"> Create role_based_theme_switcher.services.yml file:<\/span><\/li>\n<\/ol>\n<p>[java]services:<br \/>\n    theme.negotiator.role_based_theme_switcher:<br \/>\n\tclass: Drupal\\role_based_theme_switcher\\Theme\\RoleNegotiator<br \/>\n\ttags:<br \/>\n  \t&#8211; { name: theme_negotiator, priority: 10 }[\/java]<\/p>\n<p>If you want to provide the form settings with the configuration you should follow step 3 &amp; 4 otherwise skip.<\/p>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400\"> Create routing file the location will be:<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">ROOT\/modules\/role_based_theme_switcher\/role_based_theme_switcher.routing.yml<\/span><\/p>\n<p>[java]<br \/>\n    role_based_theme_switcher.settings:<br \/>\n\tpath: &#8216;\/admin\/structure\/role_based_theme_switcher\/settings&#8217;<br \/>\n\tdefaults:<br \/>\n  \t_form: &#8216;\\Drupal\\role_based_theme_switcher\\Form\\AdminSettingsForm&#8217;<br \/>\n  \t_title: &#8216;URL Setting&#8217;<br \/>\n\trequirements:<br \/>\n  \t_permission: &#8216;administer site configuration&#8217;<br \/>\n[\/java]<\/p>\n<ol start=\"4\">\n<li><span style=\"font-weight: 400\"> Define your configuration file, which needs to be placed at the following location:<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">ROOT\/modules\/role_based_theme_switcher\/config\/install\/role_based_theme_switcher.RoleBasedThemeSwitchConfig.yml<\/span><\/p>\n<p><span style=\"font-weight: 400\">You can define your own configuration that we will be using for storing the form information submitted by the user.<\/span><\/p>\n<p>Themeroll<strong>:<\/strong><\/p>\n<p>Remember you can define any name within the config file like:<\/p>\n<p>role_based_theme_switcher.config:<\/p>\n<p>5. Create a configuration form in admin:<\/p>\n<p>The for file should be placed at a specific location.<br \/>\nROOT\/modules\/role_based_theme_switcher\/src\/Form\/AdminSettingsForm.php<\/p>\n<p>Code:<\/p>\n<p>[java]<br \/>\n&lt;?php<\/p>\n<p>namespace Drupal\\role_based_theme_switcher\\Form;<\/p>\n<p>use Drupal\\Core\\Form\\FormBase;<br \/>\nuse Drupal\\Core\\Form\\ConfigFormBase;<br \/>\nuse Drupal\\Core\\Form\\FormStateInterface;<br \/>\nuse Drupal\\user\\Entity;<br \/>\nuse Drupal\\user\\Entity\\Role;<br \/>\nuse Drupal\\Core\\Extension\\ThemeHandler;<br \/>\nuse Symfony\\Component\\DependencyInjection\\ContainerInterface;<\/p>\n<p>\/**<br \/>\n * Configure Role Based settings for this site.<br \/>\n *<br \/>\n * @category Role_Based_Theme_Switcher<br \/>\n *<br \/>\n * @package Role_Based_Theme_Switcher<br \/>\n *<br \/>\n * @author pen &lt;author details&gt;<br \/>\n *<br \/>\n * @link https:\/\/www.drupal.org\/sandbox\/pen\/2760771 description<br \/>\n *\/<br \/>\nclass AdminSettingsForm extends ConfigFormBase {<br \/>\n  protected $themeGlobal;<br \/>\n \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public function __construct(ThemeHandler $themeGlobal) {<br \/>\n  $this-&gt;themeGlobal = $themeGlobal;<br \/>\n  }<br \/>\n  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public static function create(ContainerInterface $container) {<br \/>\n\treturn new static(<br \/>\n      $container-&gt;get(&#8216;theme_handler&#8217;)<br \/>\n\t);<br \/>\n  }<br \/>\n  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public function getFormId() {<br \/>\n\treturn &#8216;role_admin_settings&#8217;;<br \/>\n  }<\/p>\n<p>  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  protected function getEditableConfigNames() {<br \/>\n\treturn [<br \/>\n      &#8216;role_based_theme_switcher.RoleBasedThemeSwitchConfig&#8217;,<br \/>\n\t];<br \/>\n  }<\/p>\n<p>  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *<br \/>\n   * Implements admin settings form.<br \/>\n   *<br \/>\n   * @param array $form<br \/>\n   *   From render array.<br \/>\n   * @param \\Drupal\\Core\\Form\\FormStateInterface $form_state<br \/>\n   *   Current state of form.<br \/>\n   *\/<br \/>\n  public function buildForm(array $form, FormStateInterface $form_state) {<br \/>\n\t$themes = $this-&gt;themeGlobal-&gt;listInfo();<br \/>\n\t$themeNames = array(&#8221; =&gt; &#8216;&#8211;Select&#8211;&#8216;);<br \/>\n\tforeach ($themes as $key =&gt; $value) {<br \/>\n      $themeNames[$key] = $key;<br \/>\n\t}<br \/>\n\t$roles = Role::loadMultiple();<br \/>\n\t$roleThemes = $this-&gt;config(&#8216;role_based_theme_switcher.RoleBasedThemeSwitchConfig&#8217;);<br \/>\n    $form[&#8216;role_theme&#8217;] = array(<br \/>\n  \t&#8216;#type&#8217; =&gt; &#8216;table&#8217;,<br \/>\n  \t&#8216;#header&#8217; =&gt; array(<br \/>\n    \tt(&#8216;Label&#8217;),<br \/>\n    \tt(&#8216;Themes List&#8217;),<br \/>\n    \tt(&#8216;Weight&#8217;),<br \/>\n  \t),<br \/>\n  \t&#8216;#empty&#8217; =&gt; t(&#8216;There are no items yet. Add an item.&#8217;, array()),<br \/>\n  \t\/\/ TableDrag: Each array value is a list of callback arguments for<br \/>\n  \t\/\/ drupal_add_tabledrag(). The #id of the table is automatically prepended;<br \/>\n  \t\/\/ if there is none, an HTML ID is auto-generated.<br \/>\n  \t&#8216;#tabledrag&#8217; =&gt; array(<br \/>\n    \tarray(<br \/>\n      \t&#8216;action&#8217; =&gt; &#8216;order&#8217;,<br \/>\n          &#8216;relationship&#8217; =&gt; &#8216;sibling&#8217;,<br \/>\n      \t&#8216;group&#8217; =&gt; &#8216;role_theme-order-weight&#8217;,<br \/>\n    \t),<br \/>\n  \t),<br \/>\n\t);<br \/>\n\tif (!empty($roleThemes-&gt;get(&#8216;roletheme&#8217;))) {<br \/>\n  \t$roleThemes = $roleThemes-&gt;get(&#8216;roletheme&#8217;);<br \/>\n  \tif (count($roleThemes) == count($roles)) {<br \/>\n    \t$roles = $roleThemes;<br \/>\n  \t}<br \/>\n  \telseif (count($roleThemes) != count($roles)) {<br \/>\n    \tforeach ($roles as $rem_key =&gt; $rem_val) {<br \/>\n      \tif (!array_key_exists($rem_key, $roleThemes)) {<br \/>\n            $merge[$rem_key] = [&#8216;id&#8217; =&gt; &#8221;, &#8216;weight&#8217; =&gt; 10];<br \/>\n            $roleThemes = array_merge($roleThemes, $merge);<br \/>\n      \t}<br \/>\n    \t}<br \/>\n    \t$roles = $roleThemes;<br \/>\n  \t}<br \/>\n\t}<br \/>\n\telse {<br \/>\n  \t$roleThemes = [];<br \/>\n  \t$i = 0;<br \/>\n  \tforeach ($roles as $roles_key =&gt; $roles_val) {<br \/>\n    \tif ($roles_key == &#8216;administrator&#8217;) {<br \/>\n          $roleThemes[$roles_key][&#8216;weight&#8217;] = $i;<br \/>\n          $roleThemes[$roles_key][&#8216;id&#8217;] = &#8216;seven&#8217;;<br \/>\n      \t$i++;<br \/>\n    \t}<br \/>\n    \telse {<br \/>\n          $roleThemes[$roles_key][&#8216;weight&#8217;] = $i;<br \/>\n          $roleThemes[$roles_key][&#8216;id&#8217;] = &#8216;bartik&#8217;;<br \/>\n      \t$i++;<br \/>\n    \t}<br \/>\n  \t}<br \/>\n  \t$roles = $roleThemes;<br \/>\n\t}<br \/>\n\t\/\/ Build the table rows and columns.<br \/>\n\tforeach ($roles as $id =&gt; $entity) {<br \/>\n  \t\/\/ TableDrag: Mark the table row as draggable.<br \/>\n      $form[&#8216;role_theme&#8217;][$id][&#8216;#attributes&#8217;][&#8216;class&#8217;][] = &#8216;draggable&#8217;;<br \/>\n  \t\/\/ TableDrag: Sort the table row according to its existing\/configured weight.<\/p>\n<p>  \t\/\/ Some table columns containing raw markup.<br \/>\n      $form[&#8216;role_theme&#8217;][$id][&#8216;label&#8217;] = array(<br \/>\n    \t&#8216;#plain_text&#8217; =&gt; ucfirst($id) . t(&#8216; user&#8217;),<br \/>\n  \t);<\/p>\n<p>      $form[&#8216;role_theme&#8217;][$id][&#8216;id&#8217;] = array(<br \/>\n    \t&#8216;#type&#8217; =&gt; &#8216;select&#8217;,<br \/>\n    \t&#8216;#title&#8217; =&gt; t(&#8216;Select Theme&#8217;),<br \/>\n    \t&#8216;#options&#8217; =&gt; $themeNames,<br \/>\n        &#8216;#default_value&#8217; =&gt; (string) $roleThemes[$id][&#8216;id&#8217;],<br \/>\n  \t);<br \/>\n  \t\/\/ TableDrag: Weight column element.<br \/>\n      $form[&#8216;role_theme&#8217;][$id][&#8216;weight&#8217;] = array(<br \/>\n        &#8216;#type&#8217; =&gt; &#8216;weight&#8217;,<br \/>\n        &#8216;#title_display&#8217; =&gt; &#8216;invisible&#8217;,<br \/>\n        &#8216;#default_value&#8217; =&gt; (int) $roleThemes[$id][&#8216;weight&#8217;],<br \/>\n    \t\/\/ Classify the weight element for #tabledrag.<br \/>\n    \t&#8216;#attributes&#8217; =&gt; array(&#8216;class&#8217; =&gt; array(&#8216;role_theme-order-weight&#8217;)),<br \/>\n  \t);<br \/>\n\t}<br \/>\n\t$form[&#8216;actions&#8217;] = array(&#8216;#type&#8217; =&gt; &#8216;actions&#8217;);<br \/>\n    $form[&#8216;actions&#8217;][&#8216;submit&#8217;] = array(<br \/>\n  \t&#8216;#type&#8217; =&gt; &#8216;submit&#8217;,<br \/>\n  \t&#8216;#value&#8217; =&gt; t(&#8216;Save changes&#8217;),<br \/>\n\t);<\/p>\n<p>\treturn parent::buildForm($form, $form_state);<br \/>\n  }<\/p>\n<p>  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public function validateForm(array &amp;$form, FormStateInterface $form_state) {<br \/>\n\t$rollTheme = $form_state-&gt;getValue(&#8216;role_theme&#8217;);<br \/>\n\tforeach ($rollTheme as $key =&gt; $value) {<br \/>\n  \tif (in_array((int) $value[&#8216;weight&#8217;], $role_arr)) {<br \/>\n        $form_state-&gt;setErrorByName(&#8216;role_theme&#8217;, $this-&gt;t(&quot;Thers is an error in the form.&quot;));<br \/>\n  \t}<br \/>\n  \t$role_arr[$key] = (int) $value[&#8216;weight&#8217;];<br \/>\n\t}<br \/>\n  }<\/p>\n<p>  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public function submitForm(array &amp;$form, FormStateInterface $form_state) {<br \/>\n\t$rollTheme = $form_state-&gt;getValue(&#8216;role_theme&#8217;);<br \/>\n\tforeach ($rollTheme as $key =&gt; $value) {<br \/>\n  \t$role_arr[(int) $value[&#8216;weight&#8217;]] = [&#8216;theme&#8217; =&gt; $value[&#8216;id&#8217;], &#8216;role&#8217; =&gt; $key];<\/p>\n<p>  \t$roles[] = $key;<br \/>\n\t}<\/p>\n<p>\tksort($role_arr);<\/p>\n<p>\tforeach ($role_arr as $new_key =&gt; $new_value) {<\/p>\n<p>  \tif (in_array($new_value[&#8216;role&#8217;], $roles)) {<br \/>\n        $roll_array[$new_value[&#8216;role&#8217;]] = [&#8216;id&#8217; =&gt; $new_value[&#8216;theme&#8217;], &#8216;weight&#8217; =&gt; $new_key];<br \/>\n  \t}<br \/>\n\t}<br \/>\n\t$roleThemes = $this-&gt;config(&#8216;role_based_theme_switcher.RoleBasedThemeSwitchConfig&#8217;);<br \/>\n    $roleThemes-&gt;set(&#8216;roletheme&#8217;, $roll_array);<br \/>\n    $roleThemes-&gt;save();<br \/>\n    drupal_set_message(&quot;Role theme configuration saved succefully&quot;);<br \/>\n\t\/\/ clearing cache for anonymous users.<br \/>\n    drupal_flush_all_caches();<br \/>\n  }<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>Remember all the themes loaded above are dynamically loaded by using services &#8220;theme_handler&#8221; and dependency injection through a variable &#8220;protected $themeGlobal;&#8221;. This is important to note that loading services using defense injection is a good practice instead of loading them statically for ex:<\/p>\n<p><span style=\"font-weight: 400\">Static method:<\/span><\/p>\n<p><span style=\"font-weight: 400\">\\Drupal::service(&#8216;theme_handler&#8217;)-&gt;listInfo();<\/span><\/p>\n<p><span style=\"font-weight: 400\">Dynamic method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>[java]<br \/>\n  protected $themeGlobal;<br \/>\n  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public function __construct(ThemeHandler $themeGlobal) {<br \/>\n  $this-&gt;themeGlobal = $themeGlobal;<br \/>\n  }<br \/>\n  \/**<br \/>\n   * {@inheritdoc}<br \/>\n   *\/<br \/>\n  public static function create(ContainerInterface $container) {<br \/>\n\treturn new static(<br \/>\n      $container-&gt;get(&#8216;theme_handler&#8217;)<br \/>\n\t);<br \/>\n  }<br \/>\n[\/java]<\/p>\n<p>6. As now Drupal 8 follows PSR4 standard the files needs to be placed in specific location.<\/p>\n<p>So, RoleNegotiator.php file should goto ROOT\/modules\/custom\/role_based_theme_switcher\/Theme\/RoleNegotiator.php<\/p>\n<p>[java]<br \/>\n&lt;?php<\/p>\n<p>namespace Drupal\\role_based_theme_switcher\\Theme;<\/p>\n<p>use Drupal\\Core\\Routing\\RouteMatchInterface;<br \/>\nuse Drupal\\Core\\Theme\\ThemeNegotiatorInterface;<\/p>\n<p>\/**<br \/>\n * Sets the active theme on admin pages.<br \/>\n *\/<br \/>\nclass RoleNegotiator implements ThemeNegotiatorInterface {<\/p>\n<p>  \/\/ Protected theme variable to set the default theme if no theme is selected.<br \/>\n  protected $theme = NULL;<\/p>\n<p>  \/**<br \/>\n   * Whether this theme negotiator should be used to set the theme.<br \/>\n   *<br \/>\n   * @param \\Drupal\\Core\\Routing\\RouteMatchInterface $route_match<br \/>\n   *   The current route match object.<br \/>\n   *<br \/>\n   * @return bool<br \/>\n   *   TRUE if this negotiator should be used or FALSE to let other negotiators<br \/>\n   *   decide.<br \/>\n   *\/<br \/>\n  public function applies(RouteMatchInterface $route_match) {<br \/>\n  \/\/ Use this theme on a certain route.<br \/>\n  $change_theme = TRUE;<br \/>\n  $route = \\Drupal::routeMatch()-&gt;getRouteObject();<br \/>\n  $is_admin_route = \\Drupal::service(&#8216;router.admin_context&#8217;)-&gt;isAdminRoute($route);<br \/>\n  $current_user = \\Drupal::currentUser();<br \/>\n  $user_roles = $current_user-&gt;getRoles();<br \/>\n  $has_admin_role = FALSE;<br \/>\n  if (in_array(&quot;administrator&quot;, $user_roles)) {<br \/>\n    $has_admin_role = TRUE;<br \/>\n  }<br \/>\n  if ($is_admin_route === TRUE &amp;&amp; $has_admin_role === TRUE) {<br \/>\n    $change_theme = FALSE;<br \/>\n  }<br \/>\n  \/\/ Here you return the actual theme name.<br \/>\n  $roleThemes = \\Drupal::config(&#8216;role_based_theme_switcher.RoleBasedThemeSwitchConfig&#8217;)-&gt;get(&#8216;roletheme&#8217;);<br \/>\n  \/\/ Get current roles a user has.<br \/>\n  $roles = \\Drupal::currentUser()-&gt;getRoles();<br \/>\n  \/\/ Get highest role.<br \/>\n  $theme_role = $this-&gt;getPriorityRole($roles);<br \/>\n  $this-&gt;theme = $roleThemes[$theme_role][&#8216;id&#8217;];<\/p>\n<p>  return $change_theme;<br \/>\n  }<\/p>\n<p>  \/**<br \/>\n   * Determine the active theme for the request.<br \/>\n   *<br \/>\n   * @param \\Drupal\\Core\\Routing\\RouteMatchInterface $route_match<br \/>\n   *   The current route match object.<br \/>\n   *<br \/>\n   * @return string|null<br \/>\n   *   The name of the theme, or NULL if other negotiators, like the configured<br \/>\n   *   default one, should be used instead.<br \/>\n   *\/<br \/>\n  public function determineActiveTheme(RouteMatchInterface $route_match) {<br \/>\n  return $this-&gt;theme;<br \/>\n  }<\/p>\n<p>  \/**<br \/>\n   * Function to get roles array and return highest priority role.<br \/>\n   *<br \/>\n   * @param array $roles<br \/>\n   *   Array of roles.<br \/>\n   *<br \/>\n   * @return string $theme<br \/>\n   *   Return role.<br \/>\n   *\/<br \/>\n  public function getPriorityRole($roles) {<br \/>\n  $roleThemes = \\Drupal::config(&#8216;role_based_theme_switcher.RoleBasedThemeSwitchConfig&#8217;);<br \/>\n  foreach ($roleThemes-&gt;get(&#8216;roletheme&#8217;) as $key =&gt; $value) {<br \/>\n    if (in_array($key, $roles)) {<br \/>\n      $theme[$key] = $value[&#8216;weight&#8217;];<br \/>\n    }<br \/>\n  }<br \/>\n  $theme = array_search(max($theme), $theme);<br \/>\n  return $theme;<br \/>\n  }<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>Last for creating a link in admin configuration you can include a role_based_theme_switcher.links.menu.yml file at ROOT\/modules\/role_based_theme_switcher\/ location:<br \/>\ncode:<br \/>\nLast for creating a link in admin configuration you can include a role_based_theme_switcher.links.menu.yml file at ROOT\/modules\/role_based_theme_switcher\/ location:<br \/>\ncode:<\/p>\n<p>[java]<br \/>\nrole_based_theme_switcher.role_theme:<br \/>\ntitle: Role Theme<br \/>\nroute_name: role_based_theme_switcher.settings<br \/>\ndescription: &#8216;Manage theme for roles.&#8217;<br \/>\nparent: system.admin_config_system<br \/>\nweight: 10<br \/>\n[\/java]<\/p>\n<p><b>Notes: After completing the file structure and code development don\u2019t forget to enable the module under \u201cabc.com\/admin\/modules\u201d and clear Drupal\u2019s cache \u201cabc.com\/admin\/config\/performance\u201d.<\/b><\/p>\n<p><b>Conclusion:<\/b><\/p>\n<p><b>\u201cabc.com\u201d is an example website path.<\/b><\/p>\n<p>In the above code, there is a scenario of how to create a module, a route, a service, a form with all functionality needed to change the Theme. You can change the logic in <b>RoleNegotiator.php<\/b> according to your needs i.e changing of themes per user or by menu URLs or by any other logic.\u00a0If the requirements &amp; form fields are different then, code may be modified in <b>AdminSettingsForm.php<\/b>. Instead of using Database storage we have used Drupal 8 new feature, <b>Configuration<\/b>. All the details filled in <b>AdminSettingsForm.php<\/b> will be saved in configuration &amp; same can be retrieved with writing a database query. The code in the file role_based_theme_switcher.links.menu.yml will provide a link under system configuration under <b>abc.com\/admin\/config<\/b> with a name\u00a0<b>Role Theme<\/b> which will redirect to <b>abc.com\/admin\/structure\/role_based_theme_switcher\/settings<\/b> path you can find the form for selecting different themes for different roles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to change theme by Role in Drupal 8.x Even before I start, I would like to shed some light on what are Themes in Drupal. A theme is a bunch of files that defines the presentation layer. There are many contributed themes in Drupal project that can be found under \u2018Themes\u2019. For a programmer, [&hellip;]<\/p>\n","protected":false},"author":933,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":44},"categories":[3602,1],"tags":[4229,4862,3707,3601,3738,4228,4227],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42297"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/933"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=42297"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42297\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=42297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=42297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=42297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}