Official Documentation Available
This topic is now covered in Technical Guide - Magnolia API.
How to access repository content through the Magnolia API
This is just to get some of the fundamentals established. The Magnolia API has been evolving very quickly so describing it is like trying to hit a moving target. I have tried to avoid the deprecated parts of the API and focussed on the parts that appear to be here for good.
The objective of this documentation is to give you a basic command of how you can fundamentally access information in the repository.
Associating a request with content in the repository
When a request is received Magnolia has a filter class, info.magnolia.cms.filters.MgnlCmsFilter, that looks at the URI and determines what content it relates to. The content is then assembled and placed in the request scope where the template used to render it can access it. Practically the content is available as a info.magnolia.cms.core.Content object stored in the request scope under the key Aggregator.ACTPAGE.
The following code would get a reference to the Content object:
Rather than get the Content object directly from the request scope the class info.magnolia.context.MgnlContext is provided. Using the class is advised because implementation details might change with other classes and there are other convenience services provided by this class. MgnlContext is a Singleton,'(although the constructor is public)', and all the methods are static. Hence, you use the class directly. There are a number of methods provided by this class and you should familiarize yourself with them. To get the Content object you would use the following code:
Once you have a Content object there are a number of methods available, for example:
Content getContent(String name)
this returns a Content object representing a child node with the supplied name
returns the name of the template configuration for the page
returns the name of the content node
gets the node data with the supplied name
convenience method to return the value of the Title property. Actually uses the preceding method
various getChildren methods exist but the default, no arg, method returns child nodes of the same type as the parent
get a handle representing a path relative to the content repository
gets the level the content node appears relative to the ROOT
gets the UUID of the node referenced by this object
Working with the current Content node
The current content node refers to the node associated with URI of the current request. The template being used for that URI will be able to get a reference to this content node using:
A Content object is going to have two fundamental components; data nodes and child content nodes. To find out if a Content object has child nodes use the boolean method as follows:
A JSP snippet that uses some of the Content types basic methods would look like:
If you wanted to process this Content object children you would get a collection of Content objects representing each of the Content objects child content nodes. For example:
Working with Content in the Repository elsewhere
The MgnlContext class provides services for the Content object for the current request. To have more flexibility over what you access in the content repository you can use the info.magnolia.cms.core.HierarchyManager. The MgnlContext provides a method, 'getHierachyManager(ContentRepository.WEBSITE)', to get an instance of a HierarchyManager. You can obtain a Content object for a specified URI using the 'getContent(String uri)' method on the HierarchyManager so obtained. For example:
This would provide a Content object representing /en/about-magnolia. You could iterate over this Content objects child nodes in exactly the same way as above. I won't teach you to suck eggs but obviously this basic functionality enables you to navigate down the whole content tree in the same way.
Content Node Data
Each Content object has child node and data nodes. To get a Content objects data node you use the Content types 'getNodeDataCollection()' method. Each element of the collection will be a NodeData type object. NodeData provides various methods for working with a Content nodes data. For example:
A page template, at runtime, is provided with a reference to a Content object that represents the repository content for the URI provided in the http request. The MgnlContext class and its static .getActivePage() method will return that Content object. The Content object has two components: child nodes and data nodes. Methods are provided for navigating both types of related node content. If you want to access other parts of the repository explicitly you can get a Content node for a given URI from a HierarchyManager. The MgnlContext object also provides a method to get an instance of a HierarchyManager.
In its previous incarnation on JspWiki, this page was last edited on Sep 19, 2007 6:42:26 PM by Interition