Захват сгенерированного html-вывода Drupal 7 в переменную


Попытка подключиться к Drupal 7 из внешнего php-файла с использованием различных вариантов...

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
ob_start();
menu_execute_active_handler();
$contents = ob_get_clean();
return $contents;

Однако, похоже, что D7 просто выплевывает HTML в браузер при вызове menu_execute_active_handler().

Есть идеи, как получить вывод html в мою переменную?

 2
Author: kiamlaluno, 2012-09-22

1 answers

Отслеживание потока Drupal по строкам и функции по функциям показывает, что виновником является ob_flush в функции drupal_page_footer

Использование следующего вместо ob_flush сделало бы приложение немного более дружественным

$data = ob_get_contents();
ob_clean();
print $data;

Развивая это,

Окончательно решено путем добавления обратного вызова доставки в мою функцию module_menu и обработки там вещей, чтобы обойти проблемную функцию.

/**
 * Implements hook_menu().
 */
function MODULE-NAME_menu() 
{
    $items = array();
    $items['MODULE-NAME'] = array(
        'page callback'     =>  'MODULE-NAME_output',
        'access arguments'  =>  array('access content'),
        'delivery callback' =>  'MODULE-NAME_deliver_html_page',
        'type'              =>  MENU_CALLBACK
    );

    return $items;
}

/**
 * Page callback: Display MODULE-NAME HTML Output
 *
 * @see MODULE-NAME_menu()
 */
function MODULE-NAME_output() 
{
    return 'Some Output HTML';
}

/**
 * Delivery callback: Render MODULE-NAME Page
 *
 * @see MODULE-NAME_menu()
 */
function MODULE-NAME_deliver_html_page($page_callback_result) {
    /* Adapted from "drupal_deliver_html_page" */
    // Menu status constants are integers; page content is a string or array.
    if (is_int($page_callback_result)) 
    {
        // Integers mean an error page is required! 
        // Send to the default Drupal function and exit.
        // Note use of exit instead of drupal_exit (not needed here).
        drupal_deliver_html_page($page_callback_result);
        exit;
    }
    elseif (isset($page_callback_result)) 
    {
        /* Print anything besides a menu constant if not NULL or undefined. */
        // Send the correct charset HTTP header if code running
        // for this page request has not already set the content type header.
        if (is_null(drupal_get_http_header('Content-Type'))) 
        {
            drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
        }
        // Send appropriate HTTP-Header for browsers and search engines.
        global $language;
        drupal_add_http_header('Content-Language', $language->language);
        // Render page
        print drupal_render_page($page_callback_result);
    }
    /* Adapted from "drupal_page_footer" */
    // Perform end-of-request tasks.
    global $user;
    module_invoke_all('exit');
    // Commit the user session, if needed.
    drupal_session_commit();
    if (variable_get('cache', 0) && ($cache = drupal_page_set_cache())) 
    {
        drupal_serve_page_from_cache($cache);
    }
    else 
    {
        $data = ob_get_contents();
        ob_clean();
        print $data;
    }
    _registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE);
    drupal_cache_system_paths();
    module_implements_write_cache();
    system_run_automated_cron();
}
 1
Author: Dayo, 2012-09-26 17:03:16