How to Export Views Using Bulk Export and Ctools

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 hook_views_default_views.

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”.

Getting Started

Before we begin, I’ll assume you’ve already setup Views and Ctools. The only thing left to do is enable “Bulk Export”. So head over to the Modules page and enable it.

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.

Fig 1.0

2. From the Views table, select which one you want to export. For this example, I’ll select “Product listing”.

Fig 1.1

3. Then, in the “Module name” field enter in the name of the module that you want to export the code to.

Fig 1.2

Once you’ve filled out the form click on Export.

On the results page, Bulk Export generates code for three files:

  • example.info
  • example.module
  • example.views_default.inc

If you’ve created a custom module before, then you already know what the first two are: info and module. But, views_default.inc is something new.

The 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.

Fig 1.3

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.

Fig 1.4

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.

Fig 1.5

If you change the view and then save it, the “In code” will change to “Database overriding code” which means it’s been modified.

Fig 1.6

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.

Fig 1.7

If you’ve used Features in the past, then you should understand the concept of “overridden” and “reverting”.

Drush Integration

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.

Summary

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.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of WebWash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 12 years and has successfully completed several large Drupal projects in Australia.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Media Management in Drupal

Download a FREE 8 part video course on managing media in Drupal.