To start with lets understand how we get the related content fragment of a specific model using a query builder.
First we start with adding the specific properties to the map and running a query:
Map<String, String> map = new HashMap<>();
map.put("type", "dam:Asset");
map.put("path", "/content/dam");
map.put("First_property", "jcr:content/contentFragment");
map.put("First_property.value", "true");
map.put("Second_property", "jcr:content/data/cq:model");
map.put("Second_property.value", "conf/fragmentexamples/settings/dam/cfm/models/<your-model-name>");
map.put("property.and", "true");
map.put("p.limit", "-1");
Once we are done creating the map we will create our query variable using the request:
QueryBuilder queryBuilder = request.getResourceResolver().adaptTo(QueryBuilder.class);
Now we will get the results using query and the map using Predicategroup:Query query = queryBuilder.createQuery(PredicateGroup.create(map),
Now we will get the results using query and the map using Predicategroup:Query query = queryBuilder.createQuery(PredicateGroup.create(map),
request.getResourceResolver().adaptTo(Session.class));
final SearchResult result = query.getResult();
This result variable will hold all the Content fragments used by the specified content model at location /content/dam(we defined while creating the map).
Now we can iterate on the results and get the specified resource and adapt it to the ContentFragment interface which holds all the content fragment related APIs.
In above code we have used following content fragment APIs:
1. Adapting a resource to Content Fragment.
2. Once we adapt to Content Fragment we get the object of CF which give the content fragment name i.e contentFragmentName.
3. Once we get the content fragment object, we can get all the properties associated with that content fragment (master node) by creating an iterator on CF object of type ContentElement.
4. We can iterate on the contentElement and get the property name in tagElement and it’s content in elementContent in above created variables.
The above code snippet provides details only related to the master of the content fragment. To get the details of the variations we can use the below API and the elements and other details:
Iterator<VariationDef> variationIterator = cf.listAllVariations();
while (variationIterator.hasNext()) {
String variationName = variationIterator.next().getName();
}
Reference: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/adobe/cq/dam/cfm/ContentFragment.html
final SearchResult result = query.getResult();
This result variable will hold all the Content fragments used by the specified content model at location /content/dam(we defined while creating the map).
Now we can iterate on the results and get the specified resource and adapt it to the ContentFragment interface which holds all the content fragment related APIs.
for (Hit hit : result.getHits()) {
ContentFragment cf = hit.getResource().adaptTo(ContentFragment.class);
String contentFragmentName = cf.getName();
Iterator<ContentElement> contentElement = cf.getElements();
while (contentElement.hasNext()) {
ContentElement contentElementObject = contentElement.next();
String tagElement = contentElementObject.getName().toString();
String elementContent = contentElementObject.getContent();
}
}
In above code we have used following content fragment APIs:
1. Adapting a resource to Content Fragment.
2. Once we adapt to Content Fragment we get the object of CF which give the content fragment name i.e contentFragmentName.
3. Once we get the content fragment object, we can get all the properties associated with that content fragment (master node) by creating an iterator on CF object of type ContentElement.
4. We can iterate on the contentElement and get the property name in tagElement and it’s content in elementContent in above created variables.
The above code snippet provides details only related to the master of the content fragment. To get the details of the variations we can use the below API and the elements and other details:
Iterator<VariationDef> variationIterator = cf.listAllVariations();
while (variationIterator.hasNext()) {
String variationName = variationIterator.next().getName();
}
Reference: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/com/adobe/cq/dam/cfm/ContentFragment.html
No comments:
Post a Comment
If you have any doubts or questions, please let us know.