TransWikia.com

Get UI elements of entry in front end

Craft CMS Asked on February 14, 2021

Is is possible to get UI-elements of Craft 3.5 in the front end? Specifically wanting to get hold of the headers I use to separate fields of products.

This is my current code:

{% set layout = product.getFieldLayout() %}
{% for tab in layout.tabs %}
    {% if tab.name == "Detaljer" %}
        {% for field in tab.getFields %}
            {% if not product.getFieldValue(field.handle) is empty %}
                {% switch field.className() %}

                    {# Can I get UI header out of the tab.getFields method? #}
                    {% case "craftfieldsUI-header-element" %}
                        {# Do stuff #}
                    {% case "craftfieldsPlainText" %}
                        <tr>
                            <th>{{ field.name|t }}</th>
                            <td itemprop="value">{{ product.getFieldValue(field.handle )}}</td>
                        </tr> 
                    {% case "craftfieldsCategories" %}
                        <tr>
                            <th>{{ field.name|t }}</th>
                            <td>
                                {% include 'commerce/_blocks/categoryList' with { categories: product.getFieldValue(field.handle) } %}
                            </td>
                        </tr> 
                    {% case "craftfieldsDropdown" %}
                        <tr>
                            <th>{{ field.name|t }}</th>
                            <td>{{ product.getFieldValue(field.handle).label}}</td>
                        </tr>           
                    {% case "craftfieldsLightswitch" %}
                        <tr>
                            <th>{{ field.name|t }}</th>
                            <td>{% include 'commerce/_blocks/fieldLightswitch' with { data: product.getFieldValue(field.handle) } %}</td>
                        </tr>         
                    {% case "craftfieldsNumber" %}
                        <tr>
                            <th>{{ field.name|t }}</th>
                            <td>{{ product.getFieldValue(field.handle) }} {{ field.suffix }}</td>
                        </tr>                                                                                                        
                    {% default %}
                        <tr>
                            <th>{{ field.name|t }}</th>
                            <td>{{ field.className() }}</td>
                        </tr>

                        {# todo: other non implemented fields #}
                {% endswitch %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}   

One Answer

  1. Use the Kint plugin to spit out the current tab to the JS debug console:
{% for tab in layout.tabs %}
    {% if tab.name == "Detaljer" %}

        {{ j(tab) }}

        {% for field in tab.getFields %}

You will see that the structure of the tab-object actually contains the UI-elements as well:

debug output from Kint's j() method displaying headers and fields of the current entry, which is a product

  1. Instead of looping tab.getFields, loop tab.elements:
{% set layout = product.getFieldLayout() %}
{% for tab in layout.tabs %}
    {% if tab.name == "Detaljer" %}

        {% for element in tab.elements %}
            {% switch element.className() %}

            {% case "craftfieldlayoutelementsHeading" %}
                <tr>
                    <th class="field-category" colspan="2"><h5>{{ element.heading }}</h5></th>
                </tr> 

            {% case "craftfieldlayoutelementsCustomField" %}
    
                {% set field = element.getField() %}           
                {% if not product.getFieldValue(field.handle) is empty %}

                    {% switch field.className() %}
                        {% case "craftfieldsPlainText" %}
                            <tr>
                                <th>{{ field.name|t }}</th>
                                <td>{{ product.getFieldValue(field.handle )}}</td>
                            </tr> 
                        {% case "craftfieldsCategories" %}
                            <tr>
                                <th>{{ field.name|t }}</th>
                                <td>
                                    {% include 'commerce/_blocks/categoryList' with { categories: product.getFieldValue(field.handle) } %}
                                </td>
                            </tr> 
                        {% case "craftfieldsDropdown" %}
                            <tr>
                                <th>{{ field.name|t }}</th>
                                <td>{{ product.getFieldValue(field.handle).label}}</td>
                            </tr>           
                        {% case "craftfieldsLightswitch" %}
                            <tr>
                                <th>{{ field.name|t }}</th>
                                <td>{% include 'commerce/_blocks/fieldLightswitch' with { data: product.getFieldValue(field.handle) } %}</td>
                            </tr>         
                        {% case "craftfieldsNumber" %}
                            <tr>
                                <th>{{ field.name|t }}</th>
                                <td>{{ product.getFieldValue(field.handle) }} {{ field.suffix }}</td>
                            </tr>                                                                                                        
                        {% default %}
                            <tr>
                                <th scope="col" itemprop="name">{{ field.name|t }}</th>
                                <td>{{ field.className() }}</td>
                            </tr>

                            {# todo: other non implemented fields #}
                    {% endswitch %}
                {% endif %}

            {% endswitch %}
        {% endfor %}

    {% endif %}
{% endfor %}   

Answered by nitech on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP