Skip to main content

Identify CQ Mode

Identify CQ Mode

Figuring out what is the current authoring mode in components in Adobe CQ can sometimes be a tricky prospect.  Determining the current authoring mode is important for component development when you may want to display different HTML or controls to authors than will be displayed to the end users.  As a best practice, the same interface should display in Author and Design mode, as hiding or showing components may affect design.

The most obvious solution is to determining the authoring mode is to use the com.day.cq.wcm.api.WCMMode Java class, which does a server-side check to see what mode is currently being used.  

On new projects, I add a check for the WCMMode into the project's global.jsp and populate variables for testing if in design or author mode as such:

<%
boolean isEdit = WCMMode.fromRequest(request) == WCMMode.EDIT;
boolean isDesign = WCMMode.fromRequest(request) == WCMMode.DESIGN;
%><c:set var="isEdit" value="<%= isEdit %>" /><c:set var="isDesign" value="<%= isDesign %>" />

That way everyone can simply check isEdit or isDesign in all of my components and render the correct HTML instead of having it be checked ad-hoc on a number of different components.

But what about checking the mode JavaScript?  Well, you could certainly push a variable out with your HTML, but there are some downsides:

  • If your component is in an iParsys, it will register as being in WCM Disabled mode
  • There's already an API provided in CQ

The CQ.WCM object provides methods for checking the current mode, however first you need to see if the object exists.  CQ.WCM is not instantiated in publish mode, so if it does not exist, you automatically know you are in publish.

Once you have verified the CQ.WCM object exists, you can call one of the methods isEditMode, isDesignMode or isPreviewMode to check if the page is rendering in the appropriate state. 

// figure out the current mode
if(CQ.WCM) {
  if (CQ.WCM.isEditMode(true) || CQ.WCM.isDesignMode(true)){
    isPublish = false;
  }
}

There is a method getMode, which theoretically returns the current mode, however in CQ 5.5, this seems to only return null.

Using these two methods, you can accurately determine the current CQ Authoring mode in your components and react appropriately.

Comments

Popular posts from this blog

AEM 6.3 - How to extend Granite UI

Here in this article, I am going to explain how to extent Granite UI that is basically used to create AEM 6.x consoles. There can be various use cases where in you may need that. In our case, author wants to take all the page creation requests through AEM instead of email communication. Authors are basically looking for a form in AEM authoring console so that requestor can login and submit page creation/Asset upload request. Overall requirement is - Need a link in Navigation so that author can click on it and go to a Intake form. Author must be able to fill and save the intake requests. Author must be able to see the history of submitted intake requests Able to see the details of submitted intake requests. Solution - 1. Create a following structure - /apps/cq/core/content/nav/form and add the details given in below screenshot - You basically need to add - href, title and icon. Href is the path of page where user will land on clicking it.  Af...

CQ Page Properties from Javascript

To get CQ page properties inside javascript you can use core CQ JS API. It can be convenient if you need to get this information inside your custom JS widgets.              var pageData = CQ.HTTP.get(CQ.HTTP.externalize(CQ.utils.WCM.getPagePath() + "/jcr:content.json")); After that you can retrieve any property you need (assuming it's present in JCR):              var resourceType = pageData ? CQ.Util.formatData(CQ.HTTP.eval(pageData))['sling:resourceType'] : null; Please do not overuse it because it invokes additional ajax call to server. It's OK to use it in edit mode on author instance. Copied from -  http://adobecms.blogspot.com/2014/04/cq-page-properties-in-javascript.html

How Catalan Numbers Help in Solving DSA Problems?

Catalan numbers are a sequence of natural numbers that appear in various combinatorial problems, often related to recursively defined structures. These numbers are widely used in counting problems, particularly in dynamic programming and combinatorics. The sequence of Catalan numbers starts as follows: C 0 = 1 , C 1 = 1 , C 2 = 2 , C 3 = 5 , C 4 = 14 , C 5 = 42 , … C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, C_5 = 42, \dots Formula for Catalan Numbers The n-th Catalan number can be computed using the following formula: C n = ∑ i = 0 n − 1 C i ⋅ C n − i − 1 C_n = \sum_{i=0}^{n-1} C_i \cdot C_{n-i-1} Alternatively, it can be expressed using the binomial coefficient: Catalan numbers are a sequence of natural numbers that appear in various combinatorial problems, often related to recursively defined structures. These numbers are widely used in counting problems, particularly in dynamic programming and combinatorics. The sequence of Catalan numbers starts as follows: Formula for Catalan ...