Jen
Jen

Reputation: 2004

Kentico 10 Controlling visibility of webpart based on another webpart

I have 2 editable text web parts on a page. One web part has default text set on it as the majority of the time it will be the same text.

I want to set visibility of this web part based on the text of another web part for the live site.

I tried this:

 {#!string.IsNullOrEmpty(WebPart.GetValue("AreaDescription", "Content")) && 
CMS.PortalEngine.ViewModeEnum.LiveSite == CMS.PortalEngine.PortalContext.ViewMode  #}

But then it's never visible so it's not picking up the text inside the other webpart successfully. So I'm thinking maybe I need to call GetContent() to get the user provided text of the editable region. I tried writing a custom transformation method but using the below - PagePlaceholder is unknown and I'm not sure how to get a reference to it.

CMSAbstractWebPart webpart = PagePlaceholder.FindWebPart("webPartId");

Upvotes: 0

Views: 456

Answers (3)

Jen
Jen

Reputation: 2004

From Kentico support - macros will not allow me to do what I want to achieve because content of other web parts is not available in CMSContext or in DocumentContent.

As I was trying to avoid custom web parts they suggested I could create an alternative layout for my web part that would be the same as default plus add:

your handler for onLoad, or any better event

If it was a custom web part I could add:

///find another web part - in this case WebPart1
CMSAbstractWebPart webpart1 = PagePlaceholder.FindWebPart("WebPart1");

///store value/content of chosen property
string wp1DefaultText = ValidationHelper.GetString(webpart1.GetValue("DefaultText"), "");

Potentially on Prerender to ensure it wouldn't be overridden.

At this stage I'm just going to remove the default text as it feels like I'm fighting the system and the value gained probably isn't worth the effort/customisation.

Upvotes: 0

Peter Mogilnitski
Peter Mogilnitski

Reputation: 998

in a macro to get the content of editable text:

{%CurrentDocument.DocumentContent["webPartId"]#%}

so what you are looking for is something like this:

{%!string.IsNullOrEmpty(CurrentDocument.DocumentContent["AreaDescription"]) && (PortalContext.ViewMode == "LiveSite")#%}

Upvotes: 1

Mariia Zhokh
Mariia Zhokh

Reputation: 220

Try {% (ViewMode == "LiveSite") && (CMSContext.CurrentDocument.WebPartID != "") #%}

Upvotes: 1

Related Questions