Forum Discussion

Kev_B's avatar
Kev_B
Advisor
8 years ago

Building a component to react to Custom Content

Hello all,

 

Been a while since I've asked an odd question - I'm looking at building a component to access a specific Custom Content, and then decide whether to show the component and how it should be displayed. This is decided based on the FreeMarker placed in the Custom Content box (to allow changes without having to re-write the component itself).

 

I know how to pull the Custom Content box from Admin and display it, I'm just curious if there's a way to make the component check the code within the Custom Content box, and then decide whether or not to display it?

 

Here's more of a breakdown of my train of thought:

 

Component - checks if users.role matches to anything with the Custom Content field. If the user's role isn't listed in the Custom Content text then the component will not display.

Custom Content - dictates the HTML to be displayed based on the users.role, which is checked within the Custom Content text.

 

I hope this makes sense - any suggestions gratefully appreciated.

8 Replies

  • You can use the below code to get custom content data in the component


    <#assign nodeType = coreNode.nodeType />
    <#assign nodeID = coreNode.id />

    <#if nodeType=='board'>
    <#assign content =
    restadmin("/boards/id/"+nodeID+"/settings/name/customcontent.10_text").value
    />
    <#else>
    <#assign content =
    restadmin("/categories/id/"+nodeID+"/settings/name/customcontent.10_text").value
    />

  • Kev_B's avatar
    Kev_B
    Advisor
    8 years ago

    Thanks VikasB

     

    I'd seen that article when figuring out how to access the custom content.

     

    Which method would you recommend for checking if there's a variable within that, say checking if customcontent.#_text contains users.role, for example?

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Kev_B

    If there is a different role for different node, then you need to use below code using corNode id

    <#assign content =
    restadmin("/boards/id/${coreNode.id}/settings/name/customcontent.#_text").value
    />

    If checking for just same role on all the nodes then you can just use this one

    <#assign content =
    restadmin("/settings/name/customcontent.#_text").value
    />

  • Kev_B's avatar
    Kev_B
    Advisor
    8 years ago

    Thanks VikasB,

     

    Although maybe we're not on the same page? The component will be testing if there's a role declared within customcomponent.#_text, similar to....

     

    if customcomponent.#_text contains a users.role and it matches the current users.role
       display customcomponent.#_title and text with appropriate HTML
    else
       do not display anything

    The custom component text field would then be declaring different roles and a unique message

    if users.role = "beginner"
       display "Welcome to the Community"
    elseif users.role = "expert"
       display "Welcome back"

    That's not specific to what I'm trying to do, but the best way I can think to elaborate on what I'm trying to achieve.

     

    Thanks again for all your help.

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Kev_B

    Get value from Custom Content #

    <#assign role =restadmin("/settings/name/customcontent.#_text").value />

    Compare it with user role

    <#if role ==  'Beginner' >
        Welcome
    <#elseif role=='Expert'>
        Welcome Back
    <#else>
        <#--Nothing to display ->
    </#if>

    Hope, you are trying to do something similar. LMK if you want something else.

     

  • Kev_B's avatar
    Kev_B
    Advisor
    8 years ago

    VikasB

     

    Thank you, I think it's definitely similar. I'll have to experiment and see how it turns out.

     

    Appreciate your tips and advice.

  • Kev_B's avatar
    Kev_B
    Advisor
    8 years ago

    Just wanted to add in the solution I found for this, in case anybody else is doing similar.

     

    I pulled the text field of our Custom Content, then ran a LiQL query to pull a users' roles based on their user ID.

     

    I then ran through listing the returned role values, comparing them with the contents of the Custom Content field, using the ?contains built-in in FreeMarker.

     

    Then ran a conditional to display the Custom Content component if there was a match, otherwise to hide the component.

     

    If anybody has any suggestions for a simpler solution, I'd love to hear it. Obviously would've been a bit easier if there were a Context Object to pull the user's roles.

     

    My next stage is to then add conditionals into Custom Content to decide which message to display to which rank, but that should be straightforward having built the component to show or hide the CC component based on the roles matching those of the user.