26 Mar 2009

Exporting views with ease

garfield's picture
Senior Architect and Consultant
2

For today's simple but highly helpful trick, have you ever built out a whole bunch of views and then wanted to export them to a module? If you haven't, you're doing something wrong. :-) Of course, setting up the appropriate file can be tedious and error prone, especially in Views 2 where views definitions can be, um, painfully long.

Fortunately Views 2 includes a Bulk Export module, which lets you export a whole bunch of views at once and even pre-creates the hook_views_default_views() function for you. Score! At least, score until you need to update one of those views later. Do you export it individually and then hunt through the gigantic hook_views-default_views() implementation for the view you're looking for? Or do you re-export everything, which means trying to remember all of the views in that particular file? Because woe is you if you forget one and overwrite it (unless you're using a source control management system like Subversion).

You do neither, of course.

Views 2 also includes a handy ability to "tag" views. The Views Bulk Export module has the handy ability to filter views by tag. See where this is going?

Simply tag all of your views for a given module with the name of the module. You can do that when building out the module or site. It's particularly useful when building views and a custom module for a specific site, as you're likely to have a dozen or more views. Then when you're ready to push them to code use the Bulk Export module to filter by that tag, click the "select all" checkbox, and export. Poof, you now have the entire new contents of your module.views_default.inc file, ready to copy and paste into the file. Done. Made an update to a View? With proper tagging it takes about a minute to regenerate the entire file and commit it to your repository.

Nothing earth-shattering, but still useful.

Tags

Comments

sort your bulk export views for better diffs and commits

Here's a patch in development:

http://drupal.org/node/402650

This patch sorts all the views by name before they're exported, so you can actually commit diffs of your individual views rather than just a messy diff of the file with each view in a different order on each revision.

iiiiiiiiiiiinteresting

I have to contemplate if I like this bulk export or not. On one hand, I want to make it easier for my themer to export views. On the other hand, I like having each view as it's own file.

Bangpound's patch suggestion makes me feel *a lot* better about dealing with better diffs. We have already had examples of having to revert commits involving views (while preserving other commits involving that same view), as well as occasional instances where the same view was edited by different people in a short time period (bringing up potentials of conflicts).

But I'll definitely keep this in mind in terms of improving developer workflow.