Adding classes with a hook¶
Other modules can add their own classes by implementing hook_browserclass_classes(). The hook
receives the lowercase user agent of the current request and returns an array of class names.
The returned classes are added to the result of browserclass_get_classes() and to the
[browserclass:browser-classes] and [browserclass:hook-classes] tokens.
Note
Hook classes are not added to the <body> tag. The body classes come from the module's
JavaScript, which does not call PHP hooks.
Example¶
Add a silk class for the Amazon Silk browser, in mymodule.module:
/**
* Implements hook_browserclass_classes().
*/
function mymodule_browserclass_classes($agent) {
$classes = [];
if (strpos($agent, 'silk') !== FALSE) {
$classes[] = 'silk';
}
return $classes;
}
On Drupal 11.1 and newer you can implement it as an attribute-based hook instead:
namespace Drupal\mymodule\Hook;
use Drupal\Core\Hook\Attribute\Hook;
class BrowserClassHooks {
#[Hook('browserclass_classes')]
public function browserclassClasses(string $agent): array {
return str_contains($agent, 'silk') ? ['silk'] : [];
}
}
Return an empty array when nothing matches. The full definition is in browserclass.api.php.