Here is a small patch that adds support for batch importing the core parts of most Drupal sites: views, CCK, and nodes.

Comments

dman’s picture

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 ...

+/**
+ * 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?

dman’s picture

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 :-}


/**
 * 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!
}


James Andres’s picture

StatusFileSize
new4.13 KB

Hah! You're completely right, that is quite a typo. Here is a new patch.

James Andres’s picture

What advantage does your profile_create_view_from_file($filename) function have over the current install_views_ui_import_from_file($file, $name = NULL) function committed to the DRUPAL-6--2 branch?

Or maybe I don't understand the question?

dman’s picture

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.

James Andres’s picture

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.

dman’s picture

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.

James Andres’s picture

Status: Needs review » Closed (works as designed)