I am trying to add a link to a managed pdf file to the footer menu. Right now I am using a controller which uses a specific route in order to download the file:
// src/Controller/DownloadController.php
class DownloadController extends ControllerBase {
public function downloadPdf() {
$config = \Drupal::config('example.settings');
$pdf = $config->get('pdf');
if ($pdf) {
$file = File::load($pdf[0]);
$filename = $file->getFileName();
header("Content-Type: application/pdf");
header("Content-Disposition: attachment;filename=$filename");
print file_get_contents($file->getFileUri());
}
}
}
// example.routing.yml
example.download:
path: 'example/download'
defaults:
_controller: '\Drupal\example\Controller\DownloadController::downloadPdf'
_title: 'Download PDF'
requirements:
_permission: 'access content'
After I have activated the module I added a new link named Download PDF
to the footer menu. This link points to example/download
, this seems to work, but I was wondering if there is a better approach. If possible, I would like to add the path to the file to new menu item instead.