In this tutorial, you learn how to create and use content types, how to create a content app based on a content type, how to adapt the app defaults and how to localize it. All the features developed within the tutorial will be part of a Magnolia light module.
The tutorial is suitable for beginners. However, if you are completely new to Magnolia, we recommend you try the Hello Magnolia tutorial first.
What is a content type?
A Magnolia content type is a formal definition for a type of content in Magnolia including the properties the type may contain and its relationships to other types of content. A content type is configured in a content type definition that includes the data source definition and the model definition.
Content type items can be managed by a content app. The content can be embedded into web pages or served via REST in a headless approach.
Magnolia Travels is a travel agency that employs many tour guides. The marketing department of Magnolia Travels wants to promote their popular tour guides on their website as well as on other channels. They decide to record the following information for each tour guide:
- Short biography
To begin, you need a Magnolia bundle where you will create a new light module to contain your code.
You will then configure a content type definition to map the given use case. Then you will create a content app, referring to the content type definition, to manage data for the tour guides.
For development and testing you need a running instance of Magnolia. In this tutorial, we suppose that it is installed in a directory called
Creating a light module
The light module serves as a container to which you will add the definitions for content types and for the apps based on those content types.
- Use the shell and change into the
light-modulesdirectory of your Magnolia installation.
There, create the following folder structure:
All the files you create in the following sections will go into these folders.
You could use the Magnolia CLI
create-light-modulecommand to create the
content-type-exampleslight module. However, in this tutorial we only use a reduced set of the subfolders which would be created by the CLI.
Defining a content type for a tour guide
In this step, you create the content type definition.
Create the file
/content-type-examples/contentTypes/tourGuide.yaml. The file must be in the light module subfolder
contentTypes. Magnolia scans this directory for content type definitions. If the file is syntactically correct, Magnolia registers the defined items.
A content type definition requires a
model and a
Defining the model
Start with the model definition. In the model, you define the properties of a content item.
Following our use case, add this snippet to the
- Add one entry to the
propertieslist for each characteristic of a tour guide. Each entry should start with a
genderproperty will be rendered as a select field.
- Specify property types. Possible types are:
richText, or another content type or submodel.
- Note that we have not defined a property named
nameeven though our use case states that we should store the names of the tour guides. We capitalize on a feature that automatically creates a
nameproperty for the app to manage the content type.
- Ignore the
nodeTypeon the second line for the time being.
Defining the data source
The data source definition defines how content type items are persisted.
The Magnolia content type framework is data source agnostic, but Magnolia's default implementation uses JCR to persist the data. That's why you should be aware of a few basics concerning JCR.
Understanding JCR basics: node type, namespace, and workspace
Storing data in JCR requires: a registered workspace; a defined and registered node type; and the node type may use a namespace that must be registered too.
- Workspace. The workspace is a container which stores JCR nodes in a tree-like structure. A workspace has a name. See Workspaces for a list of some predefined Magnolia workspaces.
- Node type. A node stores an item of a certain type. Nodes contain properties. A node type defines the nature of a node, it has a name, which typically starts with
namespace:. See Node types for a list of some predefined Magnolia node types.
- Namespace. The namespace indicates a certain domain. Node types that belong to the same domain share the same namespace. The namespace
ntis used for node types provided by the JCR implementation,
mgnlis the namespace for most of the Magnolia-specific node types.
Defining a namespace, node type and workspace for the tour guide content items
Define a distinct namespace, node type definition and workspace for the use case.
Note that you can define and register namespaces, node types and workspaces in different ways.
In this tutorial, we will define them in a single YAML file: the content type definition file. This approach is fast and suitable for many use cases.
|Namespace||Stands for Magnolia Travels. Use this namespace for all items related to the domain of Magnolia Travels.|
|Node type name||The name of a node to store the data for a tour guide.|
|Workspace name||The workspace to store the |
Now we define the data source:
workspace– The name of the JCR workspace to store the content items.
namespaces– A list of JCR namespace names.
autocreate– If set to
true, both the workspace and the namespaces will be registered by the system, if they have not been registered yet.
The complete content type definition
This is the complete content type definition with both the
model and the
- Line 8,
nodeType: The name of the JCR node type for storing an item of the given content type. If
nodeTypeis not provided,
mgnl:contentis used by default.
Checking the content type definition in the Definitions app and the workspace in the JCR app
If your Magnolia instance is still running and if you have added the content type definition file in the correct location in a light module known to Magnolia, you can check the content type in the Definitions app.
If you open the JCR Browser app, you can see that the workspace pulldown lists also the
Editing content type definitions
You can edit content type definitions while Magnolia is running. The system detects the changes and updates the definition.
Be careful when editing:
- Develop and refine content type definitions in a development environment. Avoid editing content type definitions in a production environment.
- Changing the JCR-related parameters of a content type definition may have extensive consequences:
- Changing the name of a workspace will unregister the old workspace and register the new one. However, the old workspace will not be completely deleted. (See How to delete a JCR workspace).
- Changing a node type unregisters the old node type and registers a new one. Data created with the old node type then becomes difficult to edit. You may have to migrate it if you rely on it.
Creating a content app based on the content type
With just a content type definition, you cannot manage content items of the defined type. In this section, you create a Magnolia content app to create, edit, and delete tour guide items.
What is a content app?
A Content app is a specialized app type for managing structured content. The content app user interface consists of a browser subapp and one or more detail subapps. Content apps make it easy to enter items such as products or events. Many native Magnolia apps such as Tours and Contacts are content apps. Because this app style is used often, the framework provides convenience classes to make building a content app faster.
Referencing a content type from an app descriptor
A content app is configured with an app descriptor, sometimes also referred to as the app definition.
- Switch to the
- There, create a file called
tourGuides-app.yaml– the app descriptor.
Edit the descriptor to contain the following two lines:
When you reference a content type definition by name, the Magnolia UI framework generates a virtual app descriptor and registers the app with the app name. In the above case, the app is registered with the name
There is no way to see the generated app descriptor in a file but you can analyze the structure of the generated app in the Definitions app.
Starting the app
Magnolia adds a tile for your new app to the App launcher automatically when registering the app. However, to make the tile appear in the App launcher, you must first restart your Magnolia session once by logging out and logging in again. For further details see App launcher layout.
To see the list of the app tiles, click the app launcher iconto the right of the Find Bar.
To start the app, click the app tile in the App launcher:
Create the file
Overriding the auto-generated app descriptor parts
The generated app structure provides a standard starting point that you can adapt.
You can override the definitions in the app descriptor. If you make a typo or another error, the Definitions app Problems tab indicates the error.
If you have to fix an error:
- Fix it and save the app descriptor file.
- You may also have to touch the content definition file to trigger the app generation process again.
In the second part of the tutorial, an example of overriding is the
vehicleType property in the
tourVehicles-app. The property is a select field, which could be tentatively defined as such directly in the model using the
options property, but for illustration purposes, it is configured (overridden) as a select field in the app descriptor using the
Default app creation patterns
With the minimal app definition shown above, the generated app has a default set of features. The precise generated structure can be seen in the Definitions app.
Here is an overview of what is generated:
- Two subapps:
editorof the browser subapp has a single form with one tab named
The editor displays a field for each property in the content type model. Depending on the attributes of a model property, the form renders different fields:
<content-type-definition>/model/properties/ does not contain a property with
name: name, the app generator automatically adds this property.
The value given to the name property is also used to deduce a feasible JCR node name.
- Value of the name property: John Miller
- Deduced JCR node name: John-Miller
You have just created your first content type and built a customized app to manage the content type items.
Create a few tour guides before you proceed with Part II - Complex content types and security set up.