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.
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.
Now we need to create our actual form using Drupal’s lovely form API.
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.
We can now print out the data in our page--front.tpl.php file wherever we like.
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.