Forum Discussion

Claudius's avatar
13 years ago
Solved

Accessing Author information for custom component

I would like to add a custom component below the "Author" stock component that is shown to the left of each message. I've already found the page type to add my custom component (ForumMessage), but now I'm lacking a context object that grants me access to the author details (ID, SSO ID etc.).  Using ${page.context.message.author.id} I can only get the topic starter ID. I tried ${message.context}, but that does not exist - just in line with the context object documentation.

Anyone knows how to access a message author's information?

5 Replies

  • Perfect, thanks. Sometimes it's that easy that you can't find it.
  • iftomkins's avatar
    iftomkins
    Maven
    11 years ago

    Thanks for the inspiration to change the author's component, Claudius. A big improvement over the default.

     

    I rebuilt our author component to use on the forum message page, and I wanted to share the code here. I think it uses a rest call instead of the env context object, but works all the same. It reorders it from the default, and adds kudos count. The <i> tags--such as <i class="fa fa-comment"></i>--are pulling icons from the Font Awesome icon set. The design is not final yet, but it looks something like this.

     

     

    <#--Custom author component with new order: Avatar, rank, posts, kudos. Original author component XML is     <component id="author"/> -->

    <#assign messageAuthorId = rest("/messages/id/${page.context.message.uniqueId}/author").user.id />
    <#assign messageAuthorLogin = rest("/messages/id/${page.context.message.uniqueId}/author").user.login />
    <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
    <#assign kudosCount = rest("/users/id/${messageAuthorId}/kudos/received/count").value />
    <#assign postCount = rest("/users/id/${messageAuthorId}/posts/count").value />
    <#assign rankingName = rest("users/id/${messageAuthorId}/ranking/name").value />
    <#assign profileUrl = "/t5/user/viewprofilepage/user-id/${messageAuthorId}" />

    <div class="lia-message-author-avatar">    
        <div class="custom-avatar-wrapper">
            <div class="UserAvatar lia-user-avatar lia-component-common-widget-user-avatar">
                <a class="UserAvatar lia-link-navigation" tabindex="-1" target="_self" id="link_39" href="${profileUrl}"><img class="lia-user-avatar-message" id="display" src="${avatarUrl}"></a>
            </div>
        </div>
    </div>
    <div class="lia-message-author-username">
        <span class="UserName lia-user-name">
            <a class="lia-link-navigation lia-page-link lia-user-name-link" href="${profileUrl}">
                <span class="">${messageAuthorLogin}</span>
            </a>
        </span>    
    </div>
    <div class="lia-message-author-rank">${rankingName}</div>
    <div class="lia-message-author-post-count" title="Number of posts by this author"><i class="fa fa-comment"></i> ${postCount}</div>
    <div class="lia-message-author-kudos-count" title="Number of votes received by this author"><i class="fa fa-thumbs-up"></i> ${kudosCount}</div>

  • iftomkins's avatar
    iftomkins
    Maven
    11 years ago

    UPDATE: Here is the same code, updated to avoid an error on the ReplyPage by defaulting to the original author component:

     

    <#--Custom author component with new order: Avatar, rank, posts, kudos. Original author component XML is     <component id="author"/>. On Reply page, defaults to Out-of-the-box author component. -->

    <#if page.name?lower_case == "replypage" >
        <@component id="author"/>
    <#else>

    <#assign messageAuthorId = rest("/messages/id/${page.context.message.uniqueId}/author").user.id />
    <#assign messageAuthorLogin = rest("/messages/id/${page.context.message.uniqueId}/author").user.login />
    <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
    <#assign kudosCount = rest("/users/id/${messageAuthorId}/kudos/received/count").value />
    <#assign postCount = rest("/users/id/${messageAuthorId}/posts/count").value />
    <#assign rankingName = rest("users/id/${messageAuthorId}/ranking/name").value />
    <#assign profileUrl = "/t5/user/viewprofilepage/user-id/${messageAuthorId}" />

    <div class="lia-message-author-avatar">    
        <div class="custom-avatar-wrapper">
            <div class="UserAvatar lia-user-avatar lia-component-common-widget-user-avatar">
                <a class="UserAvatar lia-link-navigation" tabindex="-1" target="_self" id="link_39" href="${profileUrl}"><img class="lia-user-avatar-message" id="display" src="${avatarUrl}"></a>
            </div>
        </div>
    </div>
    <div class="lia-message-author-username">
        <span class="UserName lia-user-name">
            <a class="lia-link-navigation lia-page-link lia-user-name-link" href="${profileUrl}">
                <span class="">${messageAuthorLogin}</span>
            </a>
        </span>    
    </div>
    <div class="lia-message-author-rank">${rankingName}</div>
        <div class="lia-message-author-post-count" title="Number of posts by this author">
            <table>
                <tr><td class="fonticon"><i class="fa fa-comment"></i></td><td class="count">${postCount}</td></tr>
            </table>
        </div>
    </tr>
    <div class="lia-message-author-kudos-count" title="Number of votes received by this author">
            <table>
                <tr><td class="fonticon"><i class="fa fa-thumbs-up"></i></td><td class="count">${kudosCount}</td></tr>
            </table>
    </div>
    </#if>

  • iftomkins's avatar
    iftomkins
    Maven
    11 years ago

    Another update. Forgot to use the "env" object instead of "page" object, as described in this post. This produced the humorous result of every post being authored by the first messageauthor on any given page. Here it is:

     

     

    <#--Custom author component with new order: Avatar, rank, posts, kudos. Original author component XML is     <component id="author"/>. On Reply page, defaults to Out-of-the-box author component. -->

    <#if page.name?lower_case == "replypage" >
        <@component id="author"/>
    <#else>

    <#assign messageAuthorId = env.context.message.author.id />
    <#assign messageAuthorLogin = env.context.message.author.login />
    <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
    <#assign kudosCount = rest("/users/id/${messageAuthorId}/kudos/received/count").value />
    <#assign postCount = rest("/users/id/${messageAuthorId}/posts/count").value />
    <#assign rankingName = rest("users/id/${messageAuthorId}/ranking/name").value />
    <#assign profileUrl = "/t5/user/viewprofilepage/user-id/${messageAuthorId}" />

    <div class="lia-message-author-avatar">    
        <div class="custom-avatar-wrapper">
            <div class="UserAvatar lia-user-avatar lia-component-common-widget-user-avatar">
                <a class="UserAvatar lia-link-navigation" tabindex="-1" target="_self" id="link_39" href="${profileUrl}"><img class="lia-user-avatar-message" id="display" src="${avatarUrl}"></a>
            </div>
        </div>
    </div>
    <div class="lia-message-author-username">
        <span class="UserName lia-user-name">
            <a class="lia-link-navigation lia-page-link lia-user-name-link" href="${profileUrl}">
                <span class="">${messageAuthorLogin}</span>
            </a>
        </span>    
    </div>
    <div class="lia-message-author-rank">${rankingName}</div>
        <div class="lia-message-author-post-count" title="Number of posts by this author">
            <table>
                <tr><td class="fonticon"><i class="fa fa-comment"></i></td><td class="count">${postCount}</td></tr>
            </table>
        </div>
    </tr>
    <div class="lia-message-author-kudos-count" title="Number of votes received by this author">
            <table>
                <tr><td class="fonticon"><i class="fa fa-thumbs-up"></i></td><td class="count">${kudosCount}</td></tr>
            </table>
    </div>
    </#if>