Drupal 7 create an iframe from a node for embedding

Here is the following situation: You have a content type in Drupal 7 that will be embedded in other websites, so now you have to have a cleaned up version of it.This means no sidebars and some regions may need to be hidden in the iframe version.
The path in the following example for the iframe will be:

node/%nid/iframe

I  limit the iframe version of the pages for a certain content type only (SCHOOL).

Here is what to do:

  1. Your theme’s template.php should check for the path, then:
  • Clean up page_bottom page_top regions in preprocess_html
  • Add CSS classes to the body so you can CSS hide/show elements
  • Add custom page template suggestion
<?php

/**
* Implements hook_preprocess_html().
*/
function YOURTHEME_preprocess_html(&$vars) {
  $item = menu_get_item();
  if ($item['path'] == 'node/%/iframe') {
    // cleanup regions
    $vars['page']['page_top'] = "";
    $vars['page']['page_bottom'] = "";
    // Add CSS class
    $vars['classes_array'][] = 'node-type-SCHOOL';
  }
}

/**
 * Implements hook_preprocess_page().
 */
function YOURTHEME_preprocess_page(&$vars) {
  $item = menu_get_item();
  if ($item['path'] == 'node/%/iframe') {
    $node = node_load(arg(1));
    if ($node->type == 'SCHOOL') {
      // page template for SCHOOL iframe page.
      $vars['theme_hook_suggestions'][] = 'page__SCHOOL_iframe';
    }
  }
}

 

2. Create an iframe freindly page.tpl.php -> page–SCHOOL-iframe.tpl.php and clean it up for the iframe

Cheers!