Expand my Community achievements bar.

SOLVED

Creating a multilist field in touch dialog and printing its values.

Avatar

Level 2

I have created an AEM multifield in a touch dialog. But I am not able to read the values from the multifield.

This is the xml of the dialog.

<?xml version="1.0" encoding="UTF-8"?>

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"

    jcr:primaryType="nt:unstructured"

    jcr:title="globalFooter"

    sling:resourceType="cq/gui/components/authoring/dialog"

    helpPath="en/cq/current/wcm/default_components.html#Text">

    <content

        jcr:primaryType="nt:unstructured"

        sling:resourceType="granite/ui/components/coral/foundation/container">

        <items jcr:primaryType="nt:unstructured">

            <tabs

                jcr:primaryType="nt:unstructured"

                sling:resourceType="granite/ui/components/coral/foundation/tabs"

                maximized="{Boolean}true">

                <items jcr:primaryType="nt:unstructured">

                 

                    <navLinks

                        jcr:primaryType="nt:unstructured"

                        jcr:title="Navbar Links"

                        sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"

                        margin="{Boolean}false">

                        <items jcr:primaryType="nt:unstructured">

                            <column

                                jcr:primaryType="nt:unstructured"

                                sling:resourceType="granite/ui/components/coral/foundation/container">

                                <items jcr:primaryType="nt:unstructured">

                                    <scripts

                                        jcr:primaryType="nt:unstructured"

                                        sling:resourceType="granite/ui/components/coral/foundation/form/multifield"

                                        composite="{Boolean}true"

                                        fieldDescription="Click Add Field to add more"

                                        fieldLabel="Add Nav Links">

                                        <field

                                            jcr:primaryType="nt:unstructured"

                                            sling:resourceType="granite/ui/components/coral/foundation/form/fieldset"

                                            name="./navLinks">

                                            <items jcr:primaryType="nt:unstructured">

                                                <column

                                                    jcr:primaryType="nt:unstructured"

                                                    sling:resourceType="granite/ui/components/coral/foundation/container">

                                                    <items jcr:primaryType="nt:unstructured">

                                                        <linkText

                                                            jcr:primaryType="nt:unstructured"

                                                            sling:resourceType="granite/ui/components/foundation/form/textfield"

                                                            fieldDescription="The text to be displayed in the link"

                                                            fieldLabel="Link Text"

                                                            name="./linkText"/>

                                                        <linkUrl

                                                            jcr:primaryType="nt:unstructured"

                                                            sling:resourceType="granite/ui/components/foundation/form/pathbrowser"

                                                            fieldDescription="Enter the link URL"

                                                            fieldLabel="Link URL"

                                                            name="./linkUrl"/>

                                                    </items>

                                                </column>

                                            </items>

                                        </field>

                                    </scripts>

                                </items>

                            </column>

                        </items>

                    </navLinks>

                </items>

            </tabs>

        </items>

    </content>

</jcr:root>

I have created sling model classes to fetch the field values.

package com.techaspect.core.models;

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.Optional;

import org.apache.sling.models.annotations.injectorspecific.Self;

import org.osgi.service.component.annotations.Activate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@Model(adaptables = Resource.class)

public class NavLinkListModel {

Logger logger = LoggerFactory.getLogger(NavLinkListModel.class);

@Inject

@Optional

private List<NavLinksBeanModel> childList;

@Self

Resource resource;

@Activate

private void init() {

Iterator<Resource> children = resource.listChildren();

while(children.hasNext()) {

childList.add(children.next().adaptTo(NavLinksBeanModel.class));

}

System.out.println("Test");

}

public Collection<NavLinksBeanModel> getChildList() {

return childList;

}

}

package com.techaspect.core.models;

import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Model;

@Model(adaptables = Resource.class)

public class NavLinksBeanModel {

@Inject

private String linkText;

@Inject

private String linkUrl;

public String getLinkText() {

return linkText;

}

public String getLinkUrl() {

return linkUrl;

}

@Override

public String toString() {

return "NavLinksBeanModel [linkText=" + linkText + ", linkUrl=" + linkUrl + "]";

}

}

And have used the following in html to print them

<sly data-sly-use.object1 = "com.techaspect.core.models.NavLinkListModel"}

<ul data-sly-list.multi = "${object1.childList} }">

<li>

<a href="${multi.linkUrl}">${multi.linkText}</a>

</li>

</ul>

</sly>

Kindly someone help me as to where I am doing wrong.

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this article too - it deals with a MF:

Scott's Digital Community: Creating a HTL Repeating Data Set 6.3 Component that uses Sling Models

This will show you how to inject the Granite/CORAL MF into the SLing MOdel and how to display the values in HTL.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

See this article too - it deals with a MF:

Scott's Digital Community: Creating a HTL Repeating Data Set 6.3 Component that uses Sling Models

This will show you how to inject the Granite/CORAL MF into the SLing MOdel and how to display the values in HTL.