Patch for batch importing views, CCK and node exports
James Andres - April 13, 2009 - 23:20
| Project: | Install Profile API |
| Version: | 6.x-2.x-dev |
| Component: | CRUD functions and includes |
| Category: | feature request |
| Priority: | normal |
| Assigned: | James Andres |
| Status: | needs review |
Description
Here is a small patch that adds support for batch importing the core parts of most Drupal sites: views, CCK, and nodes.
| Attachment | Size |
|---|---|
| export-views-batch_import.patch | 4.14 KB |

#1
O_o
A small patch that does all that?
I assume the expected import inc file for views is one such as returned by the views export feature ...
<?php+/**
+ * Import all view exports in a directory.
+ **/
+function install_views_ui_import_directory($path) {
...
+
+ foreach (glob($path . '/*.inc') as $filename) {
+ $result = install_node_export_import_from_file($filename);
+
+ $results[$filename] = ($result !== FALSE) ? TRUE : FALSE;
+ }
+
+ return $results;
+}
?>
But can install_node_export_import_from_file() really handle importing a view?
#2
Here's what I came up with after tracing the code step by step.
I get the feeling there is a better API under the hood somewhere, but don't know what it's called.
So I emulate pasting an exported view into the views_ui import form :-}
<?php
/**
* Programatically create a view based on a view export definition file.
*
* Should try to use the views import api, for now just use the paste-into-form
* way.
*
* @param the filename of a view export dump.
*/
function profile_create_view_from_file($filename) {
$content_string = file_get_contents($filename);
drupal_set_message("Creating view definition from $filename" );
include_once drupal_get_path('module', 'views') .'/includes/admin.inc';
$values = array(
'view' => $content_string,
'op' => 'Import',
'submit' => 'Import',
);
$form_state = array('values' => $values);
drupal_execute('views_ui_import_page', $form_state);
// At this point the whole view is stored in the UI edit cache
// - BUT NOT SAVED YET
// I need to Really save the cached thing.
// Get it back from cache - it may have appropriate validated bits in it that were missing earlier.
// wossisname?
eval($content_string);
// Woo, now we actually know the $view.
// Could I have skipped the whole tedious import? probably.
views_ui_cache_load($view->name);
$view->save();
// DONE!
}
?>
#3
Hah! You're completely right, that is quite a typo. Here is a new patch.
#4
What advantage does your
profile_create_view_from_file($filename)function have over the currentinstall_views_ui_import_from_file($file, $name = NULL)function committed to the DRUPAL-6--2 branch?Or maybe I don't understand the question?
#5
Advantage? Possibly none apart from actually existing. I can't find the func you mention in any of my checkouts, but it sounds really useful. I wish it did exist, and that I'd known about it.
I'm mostly just posting my code up here for comparison.
I saw broken code in the patch, and supplied an alternative that worked for me because I needed it right then.
Later, it seems like you applied a better alternative, by calling in a library that I didn't have available. That's what happened there.
#6
Hi dman,
The function is in the contrib/views.inc file in the DRUPAL-6--2 branch. Note, this is CVS only at the moment, downloading the latest release from the install_profile_api homepage won't contain this code.
Thanks,
James.
#7
I sorta guessed as much. Looking forward to trying it out. It sounds like what I'm looking for.
My contribution here was hoping that an answer like this was/would-be available. But trying to be proactive in the meantime.