[MTE-121] Provide an easy way to get absolute external link for pages and assets Created: 08/Jan/21  Updated: 16/Feb/21

Status: Open
Project: Magnolia Templating Essentials
Component/s: None
Affects Version/s: 1.5.2
Fix Version/s: None

Type: Improvement Priority: Neutral
Reporter: Viet Nguyen Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Date of First Response:

 Description   

Current status:

I am trying to retrieve the external URL for a (user-selected) page or asset.

  • including full (current!) domain and context (Site Module enabled)
  • for assets: the direct URL to the asset, not a responsive asset rendition (Imaging Module enabled, however I DONT want a compressed version of the image)

Our use case is very simple:
For Pages:

<meta property="og:url" content="https://www.somedomain.de/path/to/current/page.html" />

For Assets:

  • User selects an image in page properties dialog
  • Image should be used in html header for open graph tag
    <meta property="og:image" content="https://www.somedomain.de/path/to/image.jpg" />
    

I have checked many different functions in cmsfn / sitefn / damfn. However, I have no luck retrieving full URLs including the current domain...

Expected improvement

  • Provide an easy way and to have this

Workaround

Workaround 1 with FTL code:

Appending server URL to the generated links may help, please adapt it to your needs.

<h3>Test links</h3>
    <p>
      [#assign rootNode = cmsfn.contentByPath("/travel")]
      [#assign localLink = cmsfn.link(rootNode)]
      [#assign serverPort = ctx.request.serverPort]
      [#assign serverPort = serverPort?replace(",", "")]
      [#assign serverUrl = ctx.request.scheme + "://" + ctx.request.serverName + ":" + serverPort]
      Context path = ${ctx.request.contextPath} <br/>
      Server Url = ${serverUrl} <br/>
      [#assign externalLink = serverUrl + cmsfn.link(rootNode)]
      Local link: <a href="${localLink}">${rootNode.title!}</a><br/>
      External link: <a href="${externalLink}">${rootNode.title!}</a><br/>
    </p>
Workaround 2 with custom Java code as an extended templating function:
private static final String JCR_WORKSPACE_ASSET = "dam";
private static final String ASSET_PREFIX = "jcr:";

/**
	 * Retrieves fully qualified URL for pages in WEBSITE workspace
	 * 
	 * @param identifier
	 *            JCR Identifier of page
	 * @return URL to page including schema and domain
	 */
	public String getExternalUrlForPage(final String identifier) {
		if (StringUtils.isNotBlank(identifier) && !StringUtils.startsWith(identifier, ASSET_PREFIX)) {
			final Node node = this.templatingFunctions.nodeById(identifier);
			return node != null ? LinkUtil.createExternalLink(node) : StringUtils.EMPTY;
		} else {
			return StringUtils.EMPTY;
		}
	}

	/**
	 * Retrieves fully qualified URL for assets in ASSET workspace
	 *
	 * @param identifier
	 *            JCR Identifier of asset
	 * @return URL to asset including schema and domain
	 */
	public String getExternalUrlForAsset(final String identifier) {
		if (StringUtils.isNotBlank(identifier) && StringUtils.startsWith(identifier, ASSET_PREFIX)) {
			final Node node = this.templatingFunctions.nodeById(StringUtils.remove(identifier, ASSET_PREFIX),
					JCR_WORKSPACE_ASSET);
			return node != null ? LinkUtil.createExternalLink(node) : StringUtils.EMPTY;
		} else {
			return StringUtils.EMPTY;
		}
	}

Generated at Mon Feb 12 07:41:43 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.