You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拦截Drupal 10的BigPipe输出或js-bottom-placeholder?

解决方案(Drupal 10.x)

一、拦截并修改BigPipe sendPlaceholders()生成的JSON输出

完全可行,无需修改核心代码,通过Drupal的hook_ajax_response_alter钩子即可实现,主题或自定义模块均支持:

  1. 在主题的.theme文件中添加钩子实现:
function YOUR_THEME_ajax_response_alter(AjaxResponse $response, array $original_options) {
  // 遍历所有AJAX命令,定位BigPipe相关响应
  foreach ($response->getCommands() as &$command) {
    // 针对BigPipe常用的insert命令修改数据(可根据实际需求调整命令类型)
    if (isset($command['command']) && $command['command'] === 'insert') {
      // 示例:替换命令内容中的指定文本
      $command['data'] = str_replace('旧内容', '新内容', $command['data']);
    }
  }

  // 若需直接修改JSON原始内容(不推荐,优先修改命令对象)
  // $json = $response->getContent();
  // $modified_json = str_replace('原始字符串', '修改后字符串', $json);
  // $response->setContent($modified_json);
}
  • 替换YOUR_THEME为你的主题机器名
  • 优先通过修改AjaxResponse的命令对象实现,更符合Drupal API规范
  • 如需针对特定占位符,可通过$original_options或命令附加参数判断目标占位符ID

二、拦截并修改<js-bottom-placeholder>输出

该占位符由BigPipe在页面初始化时插入,BigPipe不会覆盖你修改后的内容,优先采用主题预处理方案:

方案1:hook_preprocess_html预处理修改

在主题的.theme文件中添加:

function YOUR_THEME_preprocess_html(&$variables) {
  if (isset($variables['page']['#attached']['html_head'])) {
    foreach ($variables['page']['#attached']['html_head'] as &$head_item) {
      if (str_contains($head_item[0]['#markup'], 'js-bottom-placeholder')) {
        // 修改占位符的token或完整标签内容
        $head_item[0]['#markup'] = str_replace('{{ placeholder_token }}', '自定义token', $head_item[0]['#markup']);
        // 或直接替换整个标签
        // $head_item[0]['#markup'] = '<js-bottom-placeholder token="自定义token">';
      }
    }
  }
}

方案2:hook_page_attachments_alter修改附件

若预处理无法定位,可改用附件修改钩子:

function YOUR_THEME_page_attachments_alter(array &$attachments) {
  if (isset($attachments['#attached']['html_head'])) {
    foreach ($attachments['#attached']['html_head'] as &$head_item) {
      if (isset($head_item[0]['#markup']) && str_contains($head_item[0]['#markup'], 'js-bottom-placeholder')) {
        // 执行自定义修改逻辑
        $head_item[0]['#markup'] = '<js-bottom-placeholder token="自定义修改后的token">';
      }
    }
  }
}

注意事项

  • 修改后需清除Drupal缓存才能生效
  • 所有修改均通过钩子/主题实现,避免直接修改核心文件,保证系统可升级性
  • 如需针对特定页面或用户角色修改,可在钩子中添加条件判断(比如通过\Drupal::routeMatch()->getRouteName()判断路由)

内容的提问来源于stack exchange,提问作者stkuser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:05:27