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();

  // The key is the machine name of my download type.
  $types['feature_csv'] = array(
    // A human readable name to show in an administrative interface one day.
    'type_name' => 'Feature CSV',
    // A human readable description of the format.
    'format' => 'Comma-separated Values',
    // An array of functions that the API will use to customize your experience.
    'functions' => array(
      // The function that tripal jobs will call to generate the file.
      'generate_file' => 'trpdownload_feature_csv_generate_file',
      // OPTIONAL: provide a summary to the user on the download page.
      'summarize' => 'trpdownload_feature_csv_summarize_download',
      // OPTIONAL: determine your own filename.
      'get_filename' => 'trpdownload_feature_csv_get_filename',
      // OPTIONAL: 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.
 * @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;

}

The following is available within vars:

  • download_args
    • q: all the arguments in the path passed to the Tripal Download API
    • safe_site_name: a sanitized version of the site-name for use in filenames
    • type_info: what you defined in hook_register_trpdownload_type() for the current download type

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.
 * @return
 *   A string representing the file suffix for a specific download.
 */
function trpdownload_feature_csv_get_suffix($vars) {
  return 'csv';
}
Clone this wiki locally