Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Populate dropdown with tag values

Avatar

Level 6

Hi ,

I have requirement to populate the tags values to drop down in AEM 6.2 and in AEM 5.6.1. 

Any suggestions please.

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 10
6 Replies

Avatar

Level 6

Hi Prince,

Thanks for the response. 

 I guess that one is like populating the cq:tags from the jcr content.I am trying to populate specific section of tags dropdown.

 

Thanks

Avatar

Level 5

Similiar requirement i have to achieve, please let me know if there is any way ?

Avatar

Correct answer by
Level 10

Avatar

Administrator

Adding on top of what scott has mentioned, i would to share with you one article which really help you start developing with TagManager APis

Link:- http://labs.6dglobal.com/blog/2016-09-05/a-primer-on-programmatic-tagging-in-aem/

Link:- http://www.programcreek.com/java-api-examples/index.php?api=com.day.cq.tagging.TagManager

~kautuk



Kautuk Sahni

Avatar

Level 2
<dropDownTags        cq:showOnCreate="{Boolean}true"        jcr:primaryType="nt:unstructured"        sling:resourceType="granite/ui/components/foundation/form/autocomplete"        allowBulkEdit="{Boolean}true"        cq-msm-lockable="cq:tags"        emptyText="Add Tags"        fieldLabel="Tag"        metaType="tags"        multiple="{Boolean}true"        name="./tag"        renderReadOnly="{Boolean}true">    <datasource jcr:primaryType="nt:unstructured"                sling:resourceType="datasourceTags"/>    <values            jcr:primaryType="nt:unstructured"            sling:resourceType="granite/ui/components/foundation/form/autocomplete/tags"            multiline="{Boolean}true"/>    <options            jcr:primaryType="nt:unstructured"            sling:resourceType="granite/ui/components/foundation/form/autocomplete/list"/></dropDownTags>

 

Servlet

@SlingServlet(methods = "GET",        resourceTypes = "datasourceTags")public class DatasourceTags extends SlingAllMethodsServlet {private static final Logger LOGGER = LoggerFactory.getLogger(DatasourceTheme.class);    protected void doGet(SlingHttpServletRequest slingRequest, SlingHttpServletResponse response)throws ServletException, IOException {final ResourceResolver resourceResolver = slingRequest.getResourceResolver();        TagManager tm = resourceResolver.adaptTo(TagManager.class);        List<Tag> tags = new ArrayList<Tag>();        String path = "";        if (slingRequest.getRequestParameter("item") != null) { path = slingRequest.getRequestParameter("item").getString();        } else { String referer = slingRequest.getHeader("Referer");            if (StringUtils.isNotEmpty(referer)) { path = referer.substring(referer.indexOf(".html"));            } }if (path != null) { String[] parts = path.split("/");            if (resourceResolver.getResource("/etc/tags/tags_theme/" + parts[2]) != null) { Tag pageTags = resourceResolver.getResource("/etc/tags/tags_theme/" + parts[2]).adaptTo(Tag.class);                if (pageTags != null) {for (Tag tag : Lists.newArrayList(pageTags.listChildren())) { tags.add(tag);                    } } } }final Locale locale = slingRequest.getLocale();        final ResourceResolver resolver = resourceResolver;        @SuppressWarnings("unchecked") DataSource ds = new SimpleDataSource(new TransformIterator(tags.iterator(), new Transformer() {public Object transform(Object o) { Tag tag = (Tag) o;                String tagId = tag.getTagID();                ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());                vm.put("value", tagId);                vm.put("text", tag.getTitle(locale));                return new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm);            } }));        slingRequest.setAttribute(DataSource.class.getName(), ds);    } }

Hi, you need to feed a ValueMap thanks to a data source in a servlet that extends SlingAllMethodsServlet then use this datasource as parameter in your _cq_dialog/.content.xml. Don't forget to replace 

tags_theme

by your own section name.

Was this usefull ?