By David - Posted on 31 July 2008

One interesting method of making a home page that offers a mix of easy editing of static fields, plus the ability to embed blocks and other dynamic content, is by making your front page using CCK. This might be a good choice if what you're after is an easily editable set of pre-defined fields (which would, for instance, make it easy for your non-tech-savvy client to edit basic static information without coding anything) with additional dynamic content drawn in through embedded Regions/blocks or Views.

Tips on setting up a "Home" content type

  1. Create a new Content Type at Administer > Content management > Content types and clicking the "Add content type" link.
  2. To best follow along with this guide, set the Name to Home, and Type to home.
  3. If your home page will be more advanced, making use of custom fields, you may want to disable the Body field and just use custom fields. To do this simply delete the text Body from the Body field label setting. If the label is empty, the Body field will not be included.
  4. Click on the "Workflow Settings" fieldset and uncheck Promoted to front page and set the Attachments option to Disabled.
  5. Click on the "Comment settings" fieldset and set the Default comment setting to Disabled.
  6. By default, Content Types have a "Submitted by" line printed at the top. Most likely you want to get rid of this though in the case of your home page. To do so, go to Administer > Site building > Themes > Configure (admin/build/themes/settings) and under the Display post information on option, uncheck Home. Scroll to the bottom of the page and click "Submit configuration".
  7. In most themes the $title variable is in the page.tpl.php file. On your home page you may not want to display the title though. If you don't want the title on the home page, the you can set it to never display on the home page of the site:
    • Open page.tpl.php and find the line that says:
      <h1 class="title"><?php print $title; ?></h1> (or something similar, containing $title).
    • Adjust it similar to this:
      <?php if (!$is_front) : ?>
        <h1 class="title"><?php print $title; ?></h1>
      <?php endif; ?>

Set up permissions

You can set up permissions so that only the admin (user 1) ever sees "Home" as an available content type on the Create Content page. Of course you only want "one" Home page, so it doesn't make sense to be shown the option to make more Home pages.

  1. Go to Administer > User management > Permissions (admin/user/permissions) and uncheck "create home content" and "delete any home content" and "delete own home content" for every Role. Only the admin user will ever see the option to create the initial Home content node.
  2. For any Role that is allowed to edit the Home page, enable the "edit any home content" option. The admin user will still see the option to make new Home pages, however you should not use the admin account on a day-to-day basis (create a "site admin" role instead), so you should rarely if ever see this option.

[note: will migrate the below section about embedding Blocks, Regions, and Views into template files to a separate dedicated article, and reference it from this page]

Embedding dynamic content into node-home.tpl.php

There are a variety of ways to easily embed dynamic content into your node-home.tpl.php file.

Embedding a Block

The following uses the default Search block as an example, to illustrate the technique for embedding any Block:

<?php
  $block
= module_invoke('search', 'block', 'view', 0);
  print
$block['subject'];
  print
$block['content'];
?>

There are just 2 parts to the above code that you need to customize in order to display any particular block, which are highlighted in the sample below:
$block = module_invoke('search', 'block', 'view', 0);

  1. Blocks are uniquely identified by the combination of "module name", "block id" (relative to module), and "theme name".
  2. Go to Administer > Site building > Blocks (admin/build/block) and hover your mouse pointer over the "configure" link for the block you want. You will see the "module name" and "block id" at the end of the URL.
  3. So in the case of the Search block in the above example, the URL is:
    example.com/admin/build/block/configure/search/0 ... the "module name"
    example.com/admin/build/block/configure/search/0 ... the "block id"

Embedding a custom Region

You can embed any default or custom Region (populated with as many Blocks as you wish, which you can control and arrange at Administer > Site building > Blocks).

When you add custom Regions to your theme you have to include the default regions as well or they will no longer be used (you can, however leave out any that you don't want to keep). Add as many custom Regions as you wish. The following is for Drupal 6.

First add the Region(s) to your ThemeName.info file:

regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer
regions[home_custom] = Custom home region name

Then you can use the following code in node-home.tpl.php:

<?php print theme('blocks', 'home_custom'); ?>

Embedding a View

You can embed a View if it has a Block display using the Region method above. However it's also very easy to embed a View directly into the node-home.tpl.php file. (I need to check if this is true, but I believe a benefit of directly embedding a View instead of defining extra Regions is that it has better performance for your site). Here is info on embedding Views with Drupal 5. The following illustrates how to embed a View using Views 2 with Drupal 6 (more advanced options are also available):

<?php
  $view
= views_get_view('viewname');
  print
$view->preview('default');
?>

Where viewname is the name of your View, and default can be changed to the page_# or block_# of any Display in your View. For instance, to embed the first Block Display defined in the View, you could use block_1.

Example of a node-home.tpl.php file

The following is an example extracted from the node-home.tpl.php of absolutecross.com, which is currently being redeveloped in Drupal. It is made up of 3 custom text fields, and will soon add 2 embedded Views as well:

<div id="home_left">
  <?php print $node->field_home_welcome[0]['view'] ?>
  <?php print $node->field_home_featured_content[0]['view'] ?>
</div>

<div id="home_right">
  <?php print $node->field_home_featured_sites[0]['view'] ?>
</div>

Benefits of this technique

  • Allows you to easily theme your home page using a theme file such as node-home.tpl.php. Having a dedicated template file is more straight-forward to understand and keep organized compared to embedding your home page's code into the main page.tpl.php template.
  • Allows you to define a variety of fields for input of static content (such as a welcome message or images).
  • Gives you a convenient "Edit" link at the top of the Home page to easily access and edit the static field's content.
  • When you do edit the content of the fields, you are able to use Drupal's "node revisions" ability to save backup copies of the content of the fields.
  • Easy to embed dynamic content into the template (Blocks in Regions, Views, etc).

Drawbacks of this technique

  • Requires CCK module to be installed if you want to make use of the ability to have added fields (not really a drawback, since almost everyone needs CCK!). However you can still make a "Home" content type using just Drupal core.
Tags —