Search code examples
drupal-7drupal-6closuresfooter

Adding HTML to Drupal closure?


To add javascript, you can use:

drupal_add_js

And similar for css:

drupal_add_css

But what if I just want to add html at the end of my page. I.e. Add a div with some text in it at the end of the page?


Solution

  • A lot of suggestions here work if you want your alteration to be theme-based, but if you want this to come from a module, using a block region, page template or page prepocess won't cut it, because you're tying the alteration to the theme.

    From a modular standpoint, the following options are best:

    hook_footer() -- http://api.drupal.org/api/function/hook_footer/6 :: my_module_footer() should return a string of HTML, which can even be javascript. E.g.

    function my_module_footer(){
    
        return "<script type='text/javascript'>//your script</script>"
    
    }
    

    You can use drupal $_GET['q'] to get the page URL and have that return be conditional for the page URL.

    Otherwise, I sometimes use $GLOBALS["_add_my_footer"] as a boolean, and set it at various points in the module, then in hook_footer(), see if it is true.


    drupal_add_js -- api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6 :: You can use this to add javascript to the footer by setting the $scope parameter to "footer" -- e.g.

    function my_module_init(){
    
        $script = "your script "; // notice no <script> tags
        drupal_add_js($script, 'inline', 'footer');
        // you can also use this function to grab a .js file
    
    }
    

    Note that I put that drupal_add_js into hook_init() -- I did that because if you don't, the script could get lost when using Drupal's aggressive caching feature.

    Unfortunately, there is no $scope parameter for drupal_add_css