Adding HTML to Drupal closure?

2019-03-31 04:32发布

问题:

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?

回答1:

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



回答2:

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)



回答3:

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

  
function phptemplate_preprocess_page(&$vars) {
  $vars['closure'] .= 'Add markup here';
}
  


回答4:

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



回答5:

You could write a block to do it and put the block in the closure.



回答6:

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



回答7:

There have already beed made some good suggestions:

  • Using a block
  • Editing templates
  • Using preprocess function.

The easiest thing would be to add a block that you create, where you can put your custom markup. You could even create a special template for it, so you don't get your usual drupal block markup, but only what you write in the block content.

That should cover your needs:

  • You have 100% control over where the markup is generated (You can define the region in your them)
  • You have 100% control over what markup is generated

Now if you don't want to use blocks, because you don't want the site admins to know about the block, the 2nd best thing would be:

  1. Add a checkbox to your theme settings.
  2. In your preprocess page hook, set a boolean variable if the checkbox is checked or not.
  3. In your page template, you check for the variable, if it's TRUE you write your markup where you want it.

This will basically do the same thing as above, only you have the setting in your theme, instead of adding a block. It will require more work.

You can do pretty much the same in a custom module, but it wont make much sense to do this in a module, since it's purely presentation and you are dependent on your theme anyways.



回答8:

here is a little trick that works in drupal 6 and 7

    $script = '//--><!]]></script><strong>this is bold html code</strong><script type="text/javascript"><!--//--><![CDATA[//><!--';
    drupal_add_js($script, 'inline', 'footer');

since it is apparent you don't want to modify the template files or use any of the other methods suggested above, this should work to add html to the page just like it does for js and css



回答9:

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>',
  );
}