2
votes

I'd like to render the contents of a "basic page" in drupal. Something like this question: displaying a Drupal view without a page template around it but for Drupal 7.

My attempt almost works:

function mytheme_preprocess_page(&$variables, $hook) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
        $variables['theme_hook_suggestions'][] = 'page__ajax';
  }  
}

And have a file named page--ajax.tpl.php in the same directory where template.php lives:

<?php print $page['content']; ?>

The problem is that it still renders the menu and my two custom blocks from the sidebar. I only want the page content. What should I change?

2

2 Answers

6
votes

You are almost there. The only thing you need is to add a custom HTML wrapper template.

  • Add a function to template.php:
function THEMENAME_preprocess_html(&$variables, $hook) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
    $variables['theme_hook_suggestions'][] = 'html__ajax';
  }
}
  • Create a file named html--ajax.tpl.php
<!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">

<head>
  <?php print $styles; ?>
  <?php print $scripts; ?>
</head>
<body class="<?php print $classes; ?>">
  <?php print $page_top; ?>
  <?php print $page; ?>
  <?php print $page_bottom; ?>
</body>
</html>
  • Flush all caches. That's all.
4
votes

Based on the answer of Ufonion Labs I was able to completely remove all the HTML output around the page content in Drupal 7 by implementing both hook_preprocess_page and hook_preprocess_html in my themes template.php, like this:

function MY_THEME_preprocess_page(&$variables) {
  if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
    $variables['theme_hook_suggestions'][] = 'page__embed';
  }
}

function MY_THEME_preprocess_html(&$variables) {
  if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
    $variables['theme_hook_suggestions'][] = 'html__embed';
  }
}

Then I added two templates to my theme: html--embed.tpl.php:

<?php print $page; ?>

and page--embed.tpl.php:

<?php print render($page['content']); ?>

Now when I open a node page, such as http://example.com/node/3, I see the complete page as usual, but when I add the response_type parameter, such as http://example.com/node/3?response_type=embed, I only get the <div> with the page contents so it can be embedded in another page.

Shamelessly taken form here : displaying a Drupal view without a page template around it (second best answer for drupal 7).

Alexei solution still use the page template wich is in charge of displaying blocks