So you created a module that you want to put on drupal.org. But just before you release it, you realise it could do with a few default views.
You may think you can use Features for this, but Views allows you to ship default views by creating a
module.views_default.inc and implementing
Now, Ctools can be used to generate the required export code that needs to be placed in
hook_views_default_views. We’ll use a sub-module in Ctools called “Bulk Export” to generate the code.
It’s important to understand that there is a difference between “Bulk Export” and Features. The difference is that you can only export CTools’ configuration objects using “Bulk Export”. Whereas, Features offers full API for integrating any module with it.
In this tutorial, you’ll learn how to export default views using “Bulk Export”.
If you use Drush, run the following command:
drush en bulk_export
How to Export a View
1. Go to Structure, and click on the “Bulk Exporter” link.
2. From the Views table, select which one you want to export. For this example, I’ll select “Product listing”.
3. Then, in the “Module name” field enter in the name of the module that you want to export the code to.
Once you’ve filled out the form click on Export.
On the results page, Bulk Export generates code for three files:
If you’ve created a custom module before, then you already know what the first two are:
views_default.inc is something new.
views_default.inc is used to store exported view displays.
4. Grab the code from the “Place this in example.module” text area and paste it in your module file.
5. Then, in your module directory create a file called
example.views_default.inc and paste in the code from the “Place this in example.views_default.inc” text area.
Now go ahead and enable your custom module. If it’s already installed, then you’ll need to flush the site cache.
Managing Exported Views
If you head over to Structure, Views you should see the exported view on the page with a flag “In code”. This means that the view is stored and being served out of code.
If you change the view and then save it, the “In code” will change to “Database overriding code” which means it’s been modified.
You can easily revert modified views by clicking on the operations button and then on Revert. By reverting a view, it’ll delete the overridden version in the database and use the one stored in code.
If you’ve used Features in the past, then you should understand the concept of “overridden” and “reverting”.
No developer library would be complete without Drush integration and Ctools is no exception. You can generate an export module, like we did earlier using Drush. Just run the
drush ctex command.
You can even revert overridden objects by running
drush ctools-export-revert. For this tutorial, I would run the following to revert the view:
drush ctools-export-revert views_view product_listing.
As you can see, it’s very easy to ship default views in a module without using Features. The only module we depend on is Ctools. However, Views already requires Ctools to work. So users won’t have to download any extra modules.