Skip to content

Get category data from id in knockout html template

State of the art

Using a custom type for the page builder, i’m generating this html

<div class="gamme-content">
    <h3 data-element="title">Discover our others products !</h3>
     <p data-element="category_list">2,1</p>
</div>

From this template

<!--master.html-->
<div attr="data.main.attributes"
     ko-style="data.main.style"
     css="data.main.css"
>
    <div class="gamme-content">
        <h3 attr="data.title.attributes"
            html="data.title.html"></h3>

        <p attr="data.category_list.attributes"
           html="data.category_list.html"></p>

    </div>
</div>

The 2 and 1 are matching category ids generated from custom model config

      <field name="category_list" formElement="multiselect">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">CpyWidgetModelConfigSourceCategoriesOptionList</item>
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
                    <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                    <item name="dataScope" xsi:type="string">category_list</item>
                    <item name="label" xsi:type="string">Categories</item>
                    <item name="filterOptions" xsi:type="boolean">true</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="chipsEnabled" xsi:type="boolean">true</item>
                    <item name="multiple" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

Issue

But what i would really need to do is to retrieve data from these categories like title and image.

If i try to pass them as data in my model the model is failing…so i’m coming to the conclusion that it has to be done somehow by adding some logic to the master.html template…but i really don’t know how, any ideas how i can get to print the title in the master.html template using only the category ids ?