Add a new content class as preassigned everywhere

In my projects I often use a blocks container at the end of all pages to offer the possiblity to enhance the page layout, in an optional way. Therefore I usually add a container element on each page content class called blocks.

After creating a new content class, which should be available in all these “extension points” I need to change the preassignments of all blocks elements in all page content classes.

Doing this via SmartTree is, depending on the number of content classes, a tedious and error-prone job.

Get rid of it and find below the jRQL code needed to achieve this easily.

String logonGuid="0904ABF0E43443D2881FE7481339650E";
String sessionKey="490EC675042F4C5A8A6DF1ED63ADD7A6";
String projectGuid="268F46EF5EB74A75824856D3DA1C6597";

CmsClient client = new CmsClient(logonGuid);
Project project = client.getProject(sessionKey, projectGuid);

Template newTemplate = project.getTemplateByName(“content_templates”, “text_block”);

for (TemplateFolder folder : project.getAllTemplateFolders()) {
for (Template template : folder.getTemplates()) {
// skip not page templates
if (!template.getName().endsWith(“_page”)) {
continue;
}
if (template.contains(“blocks”)) {
TemplateElement blocksElem = template.getTemplateElementByName(“blocks”);
if (blocksElem.isTemplatePreassigned(newTemplate)) {
blocksElem.addPreassignedTemplate(newTemplate);
System.out.println(folder.getName() +”;”+ template.getName() +”;”+ blocksElem.getName());
}
}
}
}

First I get the new content class I want to preassign on all blocks elements. The double for loops simply go through all content classes in your project.

Because in my project (see it as an example) I skip all content classes which might have a container element named blocks, but are not a physical page. (I name a physical page a page in RedDot CMS which determine the filename of the published file.)

To leave out content classes for physical pages which did not have a container element called blocks (preventing an error trying to access it) I check with contains() method (available on a page too).

The isTemplatePreassigned() if condition should show you how to check a preassignment. In this context it triggers an addPreassignedTemplate(newTemplate) only, if it’s not preassigned already.

In this context maybe the method hasPreassignedTemplates() is of interest.   It delivers true, if at least one content class is preassigned and false if no preassigments are used.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: