Creating a link to download a file with Online Editor in eZ Publish

I've read a good article in french showing a way to insert direct download link to a file in eZ Publish and Online Editor. This post explains how to add a target view in Online Editor and how to use the Redirect Operator in this view to be redirected to download a file. Although I used this method, I don't think it's a good practice even if it's probably the most obvious, because templates should only be used for presentation. So I thought about another solution without redirection that only uses two features of eZ Publish / Online Editor :

First, declare a class for the <link /> tag with few lines like these in settings/override/content.ini.append.php :


In extension/ezdhtml/settings/content.ini.append, I also add this :


It's not required, but with these lines, Online Editor will display "Téléchargement" in the "Class" field which is more user friendly for french users ;-)

Then create an override of the template content/datatype/view/ezxmltags/link.tpl for example in override/templates/link_download.tpl in your design with the following code :

{def $n='' $attribute='' $url=false() $protocols=array('http', 'file', 'ftp', 'mailto', 'https')
}{if $protocols|contains( $href|explode(':')|extract_left(1))not()
   }{set $n=fetch(content, node, hash(node_path, $href))
   }{if and($n, $n.object.class_identifier|eq('file'))
   }{set $attribute=$n.data_map.file
   }{set $url=concat( '/content/download/', $attribute.contentobject_id, '/', $,'/version/', $attribute.version , '/file/', $attribute.content.original_filename|urlencode )
   }{/if}{/if}{if $url|not()}{set $url=$href}{/if}<a href={$url|ezurl}{section show=$id} id="{$id}"{/section}{section show=$title} title="{$title}"{/section}{section show=$target} target="{$target}"{/section}{section show=ne($classification|trim,'')} class="{$classification|wash}"{/section}>{$content}</a>{undef $n $attribute $url $protocols}

Finally, just add this override condition in settings/<your_siteaccess>/override.ini.append.php :


After clearing the cache, the new template will be used if the link has "download" in the class attribute and if it's not a full link (with a scheme protocol), it will try to generate a link to download the file. It can probably be improved by putting some datas in settings (object class(es), protocols list,…)… The indentation is weird in order to be more readable without inserting unnecessary space the HTML code.

Créer un lien vers un fichier à télécharger dans eZ Publish / Online Editor

So with this configuration, if you want to insert a direct link to download a file in eZ Publish, you just have to choose the download class ( Téléchargement for me) and eZ Publish generates the right link to download.