I exported a custom view for my module. I want to re-import it programmatically inside my module installer.

I tried drupal_execute('views_ui_import_page', $form_state); it didn't work. From the view admin GUI, I noticed that a view also needs be saved after it is imported.

Any help is greatly appreciated.

thx!

Comments

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Please read the documentation, particularly the page entitled "Using default views in your module" before posting support requests.

jako0013’s picture

Version: 6.x-2.2 » 5.x-1.6

How about in Drupal 5? It's not so obvious, even with the Drupal 6 documentation.

jako0013’s picture

Status: Closed (won't fix) » Postponed (maintainer needs more info)

Sorry, upon further reading I think I get it, but I would like to know if Views for Drupal 5 contains the same functionality as described on http://views-help.doc.logrus.com/help/views/api-default-views. Thank you.

merlinofchaos’s picture

Status: Postponed (maintainer needs more info) » Fixed

Drupal 5 also supports default views. That documentation is on drupal.org under http://drupal.org/handbook/modules/views/api

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

tylerwalts’s picture

I see that this thread is closed, so please help me to redirect this post if it is in the wrong place...

This applies to Drupal 6.x

My goal was to programatically import a set of views, and then automatically enable them and display the block views on a page. I too struggled with getting the out-of-the-box views api to automatically load a view export saved as a file. I ended up using a bash script in the end, with help from drush and views_import module (I only need to do this once, during a site migration script, so for me I did not care about using a module.) If you must have this in a module, then a similar approach can be taken by substituting the php parts where bash/drush is used. Here is the basic script:

#!/bin/bash

# Download the views_import module code
drush dl views_import
# As per module documentation, copy view export files into the /imports/ subfolder prior to enabling
cp /path/to/my/view/exports/myview.view /www/mydrupal/sites/all/modules/views_import/import/
# Enable the views_import (which in turn installs the views in the /imports/ subfolder)
drush enable views_import
# Now the views show up in the views list, but they are disabled.  Run this to enable it
drush eval "variable_set('views_defaults', array_merge(variable_get('views_defaults', array()), array('my_view_name'=>FALSE)));"
# Now place the blocks on the right pages, this does one on the left region on the front page only
drush sql query "INSERT INTO blocks (module, delta, theme, status, weight, region, custom, throttle, visibility, pages, title, cache) VALUES ('views', 'my_view_name-block_1', 'my_theme_name', 1, -66, 'left', 0, 0, 1, '<front>', '', -1);

This code just does a single view, and it assumes that the view has a block display set up. You could do a whole folder of view exports at once, and then either copy/paste the eval and sql lines, or turn them into big statements that eval/execute multiple array items / db rows. It does not matter what you name the view export file names, as long as they only contain the copied code from the export textarea. I named mine *.view just to be clear.

Cheers,

- Tyler

westbywest’s picture

@merlinofchaos

Default views are not helpful if you need to override a default view defined by a contributed module. Your module can't override another module's default view with its own default view.

In this instance, you do have to define an overridden view in the DB, and being able to do so programmatically becomes very useful.