2

Issue:

When I click on the link ofwebform Download as shown in below screenshot the default name of excel sheet before save to local disk appears like localhost_worklog.xlsx but I want something like localhost_worklog_02172017.xslx which is appended with _<current time>.

SCREENSHOT:

enter image description here

EDIT: After using iLLin's answer

enter image description here

Problem: How can I alter the name of localhost_worklog.xlsx before save?

I am using drupal 7.53.

1 Answer 1

0

The only way to do this is to change the $node->title on the loaded object for the webform. That filename uses that title, so I am assuming you have a webform title of "Localhost Worklog".

The problem with that is you only want to change the node title, when you are downloading the webform (or atleast running through that process).

You have two options (I think). You could implement a hook_entity_load() function and then check if the URL is

node/%webform_menu/webform-results/download or

node/%webform_menu/webform-results/download-file

function MYMODULE_entity_load($entities, $type) {
  if (arg(2) == 'webform-results' && (arg(3) == 'download' || arg(3) == 'download-file')) {
    foreach ($entities as $entity) {
      $entity->title = $entity->title .'_'. now();
    }
  }
}

Docs

The other way I can think of is altering the page callback with a hook_menu_alter(), changing the title there and then calling their function to return the form or download file with the updated node object to include the time appended to the title.

EDIT

From reviewing your update, it seems it's changing the title too soon on that page. So I think you will need a combo approach. Using hook_entity_load and hook_form_alter. The point is changing that title in the $node object that is passed around for download. So try this.

/*
 * Implements hook_entity_load().
 */
function MYMODULE_entity_load($entities, $type) {
  // Change the node title to include the time when downloading.
  if (arg(2) == 'webform-results' && arg(3) == 'download-file') {
    foreach ($entities as $entity) {
      $entity->title = $entity->title .'_'. now();
    }
  }
}

/*
 *  Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == WEBFORM_DOWNLOAD_FORM_ID) {
    // Change the node title that is passed around for download.
    // @TODO: Check to make sure this is correct.
    $form['node']['#value']->title = $form['node']['#value']->title . '_'. now();
  }
}
2
  • Thanks,I was able to change filename accordingly but how can I retain the original title on dashboard.Pls see my edit. Commented Feb 21, 2017 at 6:10
  • You need to only change that title before the download process. What does your code look like? It shouldn't "save the node title" and it should only change it on those two pages. You may need to do a form_alter or try another location in the code.
    – iLLin
    Commented Feb 21, 2017 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.