Resolving references with the delivery endpoint
This page explains how to use the delivery endpoint so that it resolves references to nodes from different workspaces.
The information on this page only applies to the Delivery API.
What is reference resolving?
The endpoints of the delivery API are configured to return content as
JSON from one JCR workspace (configured with the workspace
property).
Here is a JSON response of a tour from the workspace tours
.
{
"@name": "Kyoto",
"@path": "/magnolia-travels/Kyoto",
"@id": "b475f27e-2929-427b-9517-815118a3b36e",
"@nodeType": "mgnl:content",
"body": "<p>Experience the still beauty that permeates and surrounds Kyoto. Kyoto is famous for many things, including countless <a href=\"http://www.japan-guide.com/e/e2058.html\" style=\"text-decoration:none;\" target=\"_blank\"><u>temples</u></a>, <a href=\"http://www.japan-guide.com/e/e2059.html\" style=\"text-decoration:none;\" target=\"_blank\"><u>shrines</u></a> and other fascinating historical buildings.</p>\n<p>Join us on a visit to the City of Ten Thousand Shrines. We'll visit Tō-ji, Ginkaku-ji, Kōzan-ji and their remarkable gardens before venturing into the mountains that surround the city. You’ll enjoy a day spent in green Miyama and a day hiking in Shizuhara. You’ll never think about Japan in the same way again.</p> ",
"name": "Kyoto",
"description": "The natural side of Japan",
"destination": [
"7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2"
],
"location": "Kyoto, Japan",
"tourTypes": [
"d2245867-ecaa-4b4e-8743-e0c939be68b7",
"415025c6-e4b5-4506-9384-34f428a52104"
],
"author": "Magnolia Travels",
"duration": "7",
"image": "jcr:44689d29-5966-4d41-8fd4-2dc7da783528",
"@nodes": []
}
-
Lines 13-15: The
tourTypes
property contains two references to tour type nodes that reside in thecategory
workspace. -
Line 19: The
image
property contains the asset identifier, which is the reference to an asset. In this case the asset is stored in the Magnolia DAM in thedam
workspace. In other cases, the asset could be in another asset provider.
We could send additional requests to deliver content for the category
and dam
workspaces. However, it is more efficient to get the data of
the referenced nodes directly with the first request by resolving the
references.
In the following sections, we show you how to configure the delivery endpoint in order to resolve the references.
Details concerning the example:
The JSON is requested with this URL: http://localhost:8080/magnoliaAuthor/.rest/tours/magnolia-travels/Kyoto
The basic configuration for the delivery endpoint to read tours in the example so far is:
restEndpoint/tours.yaml
workspace: tours
#references
The references
and the referenceResolver
properties
The references
property is used in info.magnolia.rest.delivery.jcr.v2.JcrDeliveryEndpointDefinition for the Delivery API.
references:
- name: tourTypeReference
propertyName: tourTypes
referenceResolver:
$type: jcrReferenceResolver
targetWorkspace: category
Properties
references
The Specific referencing properties for resolving JCR node references,
asset references, or asset links in rich text fields must be defined under the |
The properties of a single reference are defined in info.magnolia.rest.reference.ReferenceDefinition.
Property | Description |
---|---|
|
optional An arbitrary name. If you do not set the property, the first property
must start with a |
|
required The value of this property contains the name of the JCR node property that stores one or multiple references to nodes of other workspaces. |
|
optional The value must be a valid node type. If set, references are resolved only on nodes of the given type. |
|
required The reference resolver defines how references are resolved. The properties are defined in info.magnolia.rest.reference.ReferenceResolverDefinition. |
|
required (unless Possible values are:
|
|
required (unless The value must refer to a class or interface extending info.magnolia.rest.reference.ReferenceResolverDefinition. Reference resolver definition classes provided by Magnolia:
Note that these classes may have further properties that must be configured. |
When using the
Where are these configured?
Example /<module-name>/restEndpoints/delivery/my-endpoint.yaml
|
Resolving JCR node references
Magnolia provides a JCR node reference resolver that is suitable in most cases, with the exception of assets. Use the definition class info.magnolia.rest.reference.jcr.JcrReferenceResolverDefinition for configuration.
Example configuration
workspace: tours
references:
- name: tourTypeReference
propertyName: tourTypes
referenceResolver:
$type: jcrReferenceResolver
targetWorkspace: category
expand: true
generateLink: true
The lines 3-10 define how the references stored in the field tourTypes
are resolved.
{
"@name": "Kyoto",
"@path": "/magnolia-travels/Kyoto",
"@id": "b475f27e-2929-427b-9517-815118a3b36e",
"@nodeType": "mgnl:content",
"body": "<p>Experience the still beauty that permeates and surrounds Kyoto. Kyoto is famous for many things, including countless <a href=\"http://www.japan-guide.com/e/e2058.html\" style=\"text-decoration:none;\" target=\"_blank\"><u>temples</u></a>, <a href=\"http://www.japan-guide.com/e/e2059.html\" style=\"text-decoration:none;\" target=\"_blank\"><u>shrines</u></a> and other fascinating historical buildings.</p>\n<p>Join us on a visit to the City of Ten Thousand Shrines. We'll visit Tō-ji, Ginkaku-ji, Kōzan-ji and their remarkable gardens before venturing into the mountains that surround the city. You’ll enjoy a day spent in green Miyama and a day hiking in Shizuhara. You’ll never think about Japan in the same way again.</p> ",
"name": "Kyoto",
"description": "The natural side of Japan",
"destination": [
"7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2"
],
"location": "Kyoto, Japan",
"tourTypes": [
{
"@name": "ecotourism",
"@path": "/tour-types/ecotourism",
"@id": "d2245867-ecaa-4b4e-8743-e0c939be68b7",
"@nodeType": "mgnl:category",
"body": "Nature is calling you. And there’s nowhere more natural than Africa’s savannah plains or the icy wilderness of Antartica. But what’s even better than getting closer to nature is knowing that when you travel with us, you’re making your own contribution to keeping those untouched parts of earth just as they are.",
"name": "ecotourism",
"level": "level-1",
"description": "Call of the wild",
"icon": "jcr:8ad908e7-2ab7-4fe2-b1ed-e6eb3f68f3b0",
"displayName": "Ecotourism, Nature & Wildlife",
"image": "jcr:2fd89d97-f932-4533-8a30-d70988bde30c",
"@link": "/magnoliaAuthor/tour-types/ecotourism.html",
"@nodes": [
]
},
{
"@name": "offPath",
"@path": "/tour-types/offPath",
"@id": "415025c6-e4b5-4506-9384-34f428a52104",
"@nodeType": "mgnl:category",
"body": "Riding on a big tour bus, eating tourist menus and taking pictures of the Eiffel Tower is not your idea of travelling. You want to find something new, and see things from a local point of view. With us, you can discover the local gems that most travellers never experience. ",
"name": "offPath",
"level": "level-1",
"description": "Unique holidays that take you off the beaten track ",
"icon": "jcr:6d5d576a-79f9-48d9-b141-6682f2eef585",
"displayName": "Off the Beaten Path",
"image": "jcr:272f75b9-ed87-4e0b-8bf3-15db217ba897",
"@link": "/magnoliaAuthor/tour-types/offPath.html",
"@nodes": [
]
}
],
"author": "Magnolia Travels",
"duration": "7",
"image": "jcr:44689d29-5966-4d41-8fd4-2dc7da783528",
"@nodes": [
]
}
Properties
Property | Description |
---|---|
|
required node |
|
required (unless Must be |
|
required (unless Must be
|
|
required The name of the workspace containing the nodes that are referenced. |
|
optional, default is If set to If |
|
optional, default is If The format of the generated links depends on the configuration of the
link generator (configured in |
|
optional, default is Sets the depth in the referenced nodes. The default value of |
Depth in the referenced nodes
While the referenceDepth
property sets a specific depth for the returned "root" nodes, you can control the depth in the referenced nodes with the depthInReferencedNode
property.
Example: Depth in the referenced nodes in the Tour Finder page
In the Travel Demo, the Tour Finder page has the following structure:
http://localhost:8080/magnoliaAuthor/.magnolia/admincentral#app:jcr-browser-app:browser;/travel/tour-finder
Consider these three configuration cases:
-
You don’t configure a resolver at all. In this case, for the
tour-finder
node, you would only receive:... "tourFinder": "d25e69d9-27d0-4477-824c-7cc0714c97ce" ...
-
You do configure a resolver in your endpoint, but only set
1
as the value of thedepthInReferencedNode
property in it:references: - name: referenceToParentWithDefaultDepthInReferencedNode propertyName: tourFinder referenceResolver: $type: jcrReferenceResolver targetWorkspace: website depthInReferencedNode: 1
In this case, the
tourFinder
node is resolved and shows its content, but only to one level. Themain
andfooter
area nodes are not resolved any further:... "tourFinder": { "@name": "tour-finder", "@path": "/travel/tour-finder", "@id": "d25e69d9-27d0-4477-824c-7cc0714c97ce", "@nodeType": "mgnl:page", "hideInNav": true, "title": "Tour Finder", "main": { "@name": "main", "@path": "/travel/tour-finder/main", "@id": "9cafcc1e-e561-4f15-83ba-6019c8107a86", "@nodeType": "mgnl:area", "@nodes": [] }, "footer": { "@name": "footer", "@path": "/travel/tour-finder/footer", "@id": "ce31ad28-22b2-4c94-9bff-515ee7883634", "@nodeType": "mgnl:area", "@nodes": [] }, "@nodes": [ "main", "footer" ] } ...
-
You set
2
for thedepthInReferencedNode
property in the reference resolver:references: - name: referenceToParentWithDefaultDepthInReferencedNode propertyName: tourFinder referenceResolver: $type: jcrReferenceResolver targetWorkspace: website depthInReferencedNode: 2
In this case, even the
main
andfooter
area nodes are resolved:... "tourFinder": { "@name": "tour-finder", "@path": "/travel/tour-finder", "@id": "d25e69d9-27d0-4477-824c-7cc0714c97ce", "@nodeType": "mgnl:page", "hideInNav": true, "title": "Tour Finder", "main": { "@name": "main", "@path": "/travel/tour-finder/main", "@id": "9cafcc1e-e561-4f15-83ba-6019c8107a86", "@nodeType": "mgnl:area", "0": { "@name": "0", "@path": "/travel/tour-finder/main/0", "@id": "506bfd9a-ef9f-42bf-8650-f32a3b2fd841", "@nodeType": "mgnl:component", "@nodes": [] }, "@nodes": [ "0" ] }, "footer": { "@name": "footer", "@path": "/travel/tour-finder/footer", "@id": "ce31ad28-22b2-4c94-9bff-515ee7883634", "@nodeType": "mgnl:area", "footer1": { "@name": "footer1", "@path": "/travel/tour-finder/footer/footer1", "@id": "76c936d5-c305-4745-8e23-cfb694692bad", "@nodeType": "mgnl:area", "@nodes": [] }, "footer2": { "@name": "footer2", "@path": "/travel/tour-finder/footer/footer2", "@id": "a2d47f9d-eddf-493e-bf83-e7a64c7be2f0", "@nodeType": "mgnl:area", "@nodes": [] }, ...
Reference conflicts
You may have a situation where resolvers reference fields with an identical name because the fields are generated by a jcrMultiField
of different pages.
You can resolve this conflict by setting a different name for one of the fields through the name
property in the configuration of the field. In the following example, a custom name: categoryField
is configured for a jcrMultiField
:
categories:
$type: jcrMultiField
field:
name: categoryField
$type: linkField
required: true
datasource:
$type: jcrDatasource
workspace: category
rootPath: /
allowedNodeTypes:
- mgnl:folder
- mgnl:category
Resolving asset references
Magnolia REST modules come with an asset resolver, which creates a (relative) link to the image as well as Dublin Core metadata (if it exists for the asset). Use the info.magnolia.rest.reference.dam.AssetReferenceResolverDefinition class for configuration.
The Magnolia asset resolver operates on the DAM API.
If the referenced assets reside in the Magnolia DAM, the asset resolver can also generate links to asset renditions (also known as image variations). Renditions must be defined in the theme that is linked to the site definition. See image variations to learn how to define asset renditions in a theme.
For image variations to be resolved correctly, make sure that the fallback site has theme variations configured or extends a site definition with desired variations. |
Example configuration
This example is based on the basic configuration shown above.
restEndpoint/tours.yaml
workspace: tours
references:
- name: tourImageReference
propertyName: image
referenceResolver:
$type: assetReferenceResolver
assetRenditions:
- 320x240
- 1600x1200
- nonexistent
The lines 5-12 define how the references stored in the dam
workspace
are resolved.
{
"@name": "Kyoto",
"@path": "/magnolia-travels/Kyoto",
"@id": "b475f27e-2929-427b-9517-815118a3b36e",
"@nodeType": "mgnl:content",
"body": "<p>Experience the still beauty that permeates and surrounds Kyoto. Kyoto is famous for many things, including countless <a href=\"http://www.japan-guide.com/e/e2058.html\" style=\"text-decoration:none;\" target=\"_blank\"><u>temples</u></a>, <a href=\"http://www.japan-guide.com/e/e2059.html\" style=\"text-decoration:none;\" target=\"_blank\"><u>shrines</u></a> and other fascinating historical buildings.</p>\n<p>Join us on a visit to the City of Ten Thousand Shrines. We'll visit Tō-ji, Ginkaku-ji, Kōzan-ji and their remarkable gardens before venturing into the mountains that surround the city. You’ll enjoy a day spent in green Miyama and a day hiking in Shizuhara. You’ll never think about Japan in the same way again.</p> ",
"name": "Kyoto",
"description": "The natural side of Japan",
"destination": [
"7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2"
],
"location": "Kyoto, Japan",
"tourTypes": [
"d2245867-ecaa-4b4e-8743-e0c939be68b7",
"415025c6-e4b5-4506-9384-34f428a52104"
],
"author": "Magnolia Travels",
"duration": "7",
"image": {
"@name": "flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg",
"@path": "/tours/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg",
"@id": "jcr:44689d29-5966-4d41-8fd4-2dc7da783528",
"@link": "/magnoliaAuthor/dam/jcr:44689d29-5966-4d41-8fd4-2dc7da783528/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg",
"metadata": {
"fileName": "flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg",
"mimeType": "image/jpeg",
"caption": "Stairs to Ōminesan-ji temple",
"fileSize": "1115288",
"height": "2133",
"width": "1600",
"title": "Stairs to Ōminesan-ji temple",
"format": "image/jpeg",
"coverage": "Yoshino district, Nara prefecture, Japan",
"description": "Ōminesan-ji (大峯山寺 Ōminesan-ji) is an important temple of the Shugendō religion in Yoshino district, Nara prefecture, Japan. It is located at the peak of Mount Ōmine, or Sanjōgatake. According to tradition, it was founded by En no Ozunu, the founder of Shugendō, a form of mountain asceticism drawing from Buddhist and Shinto beliefs.",
"publisher": "Tim Notari",
"rights": "by-sa/2.0/",
"source": "https://www.flickr.com/photos/tasteful_tn/204886404/",
"creator": [
"superuser"
],
"contributor": [
"Tim Notari"
],
"subject": [
"Ōminesan-ji temple"
],
"date": "2019-01-17T10:30:29.618+01:00",
"created": "2015-01-29T08:45:41.613+01:00",
"modified": "2019-01-17T10:30:29.618+01:00"
},
"renditions": {
"320x240": {
"mimeType": "image/jpeg",
"link": "/magnoliaAuthor/.imaging/mte/travel-demo-theme/320x240/dam/tours/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg/jcr:content/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg"
},
"1600x1200": {
"mimeType": "image/jpeg",
"link": "/magnoliaAuthor/.imaging/mte/travel-demo-theme/1600x1200/dam/tours/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg/jcr:content/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg"
},
"nonexistent": {
"mimeType": "image/jpeg",
"link": "/magnoliaAuthor/.imaging/default/dam/tours/flickr-japan-gate-tasteful_tn-204886404_ab34d73333_o.jpg/jcr:content.jpg"
}
}
},
"@nodes": [
]
}
NOTE:
-
Line 23: The property
@link
provides a relative link to the asset. The asset is served by info.magnolia.dam.core.download.DamDownloadServlet. -
Line 51 pp - renditions. When requesting the links of the renditions, they are served by info.magnolia.imaging.ImagingServlet.
-
Line 54: The
link
contains a relative link to an asset rendition (of a defined image variation). -
Line 62: The
link
to an undefined rendition returns the original uploaded asset. Here it returns as@link
of line 51 (the former served by dam servlet, the latter served by imaging servlet).
-
Properties
Property | Description |
---|---|
|
required node |
|
required (unless Must be |
|
required (unless Must be
|
|
optional, default is If set to If |
|
optional, default is If set to |
|
optional, default is If set to |
|
optional A list of rendition names as defined in the theme linked to the current site. |
|
required A rendition name. To get the asset in its original (default) size, use If you add an undefined (a non-existent) rendition, the asset resolver creates a link to the default variation which is the same as the asset in its original size. |
Resolving asset links in rich text fields
We provide a resolver which helps resolve links for assets in the
rich
text field. In the following example, the first word Kyoto
in the
rich text field has been turned into a link to the kyoto.png
image
with UUID 0a3bb34f-b49f-4e02-a9e9-e46cf860b612
:
In the repository, this part of content is stored like this:
Without the asset link resolver, the endpoint returns what is stored in the repository, with escaped double quotes:
<p>Experience the still beauty that permeates and surrounds
<a href=\"${link:{uuid:{0a3bb34f-b49f-4e02-a9e9-e46cf860b612},repository:{dam},path:{/untitled}}}\">Kyoto</a>.
Kyoto is famous for many things,
If the resolver is set, the link to the asset will be resolved as a URL:
<p>Experience the still beauty that permeates and surrounds
<a href=\"http://localhost:8080/magnoliaAuthor/fallback/dam/jcr:0a3bb34f-b49f-4e02-a9e9-e46cf860b612/kyoto.jpg\">Kyoto</a>.
Kyoto is famous for many things,
Properties
Property | Description |
---|---|
|
required node |
|
required (unless Must be |
|
required (unless Must be
|
|
optional, default is The endpoint returns a full URL, using the value configured in
If set to
|