Magnolia 6.1 reached end of life on March 31, 2021. This branch is no longer supported, see End-of-life policy.
This page provides an overview of all ways to define custom JCR node types and create new workspaces with Magnolia.
Even though it has always been possible to define node types and create workspaces within Magnolia Maven modules, the Content Types module brings this functionality also to the light modules. Both approaches are valid.
Whichever approach you choose, we recommend not to develop and refine these definitions in a productive environment.
Editing node type and workspace definitions can lead to new node type definitions and workspaces which are registered again, whereas the system keeps the "old" ones which become obsolete.
By utilizing the magnolia-content-types
module you can define custom JCR content types and workspaces within light modules.
Defining content types within light modules can be accomplished on a running Magnolia system without redeploying the WAR file of your Magnolia instances and without restarting the instance or a module. This makes it a perfect approach if you have a Magnolia Cloud subscription package.
You can define all of the following in just one file:
mgnl:content
. To define more sophisticated node types, you can create a node type definition file in a light module. Example
datasource: workspace: tourguides namespaces: mt: https://www.magnolia-travel.com/jcr/1.0/mt autoCreate: true model: nodeType: mt:tourGuide properties: - name: birthday type: Date - name: gender - name: shortBio
You can define JCR namespaces and nodetypes in a CND file.
While XML-based node type definitions are still supported, we recommend using CND.
Since this definition must be readable as a Magnolia Resource, it can be a file in a light module or in a JAR file or in the resources
JCR workspace.
The CND node type definition resource is loaded only if the CND resource is referenced in the Content type Data source definition of a Content type definition.
Example:
Create a CND file in your light module and define a namespace and node types:
<mt = 'http://www.magnolia.info/jcr/mt'> [mt:traveller] > mgnl:content orderable [mt:tourGuide] > mt:traveller orderable [mt:happyCustomer] > mt:traveller orderable
Reference the node type definition from a content type definition:
datasource: workspace: happycustomers autoCreate: true nodeTypeDefinition: /content-type-examples/jcr-node-type-files/travellers-node-types.cnd model: nodeType: mt:happyCustomer properties: - name: country - name: age type: Double
nodeTypeDefinition
.With Magnolia Maven modules you can register new JCR workspaces and node types. The registration involves the XML-based module descriptor.
Magnolia registers workspaces and node types during a module's start-up phase in case they have not yet been registered.
Example of a module descriptor:
In the XML-based module descriptor you can add a <workspace />
section in /repositories/repository/workspaces
.
<workspaces> <workspace>products</workspace> </workspaces>
To define and register new node types requires two things:
Creating a node type definition file (CND or XML) where you define the required node types and name spaces. Create the file in the my-maven-module/src/main/resources/mgnl-nodetypes/
folder.
Example:
<'mgnl' = 'http://www.magnolia.info/jcr/mgnl'> [mt:product] > mgnl:content orderable
Referencing the node type file in the XML-based module descriptor:
Example:
<nodeTypeFile>/mgnl-nodetypes/my-node-types.cnd</nodeTypeFile>
2 Comments
Will Scheidegger
It seems like at least defining node types through a Module Descriptor and custom node types file only works for the
magnolia
repository, e.g. a clustered repo. As soon as you define a second repository, the "mgnl:*" node types likemgnl:content
are missing. You will get aRepositoryException
when the custom node type is registered:This occurs on Magnolia 6.1, but I'm quite certain it does work in Magnolia 5.7.
Any instructions on this page how to add custom node types in repositories other than
magnolia
would be greatly appreciated!Will Scheidegger
So, after much try&error I found out that Magnolia will actually register the default node types if the repo is setup correctly. Back "in the old days" we always setup the repositories by modifying
WEB-INF/config/default/repositories.xml
. Here, we added a new repo definition, workspaces, and workspace mappings. This could end up looking like this:(For this to work, you'll also need to create a corresponding jackrabbit configuration file, add a property
magnolia.repositories.jackrabbit.clustered.config
to your properties file and set its value to the path of that config file.)Then, life got simply with the possibility to define workspaces and reference node type files in the module descriptor as described on this page. Slowly but surely I forgot about the old way of doing things, because as long as you are working with the default
magnolia
repository, the module descriptor is all you'll ever need to touch (or a content types definition).However, if you need your workspaces to live a separate repository, all the comfort is gone again. The module descriptor might give you the impression that it can create a repository (after all you can also create a workspace there), but this is not really the case. You'll need to configure it the old way. But don't try to define your node types there, because at that time, the default magnolia node types are not registered yet and you'll end up with the unknown node type error message.
So what worked for me now:
repositories.xml
config file. Do not define any workspaces or mappings there (so remove line 28 and 58 in the sample above). Magnolia will initialise the repository and register all default magnolia node types. However node types of modules like dam or categorisation are not registered!<repository>
element.mgnl:content
or alike.Your definition in the module descriptor could look like this: