Adding HTML to Drupal closure?

2019-03-31 04:40发布

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?

9条回答
小情绪 Triste *
2楼-- · 2019-03-31 05:02

...or, probably not as recommended as the other answers, but more direct, you can add the html straight to the page.tpl.php file.

查看更多
We Are One
3楼-- · 2019-03-31 05:03

I dont' recommend using block , as its overkill for just adding some elements on the footer. Here is the hook that you can use to add the content before </body> tag for Drupal 7 .

function hook_page_build(&$page) {
  $page['page_bottom']['YOUR_MODULE'] = array(
    '#markup' => '<div> I want to get inserted at the bootom of the page </div>',
  );
} 
查看更多
小情绪 Triste *
4楼-- · 2019-03-31 05:05

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

查看更多
一夜七次
5楼-- · 2019-03-31 05:05

As hook_footer did not survive Drupal 6, here's the equivalent function and how to implement it in Drupal 7:

/**
* Implements hook_page_alter().
*/
function example_page_alter(&$page) {
  if (variable_get('dev_query', 0)) {
    $page['page_bottom']['devel']= array(
      '#type' => 'markup',
      '#markup' => '<div style="clear:both;">' . devel_query_table() . '</div>',
    );
  }
}

(From http://preprocess.me/drupal-hookfooter-in-drupal-7)

查看更多
虎瘦雄心在
6楼-- · 2019-03-31 05:12

Drupal has a hook you can implement and add in whatever code you'd like to the footer of the page - hook_footer. See http://api.drupal.org/api/function/hook_footer/6

查看更多
仙女界的扛把子
7楼-- · 2019-03-31 05:15

You can add the following to your theme's template.php file:

  
function phptemplate_preprocess_page(&$vars) {
  $vars['closure'] .= 'Add markup here';
}
  
查看更多
登录 后发表回答