3.2Creating URLs
There are different types of documents that can be created in the CMS. When using page types like an article or a news page the HTML page will have links to to pages filled from those documents. But also there will be document types like teaser documents or navigation documents which will be used to structure informations but not be produced as pages by themself.
The Generators generatorconfig.txt declares which document types are page types where URLs can be created to for instance with
doctype=news, .html, page-news.jsp
doctype=article, .html, page-article.jsp
Also it declares all document types which are allowed for includes like
doctype=teaser, .html
doctype=container, .html
Page types are also automaticly includeable types.
And finally if a medium document type contains a blob property which should be addressed by a URL the declaration should be
blobtype=medium, data, blob.jsp
The Generator service is in charge of the strategy to create URLs. The DocumentModel gives a simple access to the default URLs.
String url = dmPage.toLink().getUrl();
Referenceables
If the editor should be enabled to create more advanced links there is the Referenceable interface containing the following fields
Internal link
of type Linklist
External link
of type String
Link type
an integer from a dropdown selection
Link paramter
of type String for additional parameters
A navigation document or a teaser document would have all those fields beside others like a navi title or a teaser picture.
These fields enable to link not only to another document but also to an external URL. It gives the option to open the link in a new window or with other link types to trigger any tracking.
To use one central interface to create an URL from this informations from different document types the DocumentModelConstantsAdapter can be configured within the documentmodel.jsf.
public int[] getRT_REFERENCEABLE() { return new int[] { RT_REFERENCE, RT_NAVIGATION };}
public String getPT_REFERENCE_LINK_INTERNAL() { return PT_REFERENCE_LINK_INTERNAL; }
public String getPT_REFERENCE_LINK_EXTERNAL() { return PT_REFERENCE_LINK_EXTERNAL; }
public String getPT_REFERENCE_LINK_TYPE() { return PT_REFERENCE_LINK_TYPE; }
public String getPT_REFERENCE_LINK_PARAMETER() { return PT_REFERENCE_LINK_PARAMETER; }The field names of the Reference fields have to be named the same in every referenceable document type.
After that a RefernceModel can be used to create URLs either from page documents or from Referenceable documents.
String url = new app.cmsworks.cms.document.ReferenceModel(dmAny).toLink().getUrl().
If the link is optional, the Method toLink() will return null if the link fields are not filled or if the generator has no configuration to create a link to the targeted document. So this should be checked.
String text = "I am a link";
app.cmsworks.cms.document.Link link = new ReferenceModel(dmAny).toLink();
if (link != null) {
text = "<a " + link.createAnchorTarget() + ">" + text + "</a>";
}
But if the link is mandatory, use the ErrorView to handle this.
String text = "<a " + new ReferenceModel(dmAny).toLink(errors).createAnchorTarget() + ">I am a link</a>";
