使用主题函数在 body 标记后插入代码

发布于 2024-11-01 09:51:40 字数 556 浏览 0 评论 0原文

我正在尝试在 Drupal 站点的每个页面的开头添加一段代码。 由于我有多个 page 模板,我想以编程方式执行此操作...但没有成功。

我还是个新手,虽然我了解了钩子、主题函数等的要点,但我只是想不出实现这一目标的正确方法。

到目前为止,我已经覆盖了 theme_preprocess_page(&$vars) 以添加必要的 css 和 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,最好是在打开 body 之后标签或任何其他起始部分,通过template.php文件中的函数?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恬淡成诗 2024-11-08 09:51:40

在您的预处理函数中,任何变量集都将在您的 page.tpl.php 文件中可用。

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

然后,在您的页面模板中:

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

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 : ''; ?>
...
·深蓝 2024-11-08 09:51:40

查看https://www.drupal.org/project/google_tag。他们是这样做的:

/**
 * 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;
}

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;
}
对你再特殊 2024-11-08 09:51:40

这应该使您不必修改模板中的任何代码:

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

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>');
}
黑白记忆 2024-11-08 09:51:40

我的方法最终是覆盖页面中的第一个渲染块,在我的例子中是语言切换器。
由于我已经重写它来自定义它,所以这没什么大不了的,但无论如何这是实现这一目标的一种丑陋的方式。

<?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
}
?>

由于语言切换器在每个页面中都会呈现,因此它可以工作。无论出于何种原因,当语言切换器停止显示的那一天,此解决方案将失败。

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.

画中仙 2024-11-08 09:51:40

您可以将其添加到页脚,并将选项传递到 drupal_add_js 函数。

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

这将通过模板 html.tpl.php 中的 $page_bottom 变量在结束正文标记之前打印。

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.

深海夜未眠 2024-11-08 09:51:40

另一种方法是使用本机 drupal 方法 drupal_add_jsdrupal_get_js

// 首先,添加带有自定义“范围”的 JS(在处理阶段之前)

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

// 丑陋的方式:将 drupal_get_js 直接添加到 html.tpl.php 中(但是对于测试,很有用):

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

// 更简洁的方法:在流程方法中使用中间变量
// 与 theme.inc 中的 template_process_html 完全相同,

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

享受吧:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文