You are here

function hook_js_captured_content_alter in JS Callback Handler 8.3

Same name and namespace in other branches
  1. 7.2 js.api.php \hook_js_captured_content_alter()

Allows modules to alter delivery content with captured (printed) output.

In some rare instances, some code uses "printable" functions (print, print_r, echo, var_dump, etc.) that outputs directly to the browser. This causes issues when data is encoded and compressed in the delivery callback (e.g. gzip compresses the data returned from the callback, but is appended with the printed output as well, thus resulting in a decoding error in the browser).

Instead of just discarding this captured data, this alter hook allows loaded modules (defined by the callback) a chance to alter the delivery content right before it's sent to the browser; in the event that the captured output is useful for some reason.

Parameters

string $captured_content: The captured content.

\Drupal\js\JsResponse $response: A JsResponse based object.

\Drupal\js\Js $js: The JS Callback Handler instance.

1 invocation of hook_js_captured_content_alter()
Js::deliver in src/Js.php
Sends content to the browser via the delivery callback.

File

./js.api.php, line 34
JS Callback Handler APIs.

Code

function hook_js_captured_content_alter($captured_content, JsResponse $response, Js $js) {

  // Pass the captured output to the JSON array right before delivery.
  $json = $response
    ->getData();
  $json['captured'] = Xss::filterAdmin($captured_content);
  $response
    ->setData($json);
}