Drupal 7 Overridding HTML Output Based on URL Conditions

Drupal 7 supports downloadable sitewide themes. Sometimes we want a specific site path to exclude many regions of a site and present only the page content. Or for instance if a remote site is fetching the page contents and embedding it in their site, we would not want to show all the banners, sidebars etc on the page. In the below example a client website can request any page of the website with an additional url parameter &remoteembed and the page output will be stripped of banners, header, footer and sidebars for remote embedding into another website.

We can use the Drupal Theme API to modify page output to show only page content we want. We will utilize an override to template_preprocess_html() and provide a theme suggestion to inform drupal of an additional HTML template for a pages of a given content type and a custom body class to provide a mechanism to target these pages via CSS if need be. We also provide a custom hook_preprocess_page() to strip nested content within our $page content.

function MYTHEME_preprocess_html(&$variables) {
  if (isset($_GET['remoteembed'])) {
    $variables['classes_array'][] = 'remoteembed_page';
    $variables['theme_hook_suggestions'][] = 'html__remoteembed';
  }
  if (strpos(drupal_get_path_alias(), 'newsletters') === 0) {
    $variables['classes_array'][] = 'custom_newsletter_page';
    $variables['theme_hook_suggestions'][] = 'html__newsletter';
  }
}

function MYTHEME_preprocess_page(&$variables, $hook) {
  if (isset($_GET['remoteembed'])) {
    unset($page['sidebar_first'], $page['sidebar_second'], $page['header'], $page['footer']);
    $variables['theme_hook_suggestions'][] = 'page__remoteembed';
  }
  if (strpos(drupal_get_path_alias(), 'newsletters') === 0) {
    unset($page['sidebar_first'], $page['sidebar_second'], $page['header'], $page['footer']);
    $variables['theme_hook_suggestions'][] = 'page__newsletter';
  }
}

We then create the theme file in question, essentially copying html.tpl.php from your parent theme and removing extra markup we don’t need for each template file.

<?php
// In html-remoteembed.tpl.php or html-newsletter.tpl.php

/* This is a copy of html.tpl.php without extra visual page content being rendered.*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language; ?>" version="XHTML+RDFa 1.0" dir="<?php print $language->dir; ?>"<?php print $rdf_namespaces; ?>>

<head profile="<?php print $grddl_profile; ?>">
<?php print $head; ?>
<title><?php print $head_title; ?></title>
<?php print $styles; ?>
<?php print $scripts; ?>
</head>
<body class="<?php print $classes; ?>" <?php print $attributes;?>>
<?php print $page; ?>
</body>
</html>

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


5 × 2 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>