Recreate a Drupal Feature

In the first part of this Drupal Features guide, you learned why Features is a vital tool for Drupal site builders and developers. We then created a new Feature, which contained a Todo list content type. In this part, you will create a View to display the todo list in a block and add the View to the Feature by recreating it. Views are not the only thing you might add to a feature module. You can add a whole range of components, such as image presets, context and strongarm (for variables) to an existing feature.

Create the View

If you don't already have the Views module installed, go ahead and install and enable it (you need to enable both Views and View UI modules).

Use the following details for the view:
Name: todotoday_list

  1. Create a page: leave ticked
  2. Title: What shall I do today?
  3. Format: Table
  4. Fields
    1. Content: Title (label: Task)
    2. Content: Status (label: Status)
    3. Content: Due Date (label: Due date)
  5. Filters
    1. Filter: Content: Published (Yes)
    2. Content: Type (=Task)
    3. Content: Status (Exposed, label: Status)
  6. Adding a Drupal view filter
    Figure 5-9: View filter

  7. Sort Criteria
    1. Content: Due date
    2. Sort ascending. Rearrange and move to the top.

The final View settings:

Full view settings
Figure 1: View settings

Go to /todotoday-list and you will see the todo list, with a filter for the Status field. By default, both New and Started are already selected, as this was set in the Exposed Filter settings above.

Todo list block
Figure 2: View todo list

Add Block display

It would be handy to have a block that can be added to a region such as a sidebar, so that as we move around the site, we always know what we need to do today.

Add a new Block display.

Adding block display to view
Figure 3: Adding Block display

Add Status field filter

Add a filter for the Status field and set “Is not one of: Done”. You do not want to see Tasks that you have already completed. Make sure it is set for this block (override), as we do not want to affect the page display in this view.

Adding status filter
Figure 4: View filter

Add block to region

Add the block to the desired region in /admin/structure/block, and you should see something like this:

Todo list block in sidebar
Figure 5: Todo list block

You have built the view via configuration, so it currently lives only in the database. You need to export this as code and add it to our module. Luckily Features makes this very easy.

Recreate the Feature

Now it is time to recreate the Feature in the Features UI.

Go to Structure -> Features and click on Recreate next to Todotoday.

You need to add the view that you just created. To do that, you can click on the select menu under Edit components. Then click on Views: views_view. You should see todotoday_list (todotoday_list) available as an option, so select it. The view will then be added to the feature.

Drupal Features UI
Figure 6: Features UI

Now you need to download the feature and move the files into sites/all/modules/custom/todotoday.

And that is all there is to it.

Summary

In this post, you created a View for the Todo List and added it to an existing Feature module. You can recreate a Feature any time you want to add a new component to it. This ensures that your additions are always exported to code and your feature module is always up to date.

Next steps

In the next tutorial, I will walk you through how to update and revert a feature. If you are not on the mailing list, jump on to ensure you don't miss it!

If you would like to learn more about creating features modules and how to take them to the next level with custom code, grab a copy of Master Drupal Module Development.

Learn Drupal Features

Make your life easier by packaging up Drupal site building settings in Features modules. No coding required! 

Free 5 day course where you will learn to create and update your own Drupal Feature module.

Get Day 1 now

Comments

My site has many different content types, views and panels. It also has sample content which I would like to include in the module created by features.

Blair Wadman's picture

You can add as multiple content types, panels or views to a feature. I'd recommend you split the features up though. So you might have a feature for a blog section, with a blog content type, blog panel and blog view. And then another for a news section. And so on.
As for sample content - features is not for storing content, just configuration.

Hello, i created a content type on my test site, exported and imported it to my live site using features. Since then i modified the content type deleting some fields and creating other fields and now the state of the features is marked as overriden.

What happens if i recreate the feature with the web interface or update it with drush? I will lose my nodes/fields of that content type?
Thanks.

Blair Wadman's picture

Hi Andrea,

If you recreate it (or update with drush), then the modified content type will be exported to code. The content type with the fields that you see in the web interface won't change, just the feature module code.

If you revert it (or run drush revert), then the opposite will happen. The settings stored in code will be applied and you would lose any changes you have made to the content type in the web interface (assuming you haven't recreated it yet).

Make sense?

Add new comment