Uploaded image for project: 'Magnolia Templating Essentials'
  1. Magnolia Templating Essentials
  2. MTE-121

Provide an easy way to get absolute external link for pages and assets

    XMLWordPrintable

Details

    • Improvement
    • Resolution: Unresolved
    • Neutral
    • None
    • 1.5.2
    • None
    • None

    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;
      		}
      	}
      

      Checklists

        Acceptance criteria

        Attachments

          Activity

            People

              Unassigned Unassigned
              viet.nguyen Viet Nguyen
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Checklists

                  Task DoD