Including another page in your node (or another page)

Last modified: October 6, 2006 - 01:36

PLEASE NOTE! The following snippet is user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

When you visit a page in drupal the "center" part is what is generated from the path be it a calendar, a contact form, a view of a node or many other things. So what do you do if you want to include another pages content, say a calendar in your page? The snippet that follows shows how you can do this from a node, the same code could be used in other contexts.

menu_execute_active_handler() is responsible for executing the code associated with the current path. With a little bit of code you can have it do this for the path of your chosing. Small warning, pre 4.7 the callbacks did there own call to theme('page', ...) while in 4.7 they can (are suppose to?) return just the part they generate. The old style still works when visting a URL but will not work the way you want from the snippet. The point is many paths will work correctly but you may find some that do not.

To try this out create a node of type page.
Add a title and paste the following code in the body

This is the leading text before the calendar
<?php
// We save and restore the real path for the page
$q = $_GET['q'];
// Change event to the path you want, this is the part after q= (or after the base path if you have clean URLs enabled)
// The call to drupal_get_normal() allows for using path aliases.
$_GET['q'] = drupal_get_normal_path('event');
// Get and print the output for just the content associated with the path
// If you see the complete page within a page it means the path callback is probably calling theme('page', ....)
print menu_execute_active_handler();
// Restore the real path for the page
$_GET['q'] = $q;
?>

And this is the footer

Set the input format to 'PHP code' and submit.

Note that if instead of the expected content you see a '2' it means the path was not found (not valid) and a '3' means access was denied.

Problems with title & breadcrumbs

tyswan - March 20, 2008 - 03:35

Maybe it's just me, but I always have problems with the page title and breadcrumbs being overwritten.

I use the modified code to save and restore the title & breadcrumbs...

<?php
// We save and restore the real path, title & breadcrumbs for the page
   
$q = $_GET['q'];
   
$title = drupal_get_title();
   
$crumbs = drupal_get_breadcrumb();

// Change event to the path you want, this is the part after q= (or after the base path if you have clean URLs enabled)
// The call to drupal_get_normal() allows for using path aliases.
   
$_GET['q'] = drupal_get_normal_path('event');

// Get and print the output for just the content associated with the path
// If you see the complete page within a page it means the path callback is probably calling theme('page', ....)
   
print menu_execute_active_handler();

// Restore the real path, title & breadcrumbs for the page
   
drupal_set_title($title);
   
drupal_set_breadcrumb($crumbs);

   
$_GET['q'] = $q;
?>

--
tys

BLUE MOUNTAINS health & harmony
www.health-harmony.com.au

building an alternative health & spirituality community in the Blue Mountains

 
 

Drupal is a registered trademark of Dries Buytaert.