Getting page reference from full url or picker in velocity

Hi,

I’m trying to get a reference to a page in velocity.
The preferred way would be to have document picker, i.e. pick the document from a control and get passed a reference into a velocity variable.
But I’d also fine if I could paste the full URL of an xwiki page into my form and get the reference from this.

I searched throw through the docs and found Document Picker Macro and references to a StandardURLResourceReferenceResolver.
But I can’t get both to work.

Any help would be appreciated.

Best,

rbr

What exactly have you tried?

Hi @mflorea,

to be honest I’m feeling kind of stupid now. I was so fixated on information getting POSTed in the Document Picker macro and was desperately searching for this $thisIsWhatYouWant=$someLocationPickerMagic.doMagic() line in velocity that I completely missed the GET- parameters.

So I can get what I want for now, although I may reach out to you for help on customizing the picker.

Thanks for your help.

Best regards

rbr

Hi @mflorea,

I now have a working example of what I want to achieve (see below). Yet I wonder if and how I should differentiate between nested pages and terminal pages. Currently I always append “.WebHome” to the reference to get the page. Do I need to treat terminal pages “special”?

Best regards,

rbr

 {{template name="locationPicker_macros.vm" /}}

{{velocity}}
	#set ($myspaceReference=$request.spaceReference)
	$myspaceReference
	#if(!$myspaceReference)

    {{html}}
			#set ($documentReference = $services.model.resolveDocument('Home'))
			#set ($selectedDocument = $xwiki.getDocument($documentReference))
			<form class="xform" action="">
				#locationPicker({
				'id': 'target',
				'preview': {
				  'label': 'my.preview.label',
				  'hint': 'my.preview.hint'
				},
				'parent': {
				  'label': 'core.create.spaceReference.label',
				  'hint': 'core.create.spaceReference.hint',
				  'name': 'spaceReference',
				  'reference': $documentReference.parent,
				  'placeholder': 'core.create.spaceReference.placeholder'
				}
			  })
			   <span class="buttonwrapper"><input type="submit" value="Update pictures on page" class="button"/></span>
			</form>
		{{/html}}

	#else
		fullUrl: $myspaceReference
    set($myspaceReference=$myspaceReference + ".WebHome")
    #set ($myRef=$services.model.resolveDocument($myspaceReference))
    myRef: $myRef
		#set ($mydoc = $xwiki.getDocument($myRef))
    mydoc: $mydoc
		#set ($xdom=$mydoc.getXDOM())
    xdom: $xdom
    #set ($blocks=$xdom.getBlocks('class:ImageBlock', 'DESCENDANT'))
    blocks: $blocks
	#end
{{/velocity}}

The way you configured the location picker it can select only nested pages. $myspaceReference is a space reference. The clean way to get the reference to the corresponding nested page is:

#set ($myspaceReference = $services.model.resolveSpace($myspaceReference))
#set ($myPageReference = $services.model.resolveDocument('', 'default', $myspaceReference))

Appending ‘.WebHome’ works but it’s better to hide this implementation detail.

1 Like

Hi @mflorea,

thank you very much. This works great.

I totally agree.
Aside from the picker will the “default” keyword resolve nested and terminal pages correctly?

Best regards,

rbr

It will resolve nested pages only, because the picker only allows you to select nested pages (terminal pages are not shown in the tree with your current configuration). The picker can be configured to show terminal pages also, but getting the reference of the selected terminal page is not that straightforward. Do you need to work with terminal pages?