1

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.

6
  • You know you can just add a regular menu link item that points to a file, right?
    – Kevin
    Jul 15, 2017 at 19:10
  • 1
    I see what you are doing here, and I would politely ask if you could find a way to write a Media extension that forces a file download in the browser instead of go to the media view page (e.g. media/{id}/download). That would be way more flexible and something I have wanted to do but not had time to write.
    – Kevin
    Jul 15, 2017 at 19:11
  • @Kevin - Great idea, I will look into this when I'm done with my current project. I'll keep you posted =) Jul 15, 2017 at 21:38
  • @Kevin - Are you talking about writing an extension to an existing module? I'm not able to goto media/id for any file in my current installation? Jul 15, 2017 at 23:37
  • 1
    You need the media modules if you aren't on 8.4.x. It turns files into media entities and lots of other things.
    – Kevin
    Jul 15, 2017 at 23:38

1 Answer 1

1

For menu items, you can quickly configure the contributed menu_link_attributes module to include a download link attribute.

It's a 'low code' option if there ever was one; the module even has a user interface right on your site for customizing your configuration:

  1. add the module: composer require drupal/menu_link_attributes
  2. enable the module: drush en menu_link_attributes
  3. visit the 'manage attributes' configuration page at /admin/config/menu_link_attributes/config
  4. add the following lines to the end of the edit textarea (note the indentation; it should have 2 spaces in front of the first line and 4 in front of the second line):
      download:
        filename: ''
    
  5. add a menu item in the menu of your choice (example using the main menu: /admin/structure/menu/manage/main/add)
  6. in the 'attributes' section of the 'Add menu link' page, enter a filename in the 'download' textfield and save the new link with the other desired values
  7. export your configuration to code: drush cex

Your new configuration should look something like the following:

attributes:
  class:
    label: ''
    description: ''
  download:
    description: 'Enter a name to assign to the file. Leave this field blank to exclude the download attribute.'
    filename: ''
  target:
    label: ''
    description: ''
    options:
      _blank: 'New window (_blank)'
      _self: 'Same window (_self)'
    default_value: ''

I've added a merge request to include the download attribute out of the box, tho I've also indicated it is a minor request. The MR implements the download attributed as described in MDN documentation.

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.