Forum Discussion

dkytasaari's avatar
8 years ago
Solved

Need custom component to display user's location in Profile Hero

I want to display a user's location in the Profile Hero.  I don't see where there is a stock component to display this information, which means I need to create a custom component.  I cannot find an example of anything here, or in the KB that would be a good example to work from.  I suspect I need something like this:

 

<#assign userLOC=rest( "2.0", "/search?q=" + "SELECT location from users where id = (ID NUM of PROFILE I AM VIEWING)  />
<div>
$userLOC
</div>

What do I need to finish that WHERE clause?  Can anyone point me to a repository of examples?

 

Regards,

Dennis

  • VikasB's avatar
    VikasB
    8 years ago

    Hi dkytasaari  You are missing ending quote(") in your query.

    Here is updated one

    <#assign userLOC= rest("2.0","/search?q=" + "SELECT login from users where id='${page.context.user.id}'"?url).data.items[0] />
    <div>
        ${userLOC.login}
    </div>

    Also, I do not think so there is any location field in API.  

     

8 Replies

  • dkytasaari-

     

     ${page.context.user.id} is a context object which will return you the id of user profile you will be viewing. 

     

    <#assign userLOC=rest( "2.0", "/search?q=" + "SELECT location from users where id = '${page.context.user.id}').data.items[0]  />

     

    <div>
    ${userLOC.location}
    </div>

     Also, you can check if ${userLOC} is blank or not by adding if else condition.

  • dkytasaari's avatar
    dkytasaari
    Ace
    8 years ago

    TariqHussain

     

    i've tried this:

     

    <#assign userLOC=rest( "2.0", "/search?q=" + "SELECT location from users where id = '${page.context.user.id}').data.items[0] />
    
    <div>
        ${userLOC.location}
    </div>

    The Preview parser throws an error, I cannot resolve:

    Freemarker template 'preview' parsing failed:
    ParseException:Syntax error in template "preview" in line 6, column 0:
    Lexical error: encountered <EOF> after "\"SELECT location from users where id = \'${page.context.user.id}\').data.items[0] />\n\n<div>\n    ${userLOC.location}\n</div>\n".

    Line 6 is the blank line at the bottom.  If I remove line 6, then the error moves to end of line 5.  Something is wrong, but I don't have enough experience yet to know the ins and outs of Freemarker syntax.  Is there something obvious here I am overlooking?

     

    It also seems odd that there is a quote before the SELECT, but there isn't a closing quote for the statement.  I tried inserting one in a couple of different places, but that just gave different types of error message, none of which were any clearer.

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Hi dkytasaari  You are missing ending quote(") in your query.

    Here is updated one

    <#assign userLOC= rest("2.0","/search?q=" + "SELECT login from users where id='${page.context.user.id}'"?url).data.items[0] />
    <div>
        ${userLOC.login}
    </div>

    Also, I do not think so there is any location field in API.  

     

  • VikasB

     

    I suspected there was a missing ", but I wasn't sure if ?url that you had shown me in answer to another question I had was appropriate.

     

    This seems more promising, but the Preview throws up a different error.

     

    FreeMarker template error (HTML_DEBUG mode; use RETHROW in production!)
    The following has evaluated to null or missing:
    ==> page.context.user  [in template "preview" at line 1, column 82]
    
    ----
    Tip: It's the step after the last dot that caused this error, not those before it.
    ----
    Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
    ----
    
    ----
    FTL stack trace ("~" means nesting-related):
    	- Failed at: #assign userLOC = rest("2.0", "/searc...  [in template "preview" at line 1, column 1]
    ----
    
    
    

    I do have a value for location in my profile, so there should be something to display.  I'm not sure what's going on here.

     

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Hi dkytasaari  It will work only on the profile page on all other pages it would display an error because of ${page.context.user.id} is the context object which works on the profile page only. 

    To get the location you can use below query(You need to change the user id)

    http://community.lithium.com/restapi/vc/users/id/97/profiles/name/location

    For self

    http://community.lithium.com/restapi/vc/users/self/profiles/name/location
  • This one did the trick.  Note, there is a location field available in the collection for users.  Thanks VikasB and TariqHussain for your responses.

     

    This is what I have working.

     

    <#attempt>
    	<#assign userINFO= rest("2.0","/search?q=" + "SELECT location from users where id='${page.context.user.id}'"?url).data.items[0] />
    
    <div class="lia-profile-about-me lia-component-user-about-me">
    	<div class="StringDisplayTaplet">
    		<p><b>Location:</b> ${userINFO.location}</p>
    	</div></div>
    <#recover>
    <!-- display nothing -->
    </#attempt>
    
    
  • Proha's avatar
    Proha
    Mentor
    7 years ago

    I wrote a similar (Page->Forum Message):

    <#if (env.context.message.author.id)??>
    <#assign user = rest("2.0","/search?q=" + "SELECT location FROM users WHERE id = '${env.context.message.author.id}'"?url).data.items[0] />
    <#if user.location??>
    <div class="lia-panel lia-panel-standard">
    <div class="lia-panel-heading-bar-wrapper"><div class="lia-panel-heading-bar"><span aria-level="4" role="heading" class="lia-panel-heading-bar-title">Місто</span></div></div>
    
    <p>${user.location}</p>
                            
    </div>
    </#if>
    </#if>

    But only admins and moderators can see this component.

    How can I extend view permissions for all users?

    Solved by: https://lithosphere.lithium.com/t5/Developer-Discussion/API-call-to-roles-collection-returning-empty-for-non-moderators/m-p/462829#M14239

  • Payal's avatar
    Payal
    Director
    7 years ago

    Proha - You can use rest admin instead of rest in LIQL query. See below:

    <#if (env.context.message.author.id)??>
    <#assign user = restadmin("2.0","/search?q=" + "SELECT location FROM users WHERE id = '${env.context.message.author.id}'"?url).data.items[0] />
    <#if user.location??>
    <div class="lia-panel lia-panel-standard">
    <div class="lia-panel-heading-bar-wrapper"><div class="lia-panel-heading-bar"><span aria-level="4" role="heading" class="lia-panel-heading-bar-title">Місто</span></div></div>
    
    <p>${user.location}</p>
                            
    </div>
    </#if>
    </#if>

     

    Thanks