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 - Check if page is published or not

If you want to know if the page is published or not you can use the below utility method to know if the page is published or not. Steps - Take Resource Object. Adapt it to Page Adapt page to ReplicationStatus, you will get the status Here is the code - public static Boolean isPublished(Resource resource) { Boolean activated; ReplicationStatus status = null; activated = false; if (resource != null) { try { Page page = resource.adaptTo( Page.class ); status = page.adaptTo( ReplicationStatus.class ); } catch (Exception e) { LOG.debug(e.getMessage (), e); } if (status != null) { activated = status.isActivated(); } } return activated; }

Search engine optimization in CQ

SEO Optimization Search engine optimization ( SEO ) is the process of affecting the visibility of a website or a web page in a search engine 's "natural" or un-paid (" organic ") search results . Source : Wiki Whenever you enter a query in a search engine and hit 'enter' you get a list of web results that contain that query term. Users normally tend to visit websites that are at the top of this list as they perceive those to be more relevant to the query. If you have ever wondered why some of these websites rank better than the others then you must know that it is because of a powerful web marketing technique called Search Engine Optimization (SEO) . SEO is a technique which helps search engines find and rank your site higher than the millions of other sites in response to a search query. SEO thus helps you get traffic from search engines.  HTML Tags and SEO Here is the list of important html tags which help search engine t

Create Your Own Private Ethereum Blockchain

Prerequisites You need to have Geth installed. The easiest way to do this is through homebrew. Open Terminal and  install homebrew ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2. Now  install geth brew tap ethereum/ethereum brew install ethereum Create Genesis File The Genesis  block  is the first block in the chain, the Genesis  file  is a JSON file that defines the characteristics of that initial block and subsequently the rest of the blockchain. Create a directory to hold your network files mkdir my-eth-chain cd my-eth-chain 2. Create your genesis file touch myGenesis.json 3. Open your genesis file and paste the following { "config": { "chainId": 1994, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0 }, "difficulty": "400", "gasLimit": "2000000", "alloc"