Tokens¶
The module provides tokens that return the same kind of classes as a space-separated string,
detected on the server from the request's User-Agent header. Use them anywhere Drupal replaces
tokens, for example in modules that accept tokens in their settings.
| Token | Returns | Example |
|---|---|---|
[browserclass:browser-classes] |
All classes | chrome chrome140 win desktop |
[browserclass:browser] |
Browser classes | chrome chrome140 |
[browserclass:platform] |
Platform class | win |
[browserclass:device] |
Device class | desktop |
[browserclass:hook-classes] |
Classes added by other modules through the hook | |
[site:browserclass] |
All classes, same as [browserclass:browser-classes] |
|
[user:browserclass] |
All classes, same as [browserclass:browser-classes] |
The site and user tokens can be chained, for example [site:browserclass:platform] or
[user:browserclass:device].
[user:browserclass] always describes the browser making the current request. It does not depend
on which user account the token is rendered for.
Differences from the body classes¶
The tokens use a PHP implementation of the detection, which differs from the JavaScript one in a few ways:
- One platform only. PHP stops at the first platform that matches, in this order:
win,ipad,ipod,iphone,mac,android,linux, then the rest. An iPhone getsiphone, and an Android phone getsandroid, without the extramacorlinuxthe body classes include. - Hook classes.
[browserclass:browser-classes]includes classes from other modules. The body classes do not. - WAP headers. PHP also counts a request as
mobilewhen it carries anX-Wap-ProfileorProfileheader. [browserclass:device]on its own. This token runs the device check without the platform classes, so it only looks at the user agent strings listed under Device. Android tablets usually have nomobilein their user agent, so they getdesktopfrom this token andmobilefrom[browserclass:browser-classes].
Caching¶
Warning
The tokens add no cache context, so Drupal does not vary cached output by user agent. If a token ends up in a cached page or render cache entry, every later visitor sees the classes of the browser that filled the cache. Use the tokens in output that is built per request and not cached, such as email bodies, or use the body classes for styling.