Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Created April 7, 2021 18:11
Show Gist options
  • Save DonRichards/aa4845d8d87ac6414a12aaea3344edaa to your computer and use it in GitHub Desktop.
Save DonRichards/aa4845d8d87ac6414a12aaea3344edaa to your computer and use it in GitHub Desktop.
Within Your theme

2021-04-07_14-05

Download modal

{#
/**
* @file
* Theme override for a field.
*
* To override output, copy the "field.html.twig" from the templates directory
* to your theme's directory and customize it, just like customizing other
* Drupal templates such as page.html.twig or node.html.twig.
*
* Instead of overriding the theming for all fields, you can also just override
* theming for a subset of fields using
* @link themeable Theme hook suggestions. @endlink For example,
* here are some theme hook suggestions that can be used for a field_foo field
* on an article node type:
* - field--node--field-foo--article.html.twig
* - field--node--field-foo.html.twig
* - field--node--article.html.twig
* - field--field-foo.html.twig
* - field--text-with-summary.html.twig
* - field.html.twig
*
* Available variables:
* - attributes: HTML attributes for the containing element.
* - label_hidden: Whether to show the field label or not.
* - title_attributes: HTML attributes for the title.
* - label: The label for the field.
* - multiple: TRUE if a field can contain multiple items.
* - items: List of all the field items. Each item contains:
* - attributes: List of HTML attributes for each item.
* - content: The field item's content.
* - entity_type: The entity type to which the field belongs.
* - field_name: The name of the field.
* - field_type: The type of the field.
* - label_display: The display settings for the label.
*
*
* @see template_preprocess_field()
*/
#}
{%
set classes = [
'field',
'field--name-' ~ field_name|clean_class,
'field--type-' ~ field_type|clean_class,
'field--label-' ~ label_display,
]
%}
{%
set title_classes = [
'field__label',
label_display == 'visually_hidden' ? 'visually-hidden',
]
%}
{% if label_hidden %}
{% if multiple %}
<div{{ attributes.addClass(classes, 'field__items') }}>
{% for item in items %}
<div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
{% endfor %}
</div>
{% else %}
{% for item in items %}
<div{{ attributes.addClass(classes, 'field__item') }}>
{%- if "Download" in item.content|raw -%}
<!-- Button trigger modal -->
<button type="button" class="button button-action" data-toggle="modal" data-target="#exampleModalCenter">Download <i class="fas fa-download" aria-hidden="true"></i>
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Download</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
{# {{ item.content }} #}
{% if item.content['#url'] %}
<a href="{{ item.content['#url'] }}" download="{{ item.content['#url']|raw|render|split('/')|last }}">
{{ item.content['#title'] }} Original File
</a>
{% else %}
<a class="btn disabled" href="">
Link Currently Unavailable
</a>
{% endif %}
<p>Some items may be under copyright. You are responsible for checking Use and Reproduction terms listed for thie item (below). By downloading, you agree to the <a href="/terms-use">Terms of Use.</a></p>
</div>
</div>
</div>
</div>
{%- else -%}
{{ item.content }}
{%- endif -%}
</div>
{% endfor %}
{% endif %}
{% else %}
<div{{ attributes.addClass(classes) }}>
<div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
{% if multiple %}
<div class='field__items'>
{% endif %}
{% for item in items %}
<div{{ item.attributes.addClass('field__item') }}>
{%- if "Download" in item.content|raw -%}
<!-- Button trigger modal -->
<button type="button" class="button button-action" data-toggle="modal" data-target="#exampleModalCenter">Download <i class="fas fa-download" aria-hidden="true"></i>
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Download</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
{# {{ item.content }} #}
{% if item.content['#url'] %}
<a href="{{ item.content['#url'] }}" download="{{ item.content['#url']|raw|render|split('/')|last }}">
{{ item.content['#title'] }} Original File
</a>
{% else %}
<a class="btn disabled" href="">
Link Currently Unavailable
</a>
{% endif %}
<p>Some items may be under copyright. You are responsible for checking Use and Reproduction terms listed for thie item (below). By downloading, you agree to the <a href="/terms-use">Terms of Use.</a></p>
</div>
</div>
</div>
</div>
{%- else -%}
{{ item.content }}
{%- endif -%}
</div>
{% endfor %}
{% if multiple %}
</div>
{% endif %}
</div>
{% endif %}
@seth-shaw-unlv
Copy link

Ah, based on your prompting I discovered that Bootstrap provides its own modal JS as part of the theme. I didn't even think to check our own theme for an included modal capability. 🤦‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment