I am using exposed filters for a CSV export, and then providing the URL via an email daily.

For instance:

The Data Export path is:
/admin/orders/payment-list-all.csv

With exposed filters the path becomes something like:
/admin/orders/payment-list-all.csv?created_between[min]=2011-09-22&created_between[max]=2011-09-23

Now this works, however it always spits out the same filename. I think it would be very useful to set the filename with tokens based on the exposed filter values such as:

payment-list-all-[created_between:min].csv

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

R.Hendel’s picture

It would be fantastic, if there would be an option to use also arguments in the file name.

ultimike’s picture

See this comment for the proper way to do this.

-mike

xurizaemon’s picture

Category: Feature request » Support request
Issue summary: View changes
Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

aniket.mohite88’s picture

FileSize
13.28 KB
17.72 KB
19.69 KB

Hi,

Its a good point mentioned here in this comment.

  1. But all it does is provide ONLY a static file name.
  2. Using exposed filter replacement pattern, gives all exposed filter in the form - filter1_foo-filter2_bar.xls. This name becomes ugly when there are many exposed filters or if field names are long or not all of the filters are used for filtering

I have used the below method to generate static or dynamic file-names while exporting.

--------------------------------------------------------------------------------------------

STATIC FILENAME

Step-1: Setting for the exported file

XLS filename setting

Step-2: Select "provide as file" & mention a filename

Provide as file setting

Step-3: Choose from replacement pattern tokens

replacement pattern setting

--------------------------------------------------------------------------------------------

DYNAMIC FILENAME

To have a custom dynamic file name, you have to use the hook_views_pre_build(&$view) hook in your custom module.

You can have your custom calculations or get data from exposed filters & change the file-name dynamically using -
$view->display_handler->options['style_options']['filename'] = $file;

NOTE- Since you are altering the file-name dynamically, you have to first mention a static file-name as mentioned in above section.

example usage -

<?php

/**
 * Implements hook_views_pre_build(&$view)
 * To alter download filename
 */
function mymodule_views_pre_build(&$view) {
  //dpm($view);
	
    if($view->name == 'my_view')	{
	// Do some custom calculations using some logic or through exposed filters
	// Example: to get exposed filter from query parameters, use -
	$param = drupal_get_query_parameters(); // get query parameters
	//dpm($param);
		
	$fieldVal1 = $param['exposed_field_1'];
	$fieldVal2 = $param['exposed_field_2'];
		
	//some custom logic/function here. Example - 
	$custom = $fieldVal1.'-'.$fieldVal2;
		
	$file = $custom.'.xls';
	
	// Alter the downloadable excel sheets name
	$view->display_handler->options['style_options']['filename'] = $file;
		
	}
}	

?>

The above code will work & alter the file-name every time a filter is applied (filter value is changed).

Hope this helps.

Regards,
Aniket Mohite

arunkumark’s picture

Thanks for comment #5 for dynamic file name change by coding.

The below code helps to change the file name in hook_views_post_execute().

The dynamic file name needs to pass for the argument.

$view->style_plugin->options['filename'] = 'NEW_FILENAME' . $nid;

/**
 * Implements hook_views_post_execute().
 */
function MYMODULE_views_post_execute(&$view) {
  if ($view->name == 'test' && $view->current_display == 'views_data_export_1') {
  global $user;
    $view->style_plugin->options['filename'] = 'order_of_' . $user->name;
  }
}