When working with multiple views or sites, creating every view from scratch can be dull, repetitive and error prone. To avoid this, views provides a way to copy views from one site to another or from one environment (like local) to another (like staging).
Exporting a view
The export tool is used to export a View with all of its settings to code so that you can import it somewhere else. Let's export one now:
- Go to "Structure => Views" (admin/structure/views)
- Click the dropdown arrow for the “All Events” view.
- Choose "Export"
- Select and copy all the text from the textbox (This is the PHP code that is equivalent to the query views creates.)
- Open any text editor (TextEdit)
- Paste the selected text into a new file
- Save the file (This is optional. If you’re going to import it right away, then you don’t really need to save the file. The filename and extension are not important but make sure to remember the name of the file. Something like “AllEventsView.txt” would be appropriate for this example.)
Importing a view
The import tool is typically used when migrating from one site to another but for this video we will be using the exported code from this site and we will also import it in this site. To show how this works, I’ll first delete the view from the site.
- Go to "Structure => Views" (admin/structure/views)
- Click the dropdown arrow for the “All Events” view.
- Choose “Delete”
- (Delete)
Now, let’s import the view we just exported.
- Go to "Structure => Views" (admin/structure/views)
- Click "Import" link
- View name: (This is optional. If left empty it will use the view name that appears in the export code.)
- Replace an existing view if one exists with the same name (This is useful when you update a view and want to push it to a testing environment. You can check this to replace the existing View with your updated one.)
- Bypass view validation (This will do what it says, and bypass the validation of plugins and handlers when importing this view.)
- Paste view code here: Paste the exported view from TextEdit
- (Import)
- (Save)
Now, our view is set back up exactly how it was when we exported it.