You are here

function amp_deliver_html_page in Accelerated Mobile Pages (AMP) 7

Processes the Full Page HTML through the AMP PHP Library if enabled on the AMP Configuration

NOTE: This function is mainly interested in full page AMP output, for all other cases e.g. menu statuses it passes on the task to drupal_deliver_html_page(). This is for separation of concerns.

See also

drupal_deliver_html_page()

1 string reference to 'amp_deliver_html_page'
amp_page_delivery_callback_alter in ./amp.module
Implements hook_page_delivery_callback_alter().

File

./amp.module, line 1472

Code

function amp_deliver_html_page($page_callback_result) {
  if (is_int($page_callback_result) || !isset($page_callback_result)) {

    // Pass on to drupal_deliver_html_page()
    drupal_deliver_html_page($page_callback_result);
  }
  else {

    // We're not going use drupal_deliver_html_page() from now on. So we'll have to do the tasks it did
    // before finishing i.e. outputting headers, final html output and call to drupal_page_footer() for cleanup.
    // Output http headers like in drupal_deliver_html_page()
    global $language;
    if (is_null(drupal_get_http_header('Content-Type'))) {
      drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
    }
    drupal_add_http_header('Content-Language', $language->language);

    // Render the page as a full html string
    $full_html_unprocessed = drupal_render_page($page_callback_result);

    // First check the config if full html processing is enabled, if not then don't process the html
    if (!variable_get('amp_library_process_full_html', false)) {
      $final_html = $full_html_unprocessed;
    }
    else {

      // Setup of AMP conversion
      $amp = _amp_create_amp_converter();
      $options = array(
        'scope' => Scope::HTML_SCOPE,
      );
      if (variable_get('amp_library_process_statistics', false)) {
        $options += array(
          'add_stats_html_comment' => true,
        );
      }
      $amp
        ->loadHtml($full_html_unprocessed, $options);

      // Get AMP converted html
      $full_html_processed = $amp
        ->convertToAmpHtml();

      // Add warnings to watchdog if enabled
      $request_uri = url(current_path(), array(
        'absolute' => true,
      ));
      $heading = "<h3>AMP PHP Library messages for {$request_uri}</h3>" . PHP_EOL;
      $heading .= 'To disable these notices goto <a href="/admin/config/content/amp">AMP configuration</a> and uncheck ' . '<em>Debugging: Add a notice in the Drupal log...</em>  in the AMP PHP Library Configuration fieldset' . PHP_EOL;
      if (variable_get('amp_library_process_full_html_warnings', false)) {

        // Add any warnings that were generated
        watchdog('amp', "{$heading} <pre>" . $amp
          ->warningsHumanHtml() . '</pre>');
      }
      $final_html = $full_html_processed;
      drupal_alter('amp_final_html', $final_html);
    }

    // Output html to browser
    print $final_html;

    // Cleanup
    drupal_page_footer();
  }
}