Skip to content

Customize File Name and Suffix

Lacey-Anne Sanderson edited this page Jan 27, 2017 · 4 revisions

When you define a custom download type by implementing hook_register_trpdownload_type(), you can also specify functions to alter the default file name and type suffix. These functions are specified by the key 'get_filename' and 'get_file_suffix' respectively in the functions array (see below).

/**
 * Implements hook_register_trpdownload_type().
 */
function trpdownload_example_register_trpdownload_type() {
  $types = array();

  /// This example download type will be used for the following examples:
  $types['feature_csv'] = array(
    'type_name' => 'Feature CSV',
    'format' => 'Comma-separated Values',
    'functions' => array(
      'generate_file' => 'trpdownload_feature_csv_generate_file',
      'summarize' => 'trpdownload_feature_csv_summarize_download',
      // NOTICE: determine your own filename.
      'get_filename' => 'trpdownload_feature_csv_get_filename',
      // NOTICE: Change the file suffix (defaults to .txt)
      'get_file_suffix' => 'trpdownload_feature_csv_get_suffix',
    ),
  );

  return $types;
}

Customize the file name for your download type

By default the name of your file without suffix will be [safe site name].[trpdownload type key].[current date]. Since this is the filename presented to the user you may want to change it to make it more readable. To do so, add an element to the functions array in hook_register_trpdownload_type() where the key is 'get_filename' and the value is the name of a custom function provided by your module. For consistency I suggest naming your function [your module name]_[trpdownload type key]_get_filename. The following example changes the filename of the example type above to [safe site name].sequence_features.[current date]

/**
 * Customize the filename for the feature_csv download type.
 *
 * @param $vars
 *   An array of variables available to this function. Including:
 *     - ['download_args']['q']: all the arguments in the path passed to the Tripal Download API
 *     - ['download_args']['safe_site_name']: a sanitized version of the site-name for use in filenames
 *     - ['download_args']['type_info']: what you defined in hook_register_trpdownload_type() for the current download type
 * @return
 *   A string representing the filename for a specific download.
 */
function trpdownload_feature_csv_get_filename($vars) {

  $filename = $vars['download_args']['safe_site_name'] .'.sequence_features.' . date('YMj-his');
  return $filename;

}

As you can see from the function docblock (multi-line comment describing the function), You have access to all the query variables passed to the generate file function and as such, can make the name of the file accurately reflect what will be in it. Of course, there needs to be a balance between length of the file name and descriptiveness, so keep that in mind.

How to change the file suffix

By default the file suffix is .txt. This can be changed independently of the filename by adding an element to the functions array in hook_register_trpdownload_type() where the key is 'get_file_suffix' and the value is the name of a custom function provided by your module. For consistency I suggest naming your function [your module name]_[trpdownload type key]_get_file_suffix. The following example shows you how to change the suffix from .txt to .csv.

/**
 * Customize the file suffix for the feature_csv download type.
 *
 * @param $vars
 *   An array of variables available to this function.  Including:
 *     - ['download_args']['q']: all the arguments in the path passed to the Tripal Download API
 *     - ['download_args']['safe_site_name']: a sanitized version of the site-name for use in filenames
 *     - ['download_args']['type_info']: what you defined in hook_register_trpdownload_type() for the current download type
 * @return
 *   A string representing the file suffix for a specific download.
 */
function trpdownload_feature_csv_get_suffix($vars) {
  return 'csv';
}

This is usually a very simple function that returns the same suffix for all files generate by a particular download type. You do have access to the same information as when generating the file name so this function could be used to support multiple file types using the same download type.