9

I am looking for the best way to insert arbitrary HTML content into a page before the closing body tag, similar to the way drupal_add_js() adds JavaScript to the bottom of a page.

I am coming from a Wordpress background, and in Wordpress this could be accomplished by using the wp_footer() hook.

I have looked into using hook_page_alter(), but I was wondering if this was the correct way to do this or if there is a better way of accomplishing this task.

  • 1
    Similar questions like this one pops up every now and then, and I always wonder why, is it necessary to "insert HTML just before the body tag". It seems to me like you're approaching whatever you are doing in the wrong way? – Letharion Jan 8 '12 at 17:33
  • 1
    I can't speak for why others want to do it, but in my case I am adding Underscore JS templates to the page for blocks that require them. However, multiple blocks may utilize one template so I wanted to add all templates to the bottom of the page so that I could easily see if they had been loaded or not. For me it is basically an aesthetics / convenience thing. – drebabels Jan 12 '12 at 15:16
  • Sorry if I'm being slow, but that doesn't make sense to me? With the cache/aggregation/etc benefits of #attached, that can be used on more than forms, I still don't understand the why. – Letharion Jan 12 '12 at 15:33
  • I was not aware of the versatility of #attached, so thank you for introducing it to me. However in my case I am writing the contents of those template files to the page (not just linking to the files), so correct me if I am wrong but I don't think the cache/aggregation benefits of #attached matter in this case because the actual page caching will take care of it. – drebabels Jan 15 '12 at 6:27
23

The equivalent of hook_footer() that was used in Drupal 6 is to implement hook_page_build(), and alter the content of $page['page_bottom'], using code similar to the following one:

function mymodule_page_build(&$page) {
  $page['page_bottom']['devel'] = array(
    '#weight' => 25, 
    '#markup' => '<div style="clear:both;">' . devel_query_table() . '</div>',
  );
}

The content of $page['page_bottom'] is output from html.tpl.php, using the following code:

  <?php print $page_top; ?>
  <?php print $page; ?>
  <?php print $page_bottom; ?>
</body>
</html>

The value of $page_bottom is initialized from template_process_html() using the following code:

  // Render page_top and page_bottom into top level variables.
  $variables['page_top'] = drupal_render($variables['page']['page_top']);
  $variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
  // Place the rendered HTML for the page body into a top level variable.
  $variables['page'] = $variables['page']['#children'];
  $variables['page_bottom'] .= drupal_get_js('footer');

The difference between hook_page_alter() and hook_page_build() is that hook_page_alter() is executed after hook_page_build() and should be used to alter, or remove content added by another module, while hook_page_build() should be used to add new content to the page.

  • Thank you for going above and beyond with this answer. This is very helpful. – drebabels Jan 12 '12 at 15:18

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.