Script context / script from within included pages

Hi All, I’m Florian!

I’ve been learning to script with velocity and read / learned a lot in the docs and examples! (It’s great!)

One thing I haven’t been able to grasp is – I’m guessing – the context in which a script runs. I have three examples and as far as I can tell they all have the context in common (might be wrong of course!):

  1. I’d like to show a page footer (uiextension “org.xwiki.platform.template.contentFooter”) depending on the value a user has set in his user profile (via a custom class I’ve created). The executed content is:

    ## Get the user viewing the page
    #set ( $MyUser = $xcontext.user )
    #set ( $MySettingsClass = "Main.System.UiExtensions.AdditionalUserSettings" )
    ## Check if on the user profile an object of the class has the property set to "yes"
    #if ( $MyUser.getObjects($MySettingsClass)[0].PrevNextFooter == "Yes" )
    {{include reference="Main.System.UiExtensions.Snippets.PrevNext" 
    #end
    /}}
    

    This doesn’t work as $xcontext.user seems to always return the author of the page containing the object (and code) for the uiextension.

    => How can I get the user currently logged in in order the read the object of type “Main.System.UiExtensions.AdditionalUserSettings” on his user page?

Thank you very much!

Edit:

have been split out to separate post.

What you’ve done is correct, $xcontext.getUserReference() will give you the reference to the currently logged in user. Thus $xwiki.getDocument($xcontext.getUserReference()) will give you the document, on which you can get the xobjects.

Thank you for the reply.

I actually figured out last night what was missing for the solution to work:

In an uiextension you can (and in this case have to) set the relevant “context elements”. In the case of getting the actual user that’s viewing the page User has to be selected as well. Without this it falls back on the author of the page.

I’ll split this topic into additional questions, in order to mark the part about getting the user (example 1) as solved.