2

Trying to hook into Drupal 7 from an external php file by using various variants of ...

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;

However, it seems D7 just spits out the HTML to the browser on the menu_execute_active_handler() call.

Any ideas how to get the html output into my variable?

1 Answer 1

1

Tracking the Drupal flow line by line and function by function reveals the culprit to be ob_flush in the drupal_page_footer function

Using the following instead of ob_flush would make the application a bit more friendly

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

Expanding on this,

Finally solved by adding a delivery callback to my module_menu function and handling things there to bypass the problem function.

/**
 * 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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.