Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drop-in paragraph template to display fields as bootstrap accordian
{% extends "paragraph.html.twig" %}
{% block paragraph %}
<div{{ attributes.addClass('panel-group') }} id="accordion-{{ paragraph.id() }}" role="tablist">
{% for field in content %}
{% set field_instance_items = [] %}
{% for key, field_instance in field if key|first != '#' %}
{% set field_instance_items = field_instance_items|merge([field_instance]) %}
{% endfor %}
{% if field_instance_items is not empty %}
<div class="panel panel-default">
<div class="panel-heading" data-toggle="collapse"
data-parent="#accordion-{{ paragraph.id() }}" data-target="#collapse{{ loop.index }}">
<h4 class="panel-title">
<a>
{{ field['#title'] }}
</a>
</h4>
</div>
<div id="collapse{{ loop.index }}" class="panel-collapse collapse">
<div class="panel-body">
{{ field_instance_items }}
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
{% endblock paragraph %}
@NikLP

This comment has been minimized.

Copy link
Owner Author

@NikLP NikLP commented Mar 31, 2017

Note id="accordion-{{ paragraph.id() }}" because potentially multiple per-page.

Attribution:
For getting renderable field values out of {{ content }} @ http://drupal.stackexchange.com/a/208765/160
NB note about compatibility of collapse elements @ http://stackoverflow.com/questions/19236717/how-to-make-a-bootstrap-accordion-collapse-when-clicking-the-header-div
And of course, https://www.w3schools.com/bootstrap/bootstrap_collapse.asp

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