Download & Extend

Add integrations with Views Bulk Operations (sample VBO action to expose selected records to CSV export included herein)

Project:Views data export
Version:7.x-3.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

Will be good to see integration with Views Bulk Operations module. This add feature for exporting to csv and other format many custom selected data, instead of building separated view for them.
You can do this via provide some rules via hook_node_operations() and re-use your current export functions.
Good example that exports to csv you can find here: http://britesparkz.com/tennessee-drupal-blogs/ubercart-vbo-and-cvs-exports

Comments

#1

hook_node_operations() is not supported by VBO for D7.
You either provide a core action, or a rules action (or even a complete rules component).

#2

For Drupal 7.x you can use hook_action_info(). I done for my needs csv export in custom module, here is example:

<?php
function caramba_action_info() {
  return array(
   
'caramba_action_csv_export' => array(
     
'type' => 'entity',
     
'label' => t('Export to CSV'),
     
'configurable' => FALSE,
     
'behavior' => array('caramba_export_csv'),
    ),
  );
}

function
caramba_action_csv_export($entity, $context) {
  static
$csv;
 
  if(empty(
$csv)) {
   
$data=array(
     
'sku',
     
'title',
     
'alt_name',
     
'price',
      );
     
$csv="\"".implode('","',$data)."\"\n";
  }
 
$data=array(
   
addslashes($entity->sku),
   
addslashes($entity->title),
   
addslashes($entity->field_alt_name[LANGUAGE_NONE][0]['value']),
   
addslashes($entity->field_price[LANGUAGE_NONE][0]['amount']),
    );
 
$csv .= "\"".implode('","',$data)."\"\n";
 
  if (
$context['progress']['current'] == $context['progress']['total']) {
   
$GLOBALS['devel_shutdown'] = FALSE;
   
   
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   
header('Content-Type: text/csv');
   
header('Content-Disposition: attachment; filename="export_price.csv"');

    die(print
iconv('UTF-8','CP1251',$csv));
  }
}
?>

It is very basic and not configurable, but works for my needs very good.
This is not views-related code, but will be good to see this functional in this module or separate module.

#3

Status:active» closed (works as designed)

Hey @Murz, pretty slick. Thanks for sharing this info-- works like a boss!

I had a spot of trouble with it (only got one record out from your snippet adapted for my use case) and wanted to share my observations and load this issue queue with some SEO on this elegant solution to giving Views Bulk Operation the ability to export CSV of the selected records. Maybe you can comment on them and confirm?

* It appears that you've configured your VBO action not as aggregate => TRUE and yet it still runs. This is because you're iterating the whole set of VBO provided entities against a growing, static variable and terminating iteration on VBO's $context variable status of completion field.
* Note that setting aggregate => TRUE breaks the solution and only returns results for 1 record
* what is configurable => FALSE in this case?

[edit: OOps, I misread the title of this issue queue and its project namespace -- reopening the issue]

Thanks!
-Bronius

#4

Title:Add integrations with Views Bulk Operations» Add integrations with Views Bulk Operations (sample VBO action to expose selected records to CSV export included herein)
Status:closed (works as designed)» active

(padding with some SEO in the title, even though it's more specific that the original author had requested and setting status back to Active)

#5

Hey Murz,

The code in comment #2 does not work for me. I suspect the line die(print iconv('UTF-8','CP1251',$csv)); is causing problem.

#6

a_thakur, this is not good code for use, this is only example how I solve this issue for only my site, I not recommend to use it on other sites. String

<?php
die(print iconv('UTF-8','CP1251',$csv));
?>
does codepage converting for Cyrillic language, so if you don't need this, you can use
<?php
print $csv; die();
?>