cogChange the title of a page upon saving it based on the value of an object's property
Developed by

Marta Girdea, Guillaume Lerouge

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

The aim of this code snippet is to update the value of the title field of a XWiki document based on the value of an object's property.

This is especially useful when you're using a template to create a new document. In that case, the title field of the page is not displayed. However, you might still want to be able to set the title of the document you've created based on the value of a property you've filled in the template.

The script uses the observation manager to listen to a document being saved. It then checks whether the document that was saved has an object of the specified class. If it does, it checks for the value of a specific property within the document and updates the title of the document using it.

Installation Instructions

Simply copy-paste the code below in a page of your wiki. Suggestion: create a "Code" space and put that page in it.

Since it contains groovy code (with access to restricted API) this page needs to be saved by a user that has programming rights.

Code

If you want to use this code in a page that is in syntax 1.0, remove the groovy macro at the beginning and end of the script.

{{groovy}}
import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.bridge.event.*
import org.xwiki.context.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*
import org.apache.velocity.VelocityContext;

class TitleUpdater implements EventListener
{
    def xwiki
    def context
 
   TitleUpdater(xwiki, context)
   {
       this.xwiki = xwiki
       this.context = context
   }

    String getName()
   {
       // The unique name of this event listener
       return "TitleUpdater"
   }

    List<Event> getEvents()
   {
       // The list of events this listener listens to
       return Arrays.asList(new DocumentCreatedEvent(), new DocumentUpdatedEvent())
   }

   // Called by the Observation Manager when an event matches the list of events returned
   // by getEvents()
   void onEvent(Event event, Object source, Object data)
   {
       // Current context
       def crtContext = Utils.getComponent(Execution.class).getContext().getProperty('xwikicontext')

       // Defines the type of object that should be looked for
       def myObject = source.getObject("Code.MyClass");

       // If the page has an object of the specified class, we get the value of the relevant property and assign it to our title variable
       if (myObject != null) {
         // Defines the variable that we will later use to update the title of the document
         def title = myObject.get("myProperty").value
         // If the name has changed, update the document title for pretty display
         if (source.getTitle() != title) {
            source.setTitle(title)
           // Force the storage to keep the same version number, so that this looks like a single save event
           source.setMetaDataDirty(false);
            source.setContentDirty(false);
            crtContext.getWiki().saveDocument(source, source.getComment(), source.isMinorEdit(), crtContext);
         }
       }        
   }
}

// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("TitleUpdater")
def listener = new TitleUpdater(xwiki, xcontext)
observation.addListener(listener)
{{/groovy}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags:
Created by Guillaume Lerouge on 2011/05/17 20:05

Get Connected