Using Drupal features: example of a custom blog module
What is Features
Drupal features is a module which allows you to create your own modules where you can include your own views, content types and many other features. With features you don’t have to go through the pain of making same views and features for some common type of features on your site like blogs, forums etc.Drupal feature is a module which can make life easy for people who are making websites based on Drupal day in day out. One of the biggest advantages of feature which I personally feel is re usability.
For example you have a blog feature which now days almost every client would want on his website. Then there is forum. So, let’s say you are making few content types, fields using CCK and then setting up block and page views for those content type and this is something you are doing on most of the website that you are developing. Although routine work, this can cost you a lot of time and personally doing the same thing again and again is boring. So, feature will save us from this.
In this tutorial I would work on two different Drupal installations. I would be using one installation to make up the content types and views using required modules. And the second would be a fresh install of drupal out of the box. And we would install the feature that we will be building into this fresh copy.
Start with the basic setup
So, let’s start with a basic blog feature. We want that in the primary menu, we will have a link “Blog” which will be a view page where the posts of content type blog will be visible. Then there will be a right side bar block which will contain archives and tags which will again be taxonomy. The blog landing page will have nodes in teaser view and when clicked will go to the full node. Right now, for the blog feature I have used the blog feature which comes with Drupal core.
So, the first thing is to enable the blog and blog api module in Drupal module page and then let’s create a view for the same. Here is the screen shot of the view page:
Creating the views and blocks
I personally like to have views on content type basis and that way I feel it is easy to manage the views. But yes, sometimes it is also nice to have views set up page wise for example all the views for front page can come in one single view. But yes, this is personal choice.
So, in the defaults for the view settings there are few things that we would set up first like:
- Filter the content by type – blog.
- Sort the content by postdate descending.
- I give it a default title blog post.
- Row style will be Node view set to teaser.
- We would need a pager and limit set to 10 on each page.
- With this we first save the default view.
You can check the settings in the screenshot. The ones that are in bold are the settings that I have made. Now, I add a page display. Once done, all I need to do is change the name of the display to “Blog landing page” so that it is easy to recall later what the page display is doing. I have set the path to “blog” which means the page link will be something likehttp://sitename/blog and also i have added the page link to the primary links menu item so that it is easy to access the page.
A note: it is a good practice to first setup the defaults of the view. Once we did that, rest of the page and block displays automatically get the settings and the rest if we need to change can any time override.
With all set up, and after adding two new dummy blog post my blog page looks something like this.
The menu at the top is working, the posts are coming in teaser view and things are all right. Now, it is time for the blog to get its sidebars. For this we will use the same view and add a few displays to get what we want.
The main thing about this block is to have the posts being sorted month and year wise which is a very important thing to do. In arguments we give Node: Created year + month, it will display the sorted summary and that is what we want. Once the view display is saved, we move to the blocks section under Administer > blocks and set up the block to display on the right sidebar. I am using Garland theme so I have only right side bar. Other themes may have different names (doesn’t make a difference).
Once the block is enables, the blog landing page will look something like this
So, with the blogs coming one after the other, archives block ready the only thing left is tags or what we call the categories. This is very much important to manage the post when the blog gets crowded with lot of content.
Setting up terms with Taxonomy
Now, we create a taxonomy term and associate that with the blog content type and keep it to autocomplete tag field.
With this, we will be creating a final term view where we will display all the tags with their weight as a block so that users can check the tags. Make sure you are making a term view and not node view.
In this view, its just terms with their weight to be diaplyed in the field. We can later make views with arguments as taxonomoy terms to display the blog post, but then it will be too advanced and out of scope for this tutorial, so we will stick to taxonomy terms being displayed and when clicked show us the posts.
The screenshot will give you an idea:
Once done, we have to add the tags to the content that we have created or else nothing will be visible. Once done, activate the view block on the right hand side bar like we did for the archives block. So, this is how my blog landing page looks right now.
Now we are ready with our blog and you can see how much settings we have to get through to make a simple blog post thing on the website. Now, using features, I will make a module out of the settings that I have made for my blog.
We start with creating a new feature
Once these things are set, we go to the main settings. Basically feature will allow us to make a module out of the views, taxonomy and other settings that we have done for our blog post functionality and import it on the fly to another installation.
These are the things that I have set up in the edit components section. Once completed, click “Download feature”. It will prompt you to download a module. Save the file, we will need it for our second installation. Remember, right now the second install is just out of the box. We will directly try and install the Custom Blog module and let’s see what happens.
So, on my fresh install’s module page I can see this:
You can see, this is a nice facility. It tells us which are the modules that need to be enabled and what are the modules that we are missing for this module to work.
So, I copied the modules to my new installation and went to the same page again and this is what is happening now:
All the necessary modules are available so now hit “Save configuration”, it will ask for your permission to enable the modules which are requited.
With that, you can add content and see that those things are in place. The only thing that you have to do is get those block in right side bar again. That is something that I feel should be possible. I will look into context and see if I can get that thing done. Till then enjoy features.
You can do a lot with features and it’s amazing capabilities. Share your thoughts and let other know how you have used features to make your life easy.