Check if the page is in editor or not | Community
Skip to main content
Level 3
August 21, 2023
Solved

Check if the page is in editor or not

  • August 21, 2023
  • 2 replies
  • 2306 views

Hello,

 

I have a page level component that I want to hide on its editor page, and show otherwise (preview, published, etc).

 

So it would hide in the editor page:

http://localhost:4502/editor.html/content/projectname/page1.wvcresponsive.html

 

and show in these pages:
http://localhost:4502/content/projectname/page1.wvcresponsive.html?wcmmode=disabled

http://localhost:4502/content/projectname/page1.html

 


I know that AEM sets a cookie "wcmmode" that can changes value depending on the mode, but for all the pages above, they have the same value of "edit" so it would be useless for the purpose of distinguishing them.

 

Is there a reliable AEM way to determine whether it is an editor page or not?

 

Preferably a frontend way (Cookie, browser object, etc) that can be implemented using Javascript.

 

Thank you,

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mahedi_Sabuj

Hi @sean12341

You can determine whether you're currently in editor mode from both the backend and the frontend. Feel free to choose either approach based on your needs. 

<!-- Sightly --> <sly data-sly-test.editor=${wccmmode.edit}>.....</sly> <!-- JavaScript --> var isEditor = window.Granite && window.Granite.author ? true : false; <!-- Java --> WCMMode mode = WCMMode.fromRequest(request);

You can check available wcmmode options from here: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/WCMMode.html 

 

 

2 replies

Fanindra_Surat
Community Advisor
Community Advisor
August 21, 2023

Hi @sean12341 -
In Sightly -  This can be achieved using the wcmmode context object with values like edit, preview and publish. You can hide/show the contents of component using a conditional check like below:

<sly data-sly-test.show="${wcmmode.preview || wcmmode.publish}"> 

 

In Javascript - You may try assigning the wcmmode values to data attributes and read them in JS.

Mahedi_Sabuj
Community Advisor
Mahedi_SabujCommunity AdvisorAccepted solution
Community Advisor
August 21, 2023

Hi @sean12341

You can determine whether you're currently in editor mode from both the backend and the frontend. Feel free to choose either approach based on your needs. 

<!-- Sightly --> <sly data-sly-test.editor=${wccmmode.edit}>.....</sly> <!-- JavaScript --> var isEditor = window.Granite && window.Granite.author ? true : false; <!-- Java --> WCMMode mode = WCMMode.fromRequest(request);

You can check available wcmmode options from here: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/WCMMode.html 

 

 

Mahedi Sabuj
Level 2
November 21, 2024

HI,

I ve tried the javascript approach.

1. If I open edit mode it is working fine

2. If I click "View as Published" it is working fine.

3. If I reload the page in Step 2 it is failing, not sure why, but window.Granite.author is set in that case (although I am not in edit mode)