ImageField Archive: an action to download zipped images

One of the recurring requests in the Views Bulk Operations issue queue is to create a downloadable archive of files attached to nodes. While the general case is not trivial to solve, I was recently hired by research on BLOGS to write a more specific action: create a Zip archive of all images stored in imagefields. The ImageField Archive module is the result.

Features

  • Provide a new action called "Download archive of images", that works with node views.
  • Show a settings form on the VBO admin page that allows to enter the patterns for naming the Zip archive and each image file in it. The patterns use the venerable Token module.
  • Show a user form during VBO execution that allows to select the ImageCache preset(s) to include in the archive.
  • Create the Zip archive and download it!

Technicalities

  • The PHP core class ZipArchive is needed to create the archive.
  • Because ImageCache presets don't have entries in the {files} table, I needed to expose their data for using custom tokens. To this end, I implemented the hooks hook_token_list and hook_token_values. This allows filename patterns to include preset names as well as other file information.
  • Normal actions are designed to be called with only one object at a time. In our case, however, we need all selected objects (the nodes in the VBO) to be passed at once to the action function, to simplify iterating over the nodes and placing the image files in the archive. A while ago, I had introduced the new aggregate attribute for actions, which does just that: the action function signature becomes example_action($nids, $context) instead of example_action(&$node, $context).
  • To show the action settings form on the VBO admin page (as opposed to during VBO execution), I introduced a series of new hooks for actions called action_views_bulk_operations_form. Here's an example of using them:
<?php
// @file mymodule.module

/**
* Implementation of hook_action_info().
*/
function mymodule_action_info() {
  return array(
   
'example_action' => array(
     
'type' => 'node',
     
'description' => t('Example action label'),
    ),
  );
}

/**
* Implementation of action_views_bulk_operations_form_options().
*
* These options will be saved along with other VBO settings in the view data and can be therefore exported.
*/
function example_action_views_bulk_operations_form_options() {
 
$options['zipname'] = '';
 
$options['filename'] = '';
  return
$options;
}

/**
* Implementation of action_views_bulk_operations_form().
*
* This form will appear on the "Bulk operations" style plugin settings page of the view admin,
* in a fieldset labeled after the action description.
*
* Form values will be passed to the action function in the $context['settings'] array.
*/
function example_action_views_bulk_operations_form($options) {
 
$form['zipname'] = array(
   
'#type' => 'textfield',
    ...
  );
 
$form['filename'] = array(
   
'#type' => 'textfield',
    ...
  );
  return
$form;
}

/**
* Implementation of action_views_bulk_operations_form_validate().
*/
function example_action_views_bulk_operations_form_validate($form, $form_state) {
 
// Perform your validation here.
}

/**
* Implementation of action_views_bulk_operations_form_submit().
*/
function example_action_views_bulk_operations_form_submit($form, $form_state) {
 
// Perform any additional action needed here.
}

/**
* Implementation of action().
*/
function example_action(&$object, $context) {
 
$zipname = $context['settings']['zipname'];
 
$filename = $context['settings']['filename'];
  ...
}

?>