Link Vault for Craft CMS - Template Variables

downloadURL

The download URL accepts two parameters:

Parameters

  • file (required) : This may either be an instance of an AssetFileModel or it may be a string path to a file on the server.
  • additional parameters : This is an array of custom fields or other variables to be saved to the download record.The downloadAs option changes the filename that will be used for the download.

Sample Code

{# Example 1: Passing an AssetFileModel instance. #}
{% for download in entry.downloadableAssets %}
    <a href="{{ craft.linkVault.downloadURL(download, {downloadAs: 'custom-filename.zip'}) }}" >Download This</a>
{% endfor %}

{# Example 2: Passing a system path. #}
{% for download in entry.downloadableAssets %}
    {% set basePath = craft.config.environmentVariables.basePath %}
    {% set fileFolder = '/'~download.source.settings['path']~download.getFolder().path %}
	{% set filePath = basePath ~ fileFolder ~ download.filename %}
    <a href="{{ craft.linkVault.downloadURL(filePath) }}" >Download This</a>
{% endfor %}

{# Example 3: A hard-coded full system path. #}
<a href="{{ craft.linkVault.downloadURL('/home/user1337/www/uploads/songs/love.mp3') }}" >Download This</a>

Passing an instance of an AssetFileModel is the simplest way to create a Link Vault download URL. This method also works for files stored on an S3 source.

Below are some examples that make use of the second parameter to pass along element IDs. In Craft, entries and assets are both have unique element IDs. It is entirely up to you what you store in this column. It is for informational use only.

Sample Code

{# Example 4: The asset's parent entry's ID is stored in the elementId column. #}
{% for download in entry.downloadableAssets %}
    <a href="{{ craft.linkVault.downloadURL(download, {elementId : entry.id}) }}" >Download This</a>
{% endfor %}

{# Example 5: The asset's ID is stored in the elementId column. #}
{% for download in entry.downloadableAssets %}
    <a href="{{ craft.linkVault.downloadURL(download, {elementId : download.id}) }}" >Download This</a>
{% endfor %}

You can create custom fields to store any data you like with Link Vault. These fields are created from the Link Vault area in the control panel. Once you do create a field, just use the field's handle in the array parameter.

Sample Code

{# Example 6: Passing along a value for a user-defined field. #}
{% for download in entry.downloadableAssets %}
    <a href="{{ craft.linkVault.downloadURL(download, {userEmail : craft.session.getUser().email}) }}" >Download This</a>
{% endfor %}

totalDownloads

The totalDownloads variable returns the total downloads for a given set of criteria. It is very similar to the downloadURL variable except it only has one parameter.

Parameters

  • mixed : An instance of an AssetFileModel, an array of parameters, string containing the full system path to a file.

Sample Code

{# Example 7: Passing an AssetFileModel. #}
{% for download in entry.downloadableAssets %}
    <p>The {{ download.filename }} file has been downloaded {{ craft.linkVault.totalDownloads(download) }} times!</p>
{% endfor %}

{# Example 8: Passing an array of parameters. #}
{% for download in entry.downloadableAssets %}
    <p>Your bird.txt Downloads: {{ craft.linkVault.totalDownloads({userId:craft.session.getUser().id, fileName:"bird.txt" }) }}</p>
{% endfor %}

{# Example 9: Passing a string containing the full system path. #}
Total face.gif downloads: {{ craft.linkVault.totalDownloads('/home/user1337/www/uploads/face.gif') }}

fileSize

The fileSize template variable fetches a human-readable file size string for a specified file. This can be used for server files not stored in Craft as assets though it will work with asset files as well. For asset elements, the native {{ file.size|filesize }} may be preferable.

Sample Code

{# Example 10: Passing a file path. #}
bees.jpg is {{ craft.linkVault.fileSize('/home/user1337/hidden-files/bees.jpg') }}.

{# Example 11: Passing an instance of an AssetFileModel #}
{{ file.filename }} is {{ craft.linkVault.fileSize(file) }}.

downloads

The downloads template variable fetches download records based on the specified criteria. Download records are Craft elements 

Sample Code

{# Example 12: Fetch the ten most recent download records for cheese.mpg. #}
{% for record in craft.linkVault.downloads.fileName('cheese.mpg').limit(10) %}
	<p>User {{ record.userId }} downloaded it on {{ record.dateCreated }}</p>
{% endfor %}

{# Example 13: Fetch 5 most recent downloads that occurred prior to March 1, 2016. #}
{% for record in craft.linkVault.downloads.before('2016-03-01').limit(10) %}
	<p>User {{ record.userId }} downloaded {{ record.filename }} before March 1.</p>
{% endfor %}

{# Example 14: Fetch records based on custom field value. In this example, assume existence of "downloadPage" field. #}
{% for record in craft.linkVault.downloads.downloadPage('super-mega-rockstar/free-songs') %}
    <p>User {{ record.userId }} downloaded {{ record.filename }} song file on {{ record.dateCreated }}</p>
{% endfor %}

leechAttempts

The leechAttempts template variable works in the exact same manner as the downloads variable except that it only return leech attempt records.

records

The records template variables works in the same manner as downloads and leechAttempts variables except it will return all records regardless of type.