Provide page for download and change content-type

Hi, I’m trying to provide an iCalendar file for download which content is generated with Twig.
So I crated a new template “icalendar.html.twig” which actually contains just one line saying

{{ page.content }}

and created a new page “/calendar/” of that template with content

title: calendar
twig: true
markdown: false
cache_enable: false
visible: false
template: icalendar
type: event
by: date
dir: asc
limit: '20’
pagination: false

{% set events = page.collection({'items':{'@taxonomy.type':'event'}}).order('date', 'asc') %}
... and so on.

Now, what I get returned looks like an iCalendar file.
However, it cannot be parsed by some calendar software like Thunderbird/Lightning and I think that’s due to two reasons:
#1 If I enter /calendar/calendar.ics into the browser address bar, the file opens in the tab rather than providing me to download it.
#2 The content-type is text/html instead of text/calendar

To fix the 2nd problem I copied the /system/config media.yaml to /user/config/media.yaml and added

    type: file
    mime: text/calendar

But this seems to be totaly ignored as the content-type is still text/tml.

So my questions are:

  • Did I made a completey worng turn somewhere here?
  • Or how do I change the content-type and how do I achieve to provide the file as a download instead opening it in the browser tab?

Best regards,

You should add the type .ics to the pages: types: in the system config. Also you should rename your twig to icalendar.ics.twig. I think that should do it!

OK, I renamed the twig template to icalendar.ics.twig and added the ics file type to pages: types: in both, the /system/config/system.yaml and /user/config/system.yaml
But the content-type is still text/html and it still opens in the browser tab.

At least I figured out the iCalendar parsing error was not related to this circumstances, so it might not be perfect, but it works now.


Hmm, I think the media types are not used at all by the page types when returning, this is a bug and should be sorted. I’ll add an issue here: regarding this.