I'm new to Drupal.
I have a custom form, within a custom module, using Drupal 8.
I would like to provide a simple link (or button, whatever the correct terminology would be), that when a user clicks will download a file that I specify.
How do I create the clickable part in the first place? Is there something like
'#type' => 'download'
, similar to the way'#type' => 'managed_file'
is used for uploads? Or is using the'type' => '#submit'
route the best way to go?Where do I place the file, and how do I make the clickable part grab that file?
The file will be a .xlsx in my case, but I've seen similar questions asking how to do this for a .pdf file, .doc file, etc., but none of them have a concrete answer.
I don't care what the user does with the file once they've downloaded it. It doesn't need to be protected or "read-only" or anything fancy. I just want someone to be able to download the file and save it to their computer.
Code examples would be extremely helpful. So far my best guess is something like this:
// Taxonomy Download
$form['taxonomy_download'] = [
'#type' => 'submit',
'#value' => t('Download Default Taxonomy Terms'),
'#submit' => array('::downloadFile'),
];
(...)
public function downloadFile() {
//Get file from /file/path/
//return $file
}