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

如何从Drupal模块获取RAW HTML代码?查找可用钩子无果

获取Drupal页面原始HTML的可行方案

Absolutely, this is totally feasible in Drupal! The "View Page Source" output you're referring to is the fully rendered final HTML response sent to the browser, and while there's no out-of-the-box hook specifically for grabbing this directly, you can tap into Drupal's request/response system to capture it. Here are a couple of practical approaches:

1. 自定义模块函数捕获页面原始HTML

You can create a custom function in a custom module that simulates a request for the target page and retrieves the full raw HTML response. This works exactly like how the browser receives the page source.

use Symfony\Component\HttpFoundation\Request;

/**
 * Retrieves raw HTML content for a given Drupal path.
 *
 * @param string $path
 *   The internal path of the page (e.g., '/node/1', '/about').
 *
 * @return string
 *   The full raw HTML of the requested page.
 */
function mycustommodule_get_raw_page_html($path) {
  // Create a request object for the target path
  $request = Request::create($path);
  
  // Pass the request through Drupal's HTTP kernel to get the full response
  $response = \Drupal::service('http_kernel')->handle($request);
  
  // Extract and return the raw HTML content
  return $response->getContent();
}

You can call this function from a custom controller, Drush command, or even a hook (just be cautious about performance if using it in frequent requests).

2. 自定义Drush命令快速获取

If you prefer command-line access, building a custom Drush command makes it easy to pull raw HTML for any page on demand:

/**
 * Implements hook_drush_command().
 */
function mycustommodule_drush_command() {
  $commands['get-raw-html'] = [
    'description' => 'Fetch the raw HTML source of a Drupal page.',
    'arguments' => [
      'path' => 'The internal path of the page (e.g., "/node/1").',
    ],
    'examples' => [
      'drush get-raw-html /node/1' => 'Print raw HTML for node 1 to the console.',
      'drush get-raw-html /about > about-page.html' => 'Save raw HTML to a file.',
    ],
  ];
  return $commands;
}

/**
 * Callback for the drush get-raw-html command.
 */
function drush_mycustommodule_get_raw_html($path) {
  $request = Request::create($path);
  $response = \Drupal::service('http_kernel')->handle($request);
  
  // Print the raw HTML directly to the console
  drush_print($response->getContent());
}

After enabling your custom module, just run the command and you'll get the exact same HTML as you see in "View Page Source".

Key Notes

  • Permissions: Make sure the user executing the code (whether web user or Drush user) has access to the target page—otherwise you'll get the HTML for a 403 Access Denied page.
  • Caching: This will return the cached version of the page if caching is enabled, which matches what the browser receives.
  • Performance: Each call to this function processes a full page request, so avoid using it in high-traffic parts of your site to prevent server load issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:17:41