Created
June 25, 2020 16:49
-
-
Save piotrpog/fde32772222cef3be6383a2305ea7e47 to your computer and use it in GitHub Desktop.
Attributes table automatically generated from fields grouped within matrix field. More info: http://craftsnippets.com/articles/creating-attributes-table-from-entry-fields-in-craft-cms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% apply spaceless %} | |
{# lightswitch field values #} | |
{% if lighswitchOn is not defined %} | |
{% set lighswitchOn = 'yes' %} | |
{% endif %} | |
{% if lighswitchOff is not defined %} | |
{% set lighswitchOff = 'no' %} | |
{% endif %} | |
{# attributes contents array #} | |
{% set allAttributes = [] %} | |
{% if matrixBlock is defined and matrixBlock is not null %} | |
{% set attributeFields = matrixBlock.getFieldLayout().getFields() %} | |
{% for singleField in attributeFields %} | |
{% switch className(singleField) %} | |
{# lightswitch #} | |
{% case 'craft\\fields\\Lightswitch' %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: matrixBlock[singleField.handle] ? lighswitchOn : lighswitchOff, | |
}]) %} | |
{# plain text #} | |
{% case 'craft\\fields\\PlainText' %} | |
{% if matrixBlock[singleField.handle] is not empty %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: matrixBlock[singleField.handle], | |
}]) %} | |
{% endif %} | |
{# number #} | |
{% case 'craft\\fields\\Number' %} | |
{% if matrixBlock[singleField.handle] is not empty %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: matrixBlock[singleField.handle] ~ (singleField.suffix is not empty ? ' ' ~ singleField.suffix), | |
}]) %} | |
{% endif %} | |
{# dropdown or radio #} | |
{% case 'craft\\fields\\Dropdown' or 'craft\\fields\\RadioButtons' %} | |
{% if matrixBlock[singleField.handle].value is not null %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: matrixBlock[singleField.handle].label, | |
}]) %} | |
{% endif %} | |
{# multiselect or chexkboxes #} | |
{% case 'craft\\fields\\MultiSelect' or 'craft\\fields\\Checkboxes' %} | |
{% if matrixBlock[singleField.handle] is not empty %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: matrixBlock[singleField.handle]|map(option => option.label)|join(', '), | |
}]) %} | |
{% endif %} | |
{# url #} | |
{% case 'craft\\fields\\Url' %} | |
{% if matrixBlock[singleField.handle] is not empty %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: "<a href='#{matrixBlock[singleField.handle]}'>#{matrixBlock[singleField.handle]}</a>", | |
}]) %} | |
{% endif %} | |
{# email #} | |
{% case 'craft\\fields\\Email' %} | |
{% if matrixBlock[singleField.handle] is not empty %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: "<a href='mailto:#{matrixBlock[singleField.handle]}'>#{matrixBlock[singleField.handle]}</a>", | |
}]) %} | |
{% endif %} | |
{# entries and categories #} | |
{% case 'craft\\fields\\Entries' or 'craft\\fields\\Categories' %} | |
{% if matrixBlock[singleField.handle].exists() %} | |
{% set allAttributes = allAttributes|merge([{ | |
class: className(singleField)|split('\\')[2]|lower, | |
label: singleField.name, | |
value: matrixBlock[singleField.handle].all()|map(singleElement => "<a href='#{singleElement.url}'>#{singleElement.title}</a>")|join(', '), | |
}]) %} | |
{% endif %} | |
{% endswitch %} | |
{% endfor %} | |
{% endif %} | |
{# additional attributes injected into component #} | |
{% if additionalAttributes is defined and additionalAttributes is not empty %} | |
{% for additionalSingle in additionalAttributes %} | |
{% set allAttributes = allAttributes|merge([{ | |
label: additionalSingle.label, | |
value: additionalSingle.value, | |
}]) %} | |
{% endfor %} | |
{% endif %} | |
{# output table #} | |
{% if allAttributes is not empty %} | |
<table class="attributes table is-hoverable is-bordered"> | |
<tbody> | |
{% for singleAttribute in allAttributes %} | |
<tr class="{{singleAttribute.class is defined ? 'singleAttribute.class'~'-field'}}"> | |
<td class="attributes__label">{{singleAttribute.label}}</td> | |
<td class="attributes__value">{{singleAttribute.value|raw}}</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
{% endif %} | |
{% endapply %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment