Expand my Community achievements bar.

How to create navigation root multiple

Avatar

Level 2

I want to create so user can choose more then one root for navigation here

stefanjank_0-1657790059629.png

I'm trying with multifield but doesn't work at all

4 Replies

Avatar

Community Advisor

@stefanjank I created a similar custom component and able to read the navigation root and the exclude root level as well within the sling model for the same.

shaileshbassi_2-1657803933231.png

Can you provide the error which you are facing which including the same in multifield.

Thanks

 

Avatar

Level 2

Can you share a component xml and Java code? Im trying to achieve this so render in react

Avatar

Community Advisor
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/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="Custom Component"
    sling:resourceType="cq/gui/components/authoring/dialog">
    <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">
                    <properties
                        jcr:primaryType="nt:unstructured"
                        jcr:title="Properties"
                        sling:resourceType="granite/ui/components/coral/foundation/container"
                        margin="{Boolean}true">
                        <items jcr:primaryType="nt:unstructured">
                            <columns
                                jcr:primaryType="nt:unstructured"
                                sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
                                margin="{Boolean}true">
                                <items jcr:primaryType="nt:unstructured">
                                    <column
                                        jcr:primaryType="nt:unstructured"
                                        sling:resourceType="granite/ui/components/coral/foundation/container">
                                        <items jcr:primaryType="nt:unstructured">
                                            <multifield
                                                jcr:primaryType="nt:unstructured"
                                                sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
                                                composite="{Boolean}true"
                                                fieldLabel="Click to add">
                                                <field
                                                    granite:class=""
                                                    jcr:primaryType="nt:unstructured"
                                                    sling:resourceType="granite/ui/components/coral/foundation/container"
                                                    name="./container">
                                                    <items jcr:primaryType="nt:unstructured">
                                                        <navigationRoot
                                                            jcr:primaryType="nt:unstructured"
                                                            sling:resourceType="cq/gui/components/coral/common/form/pagefield"
                                                            fieldDescription="The root page from which to build the navigation. Can be a blueprint master, language master or regular page."
                                                            fieldLabel="Navigation Root"
                                                            name="./navigationRoot"
                                                            required="{Boolean}true"
                                                            rootPath="/content"/>
                                                        <structureStart
                                                            jcr:primaryType="nt:unstructured"
                                                            sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"
                                                            fieldDescription="The levels below the navigation root that are to be excluded. To include the navigation root, enter 0."
                                                            fieldLabel="Exclude Root Levels"
                                                            max="100"
                                                            min="0"
                                                            name="./structureStart"
                                                            step="1"/>
                                                    </items>
                                                </field>
                                            </multifield>
                                        </items>
                                    </column>
                                </items>
                            </columns>
                        </items>
                    </properties>
                </items>
            </tabs>
        </items>
    </content>
</jcr:root>
package com.mysitenew2.core.models;


import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import mysitenew2.core.utils.NavLinkBean;

@Model(adaptables = { Resource.class,
		SlingHttpServletRequest.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MultiField2 {

	private static final Logger LOG = LoggerFactory.getLogger(MultiField2.class);

	@SlingObject
	Resource componentResource;

	List<NavLinkBean> list;

	@PostConstruct
	protected void init() {
		list = setNavigationLinks();
	}

	public List<NavLinkBean> getList() {
		return this.list;
	}

	public List<NavLinkBean> setNavigationLinks() {

		List<NavLinkBean> navigationLinks = new ArrayList<>();
		try {
			Resource navLinks = componentResource.getChild("container");
			if (navLinks != null && navLinks.hasChildren()) {

				for (Resource nav : navLinks.getChildren()) {
					String navigationRoot = nav.getValueMap().get("navigationRoot", String.class);
					String structureStart = nav.getValueMap().get("structureStart", String.class);
					navLinkBean.setNavigationRoot(navigationRoot);
					navLinkBean.setStructureStart(structureStart);
					//you can write the custom logic as per the business requirement
				}
			}
		} catch (Exception e) {
			LOG.error("\n ERROR while getting navigation links {} ", e.getMessage());
		}
		return navigationLinks;

	}
}

@stefanjank Hope this helps!

Thanks