Redirect Page Template?

Hello people,

I’m looking for a way to enable our users to easily add Redirect-Pages.

i.e. click on “+ Create” button, choose a name, place and the “redirect” page template, click on “Create” and then

  • either have a GUI picker to choose a redirect target
  • or directly get to page object editor with a RedirectClass-Object already added.

Adding a redirect oject to a new SimplePage template didn’t work so far. Any ideas?

Regards

This should work (provided the new page is edited after creation to set the target for the redirection).

What doesn’t work?

1 Like

Hmm, it jumped to the inline editor directly, but I’ll try out a thing or two more :slight_smile:

Yes there’s no action to go directly in xobject edit mode after creating of the page (see See https://extensions.xwiki.org/xwiki/bin/view/Extension/Administration%20Application#HActiononCreate ). The user will need to do that. Now you could script that using velocity in the template if you wanted (so that for example if the xproperty is empty, redirect to the xobject edit mode automatically, using https://snippets.xwiki.org/xwiki/bin/view/Extension/Redirect#HAdvancedRedirects).

1 Like

ahh yes, that’s smart :slight_smile: I’ll try it out!

Alright, here’s what I ended up with:

Infos: XWiki FAQ, XWiki Extension Redirect, @vmassol’s hint above

  • Create a new page template (under Templates).
  • Edit Wiki source of page template, add following velocity code:
{{velocity}}
#if ($doc.getObjectNumbers('XWiki.RedirectClass') > 0)
#if ($redir.location != "")
## There is an object of XWiki.RedirectClass for this page already, but it is still empty (e.g. the article is new!)
Weiterleitung noch nicht gesetzt!
$response.sendRedirect($doc.getEditURL("edit", "") + "&editor=object")
#else
Weiterleitung aktiv nach: $redir.location
#end
#else
## There's no object of XWiki.RedirectClass for this page yet!
$doc.createNewObject('XWiki.RedirectClass')
$doc.save("Weiterleitungs-Objekt hinzugefügt", true)
$response.sendRedirect($doc.getEditURL("edit", "") + "&editor=object")
#end
{{/velocity}}
  • I created the page as “hidden”, so you can find it in the usual pickers only after show enabled “show hidden pages” via x x x h keyboard shortcut.
  • Add an empty XWiki.RedirectClass object via object editor to the page.
  • In Administration, add another Template Provider to be able to set (Icon, Name, etc) and have it available in your “create page” wizard. Choose the template page you just created. As Action choose “Save and View”

Now, when you create a new page, choose the Redirection Template (Provider) you just created from the list and save. XWiki will open the page, realize there’s no redirection goal and instead redirect you to the page’s object editor. There you open the XWiki.RedirectClass object and pick your redirection target. Save the page.

When you open the page, it will redirect you to the target.

Caveats / limitations:

  • Might be a bit flaky when using multi-language Wikis, but it works.
  • When you want to delete the page or edit the redirection, you will have to add ?editor=object to the page URL manually.
  • The redirection target page does not tell you that you have just been redirected there from another page yet (like you know it from e.g. Wikipedia), so use with care. This might be possible with some more hacks though, but I’, fine with this behaviour atm.

Thanks a lot!

Cool, note that you could improve it visually by going in inline edit mode instead of using the object editor.

Here’s how to do that:

  • Create a sheet page to display the redirect nicely. For example:
    {{velocity}}
    #if ($xcontext.action == 'edit')
      Page to redirect to:
      $doc.display("location")
    #end
    {{velocity}}
    
  • Add an xobject of type XWiki.DocumentSheetBinding to your template page and point it to the sheet page just created
  • When you redirect to the created page in edit mode, you’ll see the content of your sheet displayed. A bit nicer for users than seeing the object editor IMO.

PS: See https://extensions.xwiki.org/xwiki/bin/view/Extension/Sheet%20Module to understand more

1 Like

great! just what I was looking for, this is of course much nicer. Thanks a lot & have a nice day!