I realized I need some advanced filters for my views and then stumbled onto the tab in Views for "export". Only problem is that, unlike theme wizard, the export here does not tell me where to put the code. What should the filename be, and where in the directory structure?

Thanks!

Comments

chachasikes’s picture

Hey there, I just had that question last month. I found a thread about this somewhere...but this is how I took it a few steps further. Hope it helps:

made a module folder called 'views_custom'
with pairs of files (.info, .module) per view
views_nameofview.info, and views_nameofview.module

What's cool about this, too, is that you can save versions of your views in subversion...which has been great.

Here's some code, Hope it helps -- Chach

**** for .module****


/**
 * This view was generated by the drupal system, with code pasted from Views Export.
 *
 * USAGE: 
 * To make a view into a module:
 * 1. Create or revise a view using the drupal View GUI
 * 2. Export the View
 * 3. Paste the contents from the Views export window between the BEGIN/END paste tags below
 * 4. Rename the default view generating function.
 * 		 Syntax: views_  +  name_of_view + _views_default_views() 
 * 		 Example: views_nanodays_reports_views_default_views()
 * 
 * 5. Rename and save the module
 * 		 Syntax: views_ + name_of_view + .module 
 * 		 Example: views_nanodays_reports.module
 * 6. Rename and save the .info file (This file helps drupal to find your view
 * 		 Syntax: views_ + name_of_view + .info
 * 		 Example: views_nanodays_reports.info
 * 7. Edit the contents of the .info file
 *    name - shows up on Module Menu
 *    
 * 	  Example:
 * 		; $Id$
		name = CUSTOM VIEWS: Reports 2008
		description = "Displays Reports "
		dependencies = views
		package = Views
		project = "views_custom"
 * 
 * 
 * 
 * 8. Back up your drupal system
 * 9. Delete the view on the system
 * 10. Your .module and .info file should be in the folder views_custom
 * 11. Upload your module to sites/all/modules
 * 12. Enable the module (admin/build/modules); will appear under 'Views' (controlled in .info file)
 * 13. Go to administer views page: admin/build/views...you should see your view there
 * 14. If change your view, it overrides this new 'default' setting that you have declared.
 * 
 * HINTS: If you do not see your view:
 * 1. Make sure you have enabled it
 * 2. Clear the Views cache.
 * 3. Disable and then re-enable the view.
 * 4. Remember that any changes you make to a view in the drupal system will not be saved to the module. 
 *    However, you can make more changes, re-export, paste into your .module, and reupload, clear your views cache to edit your view.
 * 
 */

//function views_{name of view, matches name of module, eg. all_reports}_views_default_views() {
//for views_all_reports.module, views_all_reports.info

function views_all_reports_views_default_views() {
	
/* --- BEGIN ---- PASTE from Views export ----------- */	
  



/* --- END ---- PASTE from Views export ----------- */
  return $views;
}


****** for .info
; $Id$
name = Custom View: Reports
description = "Generates a list of all reports"
dependencies = views
package = Views
project = "views_custom"
version = "5.7"
datestamp = "1201565404"

NOTE: I didn't know how to deal with the versioning info and datestamp, to that part is a hack. It hasn't been too much of a problem, but sometimes the update module shows errors because my modules are custom.

chachasikes’s picture

that's the hard way -- the other, easier, way is basically:
1. copy the text from the export, and you could save that text as a .txt file. For example, myview_version1.txt
2. whenever you want to import that version of the view, paste text into the 'import view' text field. you would just have to check that you weren't trying to import when you already have a view with the same name.

generaltao’s picture

Oh wow, thanks! This will most probably help me.

Say I want to add custom filters for a view, that are based on session values. Would this be the best way to do it? Just alter the view output code?

Thanks for getting back to me and with such thorough code to boot!

michelle’s picture

I don't see how this helps with custom filters, but this is one way to handle the exported views:

http://shellmultimedia.com/articles/managing-your-views-code

Michelle

--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.

mpp’s picture

You can also export individual views. If you do this, keep in mind that this export does not include the line that adds the exported $view into the larger $views array:
$views[$view->name] = $view;

source: http://views-help.doc.logrus.com/help/views/api-default-views

doeboy’s picture

Does anyone know you can find a good repository of great views export code on the internet?

uplinkspyder’s picture

Unanswered since 2009, WOW!
Maybe I can reinvigorate this thread. It stands to reason that it would benefit the collective intelligence if there was a repository of Views. yeah, can we have a show of hands?

seoslinger

WorldFallz’s picture

because there isn't one. Views are so specific to content types and fields there's very little chance they could simply be imported on another site. And the ones that could be, based on core content types and fields, would be so generic as to be useless.