Expand my Community achievements bar.

SOLVED

Pre-define set of variations when creating a content fragment model

Avatar

Level 2

Hi all

 

I would like to pre-define a set of variations (to achieve a naming in accordance to our conventions) when creating a content fragment model. Is this somehow possible?

 

Another approach which comes to my mind is that the variations would be created automatically whenever a CF of that specific model gets created. Do you have any sample code for such a use-case?

 

Thank you and best regards

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @wolle89581941 

For second part of your question related to creating variations automatically, you can write Event Listeners to create variation as soon as content fragment is created.

Examples

https://medium.com/@toimrank/aem-handler-and-listener-12b6c8b5a3d3 

 



Arun Patidar

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hi @wolle89581941 

For second part of your question related to creating variations automatically, you can write Event Listeners to create variation as soon as content fragment is created.

Examples

https://medium.com/@toimrank/aem-handler-and-listener-12b6c8b5a3d3 

 



Arun Patidar

Avatar

Community Advisor

a Content Fragment (CF) of a specific model is created can be achieved through a custom workflow process step. Below is a simplified example using Java code within an AEM Workflow Process Step to automatically create variations:

package com.example.workflow;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.commons.jcr.JcrConstants;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

public class AutoCreateVariationsProcess implements WorkflowProcess {

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {

try {
// Get the workflow data
WorkflowData workflowData = workItem.getWorkflowData();

// Get the payload path (assuming it's a path to a Content Fragment)
String payloadPath = (String) workflowData.getPayload();

// Create variations based on the specific model
createVariationsForModel(workflowSession, payloadPath);

} catch (Exception e) {
throw new WorkflowException("Failed to execute AutoCreateVariationsProcess", e);
}
}

private void createVariationsForModel(WorkflowSession workflowSession, String contentFragmentPath)
throws RepositoryException {

// Assuming variations are based on a specific model
String modelPath = "/content/dam/cf-models/my-content-fragment-model";
String variationNamePrefix = "auto_variation_";

// Get a resource resolver
ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

// Get the Content Fragment model
Resource modelResource = resourceResolver.getResource(modelPath);

// Get the variations node under the model
Node variationsNode = modelResource.adaptTo(Node.class).getNode("jcr:content/renditions/variations");

// Get the Content Fragment node
Resource cfResource = resourceResolver.getResource(contentFragmentPath);
Node cfNode = cfResource.adaptTo(Node.class);

// Create variations based on the model
if (variationsNode != null && cfNode != null) {
Session session = resourceResolver.adaptTo(Session.class);
for (Node variationNode : ResourceUtil.adaptTo(variationsNode, Node.class).getNodes()) {
// Assuming variations are based on specific properties of the model
if (variationNode.hasProperty("someProperty")) {
String variationName = variationNamePrefix + variationNode.getProperty("someProperty").getString();

// Create a new variation node under the Content Fragment
Node newVariationNode = cfNode.addNode(JcrConstants.JCR_CONTENT + "/renditions/variations/" + variationName, "cq:Page");
session.save();
}
}
}
}
}


This example assumes that variations are based on a specific model and specific properties of that model.

https://experienceleague.adobe.com/docs/experience-manager-65/content/assets/content-fragments/conte... 



Avatar

Community Advisor

@wolle89581941 

You can start you thread the way you want either by initiating it via the Event Listeners as mentioned in the thread by @arunpatidar  or workflows as mentioned by @Raja_Reddy. And from event listeners you can create the service to create the sling job queue associated with the job topic if you are working specially on AEMaaCS.

 

For creating the variation you should leverage the APIs of the content fragment instead of using the Node. Details about the CF API is listed in the documentation https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/...

contentFragment.createVariation(name, title, description)

Hope this helps!

Thanks

Avatar

Administrator

@wolle89581941 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Level 2

Thanks all for your suggestions!

 

I guess there is no solution for the first part of the request yet (pre-defining variations during the creation of the model)? 

 

Best regards