HIDE component in local and author | Community
Skip to main content
Level 2
March 6, 2023
Solved

HIDE component in local and author

  • March 6, 2023
  • 5 replies
  • 3017 views

As a supplement to the design document, please do
not display it in the Author environment (editor, view as published screens), but operate only in the Publish environment .

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 SivakumarKanoori

 

@ferosekhan 

Can you replace with below in your sightly .

<div data-sly-test="${runMode.hasRunMode }">

 

 

5 replies

SantoshSai
Community Advisor
Community Advisor
March 6, 2023

Hi @ferosekhan ,

 

You can setup run mode configurations for your component, and fetch that logic in HTL to check whether you would like to have it displayed or not.

eg. for run modes - https://www.techinnovia.com/run-modes/

 

Hope that helps you!

Regards,

Santosh

Santosh Sai
arunpatidar
Community Advisor
Community Advisor
March 6, 2023

You can do this by detecting AEM runmode

If publish show otherwise hide

https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem 

Arun Patidar
Level 2
March 6, 2023

any other example? a

little bit confusing!

Rohit_Utreja
Community Advisor
Community Advisor
March 6, 2023

@ferosekhan 

It can be done by fetching runmode from AEM instance. you can hide the component afterwards.

TO fetch the runmode, please refer below thread 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/detect-instance-runmode-programmatically-in-aem-as-cloud-service/m-p/418899

I hope it helps.

SivakumarKanoori
Community Advisor
Community Advisor
March 7, 2023

@ferosekhan :

 

You can add a new field in your dialog as runmode and you can input as publish. 

 

Then in your sightly you an call as ,

 

<sly data-sly-use.runMode= "com.sourcedcode.core.models.utils.RunModeHelper ;">

 

<div data-sly-test="${runMode.getHasRunMode}">

// you can write your component logic here.

</div>

 

 

package com.sourcedcode.core.models.utils;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
import org.apache.sling.settings.SlingSettingsService;

import javax.annotation.PostConstruct;

@Model(adaptables = SlingHttpServletRequest.class)
public class RunModeHelper {

@OSGiService
private SlingSettingsService slingSettingsService;

@RequestAttribute
@Default(values = "null")
private String runmode;

private boolean hasRunMode = false;

@PostConstruct
public void init() {
String runModes = slingSettingsService.getRunModes().toString();
hasRunMode = runModes.contains(runmode);
}

public boolean getHasRunMode() {
return hasRunMode;
}
}

 

source : https://sourcedcode.com/blog/aem/checking-run-mode-from-sightly-htl-in-aem

 

Thanks,

Siva

 

Thanks,Siva
Level 2
March 7, 2023

 code working good but publish environment also not showing!

 
SivakumarKanoori
Community Advisor
SivakumarKanooriCommunity AdvisorAccepted solution
Community Advisor
March 7, 2023

 

@ferosekhan 

Can you replace with below in your sightly .

<div data-sly-test="${runMode.hasRunMode }">

 

 

Thanks,Siva
Manu_Mathew_
Community Advisor
Community Advisor
March 7, 2023

@ferosekhan You just need to get the runmode and use data-sly-test to match it using a variable isRunModePublish.

You can find your run modes in: http://localhost:4502/system/console/status-slingsettings

 

Using Java:

slingSettingsService.getRunModes()

 

Using Slightly:

 

var SlingSettingsService = Packages.org.apache.sling.settings.SlingSettingsService; use(function () { // Get runmodes and transform them into an object that is easier to read for Sightly var runmodesObj = {}; var runmodesSet = sling.getService(SlingSettingsService).getRunModes(); var iterator = runmodesSet.iterator(); while (iterator.hasNext()) { runmodesObj[iterator.next()] = true; } return { runmodes: runmodesObj } });

 

 

and use HTL  

 

<div data-sly-use.logic="logic.js"> <p>Current runmodes: ${logic.runmodes}</p> <p data-sly-test="${logic.runmodes.publish}">publish runmode</p> </div>

 

 

Level 2
March 9, 2023

try in a different manner, but the expected result does not come.