Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Last active April 17, 2025 05:05
Show Gist options
  • Save megclaypool/2dda29bb2e45cc58b4eb4e4deb0e6a61 to your computer and use it in GitHub Desktop.
Save megclaypool/2dda29bb2e45cc58b4eb4e4deb0e6a61 to your computer and use it in GitHub Desktop.
  1. Install the Media Entity Download module.
    • composer require drupal/media_entity_download
  2. Create a FileDownloadLink field formatter:
    • either use an existing custom module on your site or create a new one
    • create a file at whatever_module/src/Plugin/Field/FieldFormatter/FileDownloadLink.php
    • paste in the contents of the attached FileDownloadLink.php
  3. In the GUI create a paragraph that has two fields:
    1. a Media Library reference field: file_from_media_library
    2. a short plain text field: button_text
  4. Set the display for the media file to File Download Link
  5. Put an instance of that paragraph on some node, so you can grab the name of the paragraph template.
  6. Create a paragraph template. In it put:
{% extends 'paragraph.html.twig' %}

{% block content %}
  {% set link = content.field_file_from_media_library[0]['#plain_text'] %}
  {% set label = content.field_button_text['#items'].value %}

  <a href="{{ link }}" class="button">{{ label }}</a>
{% endblock content %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment