Loading...


Related Products


Date Published: October 11, 2023

Using Drush to render a Drupal page

When troubleshooting a problem with a specific Drupal page, the ability to simulate a request to this page via Drush can be helpful in the debugging process. This how-to explains why and how to render a Drupal page using Drush.

Why would I use Drush to render Drupal pages?

  • PHP errors and warnings that occur during the rendering process are output directly to the command-line in real time, obviating the need to search through your sites logs for these errors.
  • Server variables involved in controlling the request's output can be easily manipulated for additional control over Drupal's behavior when rendering the page.
  • Rendering a page via command-line can provide a workaround for debugging Drupal application issues that result in network performance problems (e.g: HTTP timeouts) that might otherwise impact your ability to render pages with a standard HTTP request.

How do I use Drush to render a Drupal page?

Rendering a Drupal page via Drush requires the use of Drush's php-eval command to execute the page rendering mechanism found in Drupal's index.php file. The command relies on PHP's global $_SERVER variables to gather information about the type of request you're making. Helpfully, Drush automatically populates several of the key server variables used by Drupal for this purpose, and allows you to control them via Drush's command-line options. For example:

  • Drush's --uri=[full request URL] option allows you to populate the HTTP host, request URI, querystring, port, and all other variables that might be populated by the URL of an incoming web request. The value of this option tells Drupal which page you're trying to render.
  • Drush's --user=[uid] option (older Drush versions only) allows you to simulate a request as a specific Drupal user. By default, Drupal will use the anonymous user (i.e: --user=0) for this purpose.

Additionally, you assign values directly to $_SERVER, $_GET and $_POST and other PHP superglobals in the body of the php-eval command itself, prior to executing the code that renders the page.

At their most basic, the Drush commands you'd use to render a page on your Drupal site are as follows:

For Drupal 7:

drush --user=0 --uri=http://www.example.com php-eval 'menu_execute_active_handler("path/to/page");'

For Drupal 9 and higher:

drush --uri=www.example.com ev '
  // EDIT THIS User ID that you want to masquerade the page as. E.g. 1 would (normally) be the superuser.
  $user_id = 0; 
  // EDIT THIS Path you want. ONLY the path like /filter/tips or /myview/something
  $path = "path/to/page";
  
  // Do not edit code below --------------------
  global $config; $config["system.logging"]["error_level"] = "verbose";
  // Switch the user.
  assert($this instanceof \Drush\Commands\core\PhpCommands);
  $switcher = \Drupal::getContainer()->get("account_switcher");
  $switcher->switchTo(\Drupal\user\Entity\User::load($user_id));
  // Create the sub-request
  $request = \Symfony\Component\HttpFoundation\Request::create($path);
  // Get the response.
  $response = \Drupal::getContainer()
    ->get("http_kernel")
    ->handle($request);  
  // Output the results
  // Headers...
  echo "RESULT HEADERS=====\n\n"; 
  print_r($response->headers->all());
  // HTML...
  echo "RESULTING HTML======\n\n";
  $response->send();
  // Cache tags...
  if (method_exists($response, "getCacheableMetadata")) { 
    echo "\n\nRESULTING CACHE TAGS=====\n\n"; 
    print_r($response->getCacheableMetadata()); 
  }
  else { 
    echo "\n\nNO CACHE TAGS available.\n\n"; 
  }'
 

Both of the commands above will render the page at http://www.example.com/path/to/page for an anonymous user (UID 0). Editing the URL or user, or adding additional PHP code to the php-eval commands being executed above can change the way that Drupal behaves when rendering the page, and may change the output that you receive in response to these commands.

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation