Search code examples
phphtmldrupaldrupal-7drupal-theming

Add id attribute to body tag in drupal


I'm trying to add an id to the <body> tag in my drupal theme. Currently I am doing it in the following manner:

PHP

function myTheme_preprocess_html(&$variables) {
    $variables['bodyId'] = $variables['is_front'] ? 'id="page-AnaSayfa"' : 'id="page"';
}

HTML:

<body class="<?php print $classes;?>" <?php print $attributes;?> <?php print $bodyId;?>>

I am wondering is there a way to do it using the $attributes variable? So far what I have tried with it didn't work which led me to use the $bodyId I have now.

Thanks for your help.


Solution

  • I figured it out.

    Change it from :

    function myTheme_preprocess_html(&$variables) {
        $variables['bodyId'] = $variables['is_front'] ? 'id="page-AnaSayfa"' : 'id="page"';
    }
    

    To:

    function sat7turk_preprocess_html(&$variables) {
        $variables['attributes_array']['id'] = $variables['is_front'] ? 'page-AnaSayfa' : 'page';
    }