Insert code right after body tag using theme funct

2019-07-15 08:55发布

问题:

I am trying to add a piece of code at the begining of every page in a Drupal site. Since I have more than one page template, I want to do this programatically... but am not succeeding.

I am still new and, though I get the gist of hooks, theme functions, and the such, I just can't figure the correct way to achieve this.

So far I've overriden the theme_preprocess_page(&$vars) to add the necessary css and js:

function mytheme_preprocess_page(&$vars) {
    if(condition) {
        drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
    }
}

How can I now add html code in every drupal page, preferably just after the opening bodytag or in any other starting section, via a function in the template.phpfile?

Thank you

回答1:

In your preprocess function, any variable set, will be available in your page.tpl.php file.

function mytheme_preprocess_page(&$vars) {
  if (condition) {
    $vars['foo'] = '<div id="bar">TEST</div>';
  }
}

then, in your page templates:

<body>
  <?php print !empty($foo) ? $foo : ''; ?>
...


回答2:

This should keep you from having to modify any code in your templates:

function MY_MODULE_page_build(&$page)
{
    $page['page_bottom']['my-markup'] = array('#markup' => '<div>My Markup Here</div>');
}


回答3:

Had a look at https://www.drupal.org/project/google_tag. This is how they did it:

/**
 * Implements hook_page_alter().
 *
 * Adds a post_render callback
 */
function MYMODULE_page_alter(&$page) {
  $page['#post_render'][] = 'MYMODULE_CALLBACK';
}

/**
 * Implements callback_post_render().
 *
 * Inserts JavaScript snippet immediately following the opening body tag.
 */
function MYMODULE_CALLBACK(&$children, $elements) {
  $script = '<script type="text/javascript">console.log(\'hello world\');</script>';
  // Insert snippet after the opening body tag.
  $children = preg_replace('@<body[^>]*>@', '$0' . $script, $children, 1);
  return $children;
}


回答4:

My approach finally was overriding the first rendered block in the page, in my case the Language Switcher. Since I already was overriding it to customize it, it wasn't too much of a big deal, but it is anyway an ugly way to achieve that.

<?php
function mytheme_languageswitcher($links) {
    // inserting this here like a virus in a dna strip
    if(condition) {
        drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
    }
    // the *real* code for the language switcher override
}
?>

Since the Language Switcher is rendered in every page, it works. The day the language switcher stops being displayed for whatever reason, this solution will FAIL.



回答5:

You can add it to the footer with an option passed into the drupal_add_js function.

function THEMENAME_preprocess_page(&$variables) {
   if (condition) {
      drupal_add_js(drupal_get_path('theme', 'THEMENAME') . '/PATH/TO/FILE.js', array('scope' => 'footer'));
   }
}

This will end up printing just before the closing body tag via the $page_bottom variable in the template html.tpl.php.



回答6:

Another way to do it is to use the native drupal methods drupal_add_js and drupal_get_js.

// first, add your JS with a custom "scope" (before process phase)

<?php
drupal_add_js($tag_js, array(
  'type' => 'inline',
  'group' => JS_GROUP_TAGS,
  'every_page' => TRUE,
  'scope' => 'body_start',
  'weight' => 1,
)); 
?>

// ugly way : add the drupal_get_js directly into html.tpl.php (but for testing, it's useful):

<body>
<?php print drupal_get_js('body_start'); ?>
...
</body>

// A cleaner way : use an intermediate variable, in a process method // Exactly like template_process_html, in theme.inc

<?php
function MYMODULEORTHEME_process_html(&$variables){
  $variables['js_body_start'] .= drupal_get_js('body_start');
}
?>

Enjoy :)