{% macro includeVariant( context, parentTemplate, outerClass, outerAttributes) %}
{% set editmode = context.editmode %}
{% set document = context.document %}
{% set config = context.config %}
{% if context.brick is defined %}
{% set brick = context.brick %}
{% endif %}
{% if config.disabled|default(null) and editmode %}
<div class="basisbundle_edit_line basisbundle_edit_info">
<tt>{{ parentTemplate }}</tt>
<p><b>Warning: Areabrick is disabled</b> ({{ config.disabled }})</p>
</div>
{% endif %}
{% set variantStore = [] %}
{% set availableVariants = config.variants|default([]) %}
{% for id,variant in availableVariants %}
{% if variant is iterable and variant.variant_name is defined %}
{% set varianteSelectable = true %}
{% if variant.variant_disable_user_select|default(false) %}
{% set varianteSelectable = false %}
{% endif %}
{% if varianteSelectable %}
{% set variantStore = variantStore|merge([[id,variant.variant_name]]) %}
{% endif %}
{% endif %}
{% endfor %}
{% if context.variant|default(false) %}
{# when a fixed variant is set, reduce the set of available variants #}
{% for id,variant in availableVariants %}
{% if id == context.variant %}
{% set variantStore = [[id,variant.variant_name]] %}
{% endif %}
{% endfor %}
{% endif %}
{% set defaultVariant = context.variant|default( config.variant_default|default('default')) %}
{% set selectedVariant = context.variant|default( pimcore_select('variant').getValue()|default(defaultVariant)) %}
{% if editmode %}
{% if pimcore_select('variant').isEmpty() or (pimcore_select('variant').getValue() != selectedVariant) %}
{% do pimcore_select("variant").setDataFromResource(defaultVariant) %}
{% endif %}
{% if variantStore|length > 1 %}
<div class="basisbundle_edit_line">
<div class="basisbundle_edit_entry">
<div class="basisbundle_edit_label">{{ config.variant_label|default('Choose variant') }}:</div>
<div class="basisbundle_edit_input">{{ pimcore_select('variant',{store:variantStore,reload:true,class:'m-0'}) }}</div>
</div>
</div>
{% endif %}
{% endif %}
{% if not availableVariants %}
<div class="basisbundle_edit_line basisbundle_edit_error">
No variants available for {{ parentTemplate }}!
</div>
{% elseif not attribute( availableVariants, selectedVariant) is defined %}
{% if selectedVariant != defaultVariant %}
{% if editmode %}
<div class="basisbundle_edit_line basisbundle_edit_error">
Selected variant "<em>{{selectedVariant}}</em>" does not exist, using: "<em>{{defaultVariant}}</em>"!
</div>
{% endif %}
{% set selectedVariant = defaultVariant %}
{% else %}
{% if editmode %}
<div class="basisbundle_edit_line basisbundle_edit_error">
Selected variant "<em>{{selectedVariant}}</em>" does not exist!
</div>
{% endif %}
{% endif %}
{% elseif not availableVariants[selectedVariant] is iterable %}
{% if editmode %}
<div class="basisbundle_edit_line basisbundle_edit_error">
Selected variant "<em>{{selectedVariant}}</em>" has no valid definition:"
</div>
{% endif %}
{% endif %}
{% if attribute( availableVariants, selectedVariant) is defined and availableVariants[selectedVariant] is iterable %}
{% set selectedTemplate = availableVariants[selectedVariant]['variant_template']|default(selectedVariant) %}
{% set isAppTemplate = availableVariants[selectedVariant]['variant_template_from_app']|default(false) %}
{% if parentTemplate starts with '@' %}
{% set parentTemplateNamespace = parentTemplate|split('/')[0] %}
{% set parentTemplatePath = parentTemplate|split('/')|slice(1,-1)|join('/') %}
{% elseif 'Bundle:' in parentTemplate %}
{% set parentTemplateNamespace = '@' ~ parentTemplate|split('Bundle:')[0] %}
{% set parentTemplatePath = parentTemplate|split('Bundle:')[1] %}
{% if ':' in parentTemplatePath %}
{% set parentTemplatePath = parentTemplatePath|split(':')[0] %}
{% else %}
{% set parentTemplatePath = parentTemplatePath|split('/')|slice(0,-1)|join('/') %}
{% endif %}
{% else %}
{# ??? #}
{% endif %}
{% if isAppTemplate %}
{% set parentTemplateNamespace = '@App' %}
{% endif %}
{% set areaBase = parentTemplateNamespace ~ '/' ~ parentTemplatePath ~ '/' %}
{#
<!-- parentTemplate: "{{ parentTemplate }}" -->
<!-- parentTemplateNamespace: "{{ parentTemplateNamespace }}" -->
<!-- parentTemplatePath: "{{ parentTemplatePath }}" -->
<!-- isAppTemplate: "{{ isAppTemplate }}" -->
<!-- areaBase: "{{ areaBase }}" -->
#}
{% set variantConfig = availableVariants[selectedVariant] %}
{% if variantConfig.disabled|default(null) and editmode %}
<div class="basisbundle_edit_line basisbundle_edit_info">
<tt>{{ parentTemplate }}</tt> / <tt>{{ selectedVariant }}</tt>
<p><b>Warning: Selected Variant is disabled</b> ({{ variantConfig.disabled }})</p>
</div>
{% endif %}
{% if not variantConfig.linkmodul is defined %}
{# linkmodul nicht in variante definiert => setzen #}
{% set variantConfig = variantConfig|merge({linkmodul:config.linkmodul}) %}
{% endif %}
{% if not variantConfig.image_module is defined %}
{# image_module nicht in variante definiert => setzen #}
{% set variantConfig = variantConfig|merge({image_module:config.image_module}) %}
{% endif %}
{% set overwriteKeys = ['headlines','lead','wysiwyg'] %}
{% for overwriteKey in overwriteKeys %}
{% if context[overwriteKey] is defined %}
{% set variantConfig = variantConfig|merge({(overwriteKey):context[overwriteKey]}) %}
{% endif %}
{% endfor %}
{% set mergeKeys = ['image_module'] %}
{% for mergeKey in mergeKeys %}
{% if context[mergeKey] is defined %}
{% set variantConfig = variantConfig|merge({(mergeKey):variantConfig[mergeKey]|merge(context[mergeKey])}) %}
{% endif %}
{% endfor %}
{% set wrapper = null %}
{% if editmode and variantConfig.editmode_wrapper is defined %}
{% set wrapper = variantConfig.editmode_wrapper %}
{% elseif not editmode and variantConfig.frontend_wrapper is defined %}
{% set wrapper = variantConfig.frontend_wrapper %}
{% elseif variantConfig.wrapper is defined %}
{% set wrapper = variantConfig.wrapper %}
{% endif %}
{# <!--- start config "{{ selectedVariant }}" variant-{{ selectedTemplate }} --> #}
{% set passedParams = { config: variantConfig} %}
{% for passKey in config.pass_params|default(null) %}
{% if passKey in _context.context|keys %}
{% set passedParams = passedParams|merge({ (passKey): _context.context[passKey]}) %}
{% endif %}
{% endfor %}
{% for passKey in variantConfig.pass_params|default(null) %}
{% if passKey in _context.context|keys %}
{% set passedParams = passedParams|merge({ (passKey): _context.context[passKey]}) %}
{% endif %}
{% endfor %}
{% if wrapper %}
{# a wrapper is defined #}
{% import '@FoxHabbitBasis/Macro/wrap.html.twig' as wrap_macro %}
{{ wrap_macro.wrapperOpen( wrapper, _context, outerClass, outerAttributes) }}
{% include areaBase ~ 'variant-' ~ selectedTemplate ~ '.html.twig' with passedParams %}
{{ wrap_macro.wrapperClose( wrapper) }}
{% else %}
{# no wrapper => include directly #}
{% include areaBase ~ 'variant-' ~ selectedTemplate ~ '.html.twig' with passedParams %}
{% endif %}
{# <!--- end variant-{{ selectedTemplate }} --> #}
{% endif %}
{% endmacro %}