Drupal's Front Page

Posted By Chris Keller on Thursday, December 26, 2013 - 11:34

When I first started developing Drupal sites the front page baffled me a bit. What if I didn’t want the front page to show “No front page content has been created yet.”? What if I wanted to give the client more control over the front page content? As I learned more and more about Drupal I found a couple of solutions.

The Views module has a frontpage view that you can enable and that seems a bit cleaner, but did I really need a view for that? Making a page--front.tpl.php file in my theme allowed me to completely remove the content region, but maybe I’d like to keep that region around.

Enter the Empty Front Page module. It simply alters the menu callback for your front page and returns an empty array. This solved one problem for me. I could now place blocks, even in the content region, and didn’t require a view.

How should I display content on the front page? Blocks seemed like the best solution, but trying to deal with WYSIWYG entry, the blocks page growing to a huge size, and training clients on how to use the block system overall I decided to create a simple one page form. The form contains a few custom fields that I want the client to be able to alter.

We start with hook_menu.

  1. /**
  2.  * Implements hook_menu().
  3.  */
  4. function MYMODULE_menu() {
  5.   $items = array();
  6.  
  7.   $items['admin/content/frontpage'] = array(
  8.     'title' => 'Front page content',
  9.     'description' => 'Custom fields for front page content.',
  10.     'page callback' => 'drupal_get_form',
  11.     'page arguments' => array('MYMODULE_front_page_content'),
  12.     'access arguments' => array('administer front page content'),
  13.   );
  14.  
  15.   return $items;
  16. }

This creates a nice menu link that can be found under Content->Front page content. We should also create a permission for editing the front page content.

  1. /**
  2.  * Implements hook_permission().
  3.  */
  4. function MYMODULE_permission() {
  5.   return array(
  6.     'administer front page content' => array(
  7.       'title' => t('Administer front page content'),
  8.       'description' => t('Modify front page content.'),
  9.     ),
  10.   );
  11. }

Now we need to create our actual form using Drupal’s lovely form API.

  1. /**
  2.  * Callback function for editing front page content.
  3.  */
  4. function MYMODULE_front_page_content($form_state) {
  5.  
  6.   $form['front_page_title'] = array(
  7.     '#title' => t('Front page title'),
  8.     '#type' => 'textfield',
  9.     '#default_value' => variable_get('front_page_title', ''),
  10.   );
  11.  
  12.   $form['front_page_subtitle'] = array(
  13.     '#title' => t('Front page sub-title'),
  14.     '#type' => 'textfield',
  15.     '#default_value' => variable_get('front_page_subtitle', ''),
  16.   );
  17.  
  18.   return system_settings_form($form);
  19. }

This sets up our simple form and by using system_settings_form it will save all of our fields to the variable table in the database.

Now that we have all of our data stored we need to setup a way to display it on the front page. We could create blocks with the content or we could print them out directly in our theme template file. I normally create blocks but for this example I will just be showing you how to print it out in a template file. First we need to pass the data into the template file and we will do that by adding some code to the template.php file in our theme.

  1. function MYTHEME_preprocess_page(&$variables) {
  2.  
  3.   if (drupal_is_front_page()) {
  4.     $variables['front_page_title'] = variable_get('front_page_title', '');
  5.     $variables['front_page_subtitle'] = variable_get('front_page_subtitle', '');
  6.   }
  7.  
  8. }

We can now print out the data in our page--front.tpl.php file wherever we like.

  1. <?php print $front_page_title; ?>
  2. <?php print $front_page_subtitle; ?>

This might seem like a lot of work to do what Drupal’s block system already does, but I find the client has an easier time understanding this and once you do it a couple of times it doesn’t take very long to write.