2

I have found How can I programmatically get the theme name?, which works for the current user, but would I apply such logic to learn what theme another user would use?

I can get the active theme for current user and the default theme by using:

$my_theme = \Drupal::theme()->getActiveTheme();
$default_theme = \Drupal::config('system.theme')->get('default');

I was hoping there would be something like \Drupal::theme()->getActiveTheme($other_user); to discover what theme another user would be using.

Is such a thing even possible and how?

UPDATE

To clear things up, I am attempting to figure out what theme was negotiated for a certain user. This could be the global default theme, but it could also be the theme that was negotiated based on a certain role, user selected theme, or any other contrib theme negotiator that might be in place.

So the better question would be, how can I discover what theme a user would be presented if he visits the site (no specific page)?

2

As far as I remember, this type of functionality was built in in Drupal 6.

In Drupal 7 there was the ThemeKey module .

Unfortunately, as of this date ThemeKey is not yet ported to Drupal 8, however I think you can achieve pretty easy that functionality by using the user data object inside custom module.

In order get something from the user data object:

$data = \Drupal::service('user.data')->get('mymodule', \Drupal::currentUser()->id(), 'key');

In order to set something in the user data object:

\Drupal::service('user.data')->set('mymodule', \Drupal::currentUser()->id(), 'key', $value);

Update:

you can combine the code above with the solution posted here:

https://www.drupal8.ovh/en/tutoriels/259/theme-negotiators-switch-theme-dynamically

where your negotiator will be called last by adding priority in the services.yml

you can implement the business logic inside the function the function determineActiveTheme. Combine this with hook_user_login where the information is stored inside users_data and you have a working solution (If I don't have a mistake somewhere).

  • Thanks, but it is a little more complicated, hence the question. Drupal 8 uses theme negotiators that may negotiate a theme based on an order or negotiators that each may or may not apply. So just checking for a theme key might not be enough as it might be overruled by a role based negotiator and so on. I was hoping to fetch the final theme after all these criteria. – Neograph734 Mar 27 '17 at 8:13
  • @Neograph734 Indeed, but you need to provide context to those negotiators for them to do anything. What context do you want to provide? Just URL or something else too? – Clive Mar 27 '17 at 14:47
  • @Clive, I'd like to get the default theme for any other user (provide the user as context). If I have to provide a URL for that, it would probably be the home page. – Neograph734 Mar 27 '17 at 16:08
  • What do you mean by "default" though, that wouldn't be covered by this answer? Their default theme is either the default theme, or the one they've chosen, right? Unless you want the result to be based on some context like current route, current device, etc, a la themekey. Have I misunderstood? – Clive Mar 27 '17 at 16:50
  • @Clive, I meant their negotiated theme; the theme they see when they access the site (not a particular page). This might be the default theme, might be what they have chosen, but can also be the theme that any other contrib negotiator might have put in between. – Neograph734 Mar 27 '17 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.