Foros de discusión

Liferay 7 get link to page name in freemarker

thumbnail
William Gosse, modificado hace 6 años.

Liferay 7 get link to page name in freemarker

Liferay Master Mensajes: 533 Fecha de incorporación: 4/07/10 Mensajes recientes
I've got the following repeating structure:
{
    "availableLanguageIds": [
        "en_US"
    ],
    "defaultLanguageId": "en_US",
    "fields": [
        {
            "label": {
                "en_US": "Select image"
            },
            "predefinedValue": {
                "en_US": ""
            },
            "style": {
                "en_US": ""
            },
            "tip": {
                "en_US": ""
            },
            "dataType": "document-library",
            "fieldNamespace": "ddm",
            "indexType": "keyword",
            "localizable": true,
            "name": "carouselimage",
            "readOnly": false,
            "repeatable": true,
            "required": false,
            "showLabel": true,
            "type": "ddm-documentlibrary",
            "nestedFields": [
                {
                    "label": {
                        "en_US": "Select page"
                    },
                    "predefinedValue": {
                        "en_US": ""
                    },
                    "style": {
                        "en_US": ""
                    },
                    "tip": {
                        "en_US": ""
                    },
                    "dataType": "link-to-page",
                    "fieldNamespace": "ddm",
                    "indexType": "keyword",
                    "localizable": true,
                    "name": "pagelink",
                    "readOnly": false,
                    "repeatable": false,
                    "required": false,
                    "showLabel": true,
                    "type": "ddm-link-to-page"
                }
            ]
        }
    ]
}


I'm trying to access the page names for the link-to-pages that were selected. Here's my template for this structure:
<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    &lt;#if carouselimage.getSiblings()?has_content&gt;
    	&lt;#list carouselimage.getSiblings() as x&gt;
        	&lt;#if x?index == 0&gt;
                <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
            &lt;#else&gt;
                <li data-target="#myCarousel" data-slide-to="${x?index}"></li>
            <!--#if-->
     	<!--#list-->
    <!--#if-->  
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
    &lt;#if carouselimage.getSiblings()?has_content&gt;
    	&lt;#list carouselimage.getSiblings() as x&gt;
    		&lt;#if x.getData()?? &amp;&amp; x.getData() != ""&gt;
                &lt;#assign page=x.getChildren()[0]&gt;    		    
     		    &lt;#if x?index == 0&gt;
            		<div class="item active">
                		<a href="${page.getFriendlyUrl()}">
                			<img src="${x.getData()}" alt="${page.getTitle()}">
                		</a>	
            		</div>	
        		 &lt;#else&gt;
             		<div class="item">
               		    <a href="${page.getFriendlyUrl()}">
                			<img src="${x.getData()}" alt="${page.getTitle()}">
                		</a>	
            		</div>	
                <!--#if-->
    		<!--#if-->
    	<!--#list-->
    <!--#if-->
  </div>

  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>


I can get the URL for the page just fine but can't figure out how to get the name of the page
thumbnail
William Gosse, modificado hace 6 años.

RE: Liferay 7 get link to page name in freemarker (Respuesta)

Liferay Master Mensajes: 533 Fecha de incorporación: 4/07/10 Mensajes recientes
I decided to for go getting the page name and just added some additional text fields to the structure which allows the to specify an optional caption title and description. This web content structure and template was for an image carousel that uses the bootstrap carousel tags. Here's the code if anyone is interested.

Structure:
 {
    "availableLanguageIds": [
        "en_US"
    ],
    "defaultLanguageId": "en_US",
    "fields": [
        {
            "label": {
                "en_US": "Select image"
            },
            "predefinedValue": {
                "en_US": ""
            },
            "style": {
                "en_US": ""
            },
            "tip": {
                "en_US": ""
            },
            "dataType": "document-library",
            "fieldNamespace": "ddm",
            "indexType": "keyword",
            "localizable": true,
            "name": "carouselimage",
            "readOnly": false,
            "repeatable": true,
            "required": true,
            "showLabel": true,
            "type": "ddm-documentlibrary",
            "nestedFields": [
                {
                    "label": {
                        "en_US": "Select page (optional)"
                    },
                    "predefinedValue": {
                        "en_US": ""
                    },
                    "style": {
                        "en_US": ""
                    },
                    "tip": {
                        "en_US": ""
                    },
                    "dataType": "link-to-page",
                    "fieldNamespace": "ddm",
                    "indexType": "keyword",
                    "localizable": true,
                    "name": "pagelink",
                    "readOnly": false,
                    "repeatable": false,
                    "required": false,
                    "showLabel": true,
                    "type": "ddm-link-to-page"
                },
                {
                    "label": {
                        "en_US": "Enter caption title (optional)"
                    },
                    "predefinedValue": {
                        "en_US": ""
                    },
                    "style": {
                        "en_US": ""
                    },
                    "tip": {
                        "en_US": ""
                    },
                    "dataType": "string",
                    "indexType": "keyword",
                    "localizable": true,
                    "name": "captiontitle",
                    "readOnly": false,
                    "repeatable": false,
                    "required": false,
                    "showLabel": true,
                    "type": "text"
                },
                {
                    "label": {
                        "en_US": "Enter caption description (optional)"
                    },
                    "predefinedValue": {
                        "en_US": ""
                    },
                    "style": {
                        "en_US": ""
                    },
                    "tip": {
                        "en_US": ""
                    },
                    "dataType": "string",
                    "indexType": "keyword",
                    "localizable": true,
                    "name": "Text8vmg",
                    "readOnly": false,
                    "repeatable": false,
                    "required": false,
                    "showLabel": true,
                    "type": "text"
                }
            ]
        }
    ]
}


Template:
&lt;#macro showimage image page title description&gt;
   	&lt;#if page?? &amp;&amp; page.getFriendlyUrl() != ""&gt;
		<a href="${page.getFriendlyUrl()}">
		    <img src="${image.getData()}" alt="${title.getData()}" class="img-responsive">
		</a>
	&lt;#else&gt;
		<img src="${image.getData()}" alt="${title.getData()}" class="img-responsive">
	<!--#if-->    
    <div class="carousel-caption">
	    &lt;#if title?has_content &amp;&amp; page?? &amp;&amp; page.getFriendlyUrl() != ""&gt;
	        <a href="${page.getFriendlyUrl()}" style="color: #fff;">
                <h3>${title.getData()}</h3>
            </a> 
        &lt;#elseif title?has_content&gt;
            <h3>${title.getData()}</h3>    
        <!--#if-->   
        &lt;#if description?has_content &amp;&amp; page?? &amp;&amp; page.getFriendlyUrl() != ""&gt;
	        <a href="${page.getFriendlyUrl()}" style="color: #fff;">
	            <p>${description.getData()}</p>
	        </a>
        &lt;#elseif description?has_content&gt;	            
            <p>${description.getData()}</p>
        <!--#if-->         
    </div>
<!--#macro-->

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    &lt;#if carouselimage.getSiblings()?has_content&gt;
    	&lt;#list carouselimage.getSiblings() as x&gt;
        	&lt;#if x?index == 0&gt;
                <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
            &lt;#else&gt;
                <li data-target="#myCarousel" data-slide-to="${x?index}"></li>
            <!--#if-->
     	<!--#list-->
    <!--#if-->  
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner">
    &lt;#if carouselimage.getSiblings()?has_content&gt;
    	&lt;#list carouselimage.getSiblings() as image&gt;
    		&lt;#if image.getData()?? &amp;&amp; image.getData() != ""&gt;
                &lt;#assign page=image.getChildren()[0]&gt;    		    
                &lt;#assign title=image.getChildren()[1]&gt;    		    
                &lt;#assign description=image.getChildren()[2]&gt;    		    
     		    &lt;#if image?index == 0&gt;
            		<div class="item active">
            		    &lt;@showimage image=image page=page title=title description=description/&gt;
            		</div>
        		 &lt;#else&gt;
             		<div class="item">
             		    &lt;@showimage image=image page=page title=title description=description/&gt;
            		</div>	
                <!--#if-->
    		<!--#if-->
    	<!--#list-->
    <!--#if-->
  </div>

  <!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
Thomas Kellerer, modificado hace 6 años.

RE: Liferay 7 get link to page name in freemarker

Expert Mensajes: 490 Fecha de incorporación: 9/06/08 Mensajes recientes
William Gosse:
I've got the following repeating structure:
I can get the URL for the page just fine but can't figure out how to get the name of the page

I am struggling with the same problem. But I can't use the workaround to define my own title in the structure. I have to use the name of the page the link points to.

Is there a way to get the localized name of that page?

Where is the API of all the elements that can be used in a template for a WebContent documented?
thumbnail
William Gosse, modificado hace 6 años.

RE: Liferay 7 get link to page name in freemarker

Liferay Master Mensajes: 533 Fecha de incorporación: 4/07/10 Mensajes recientes
There are ways to get the current page in a template. This problem was a little different. I originally wanted to get the name of the page that was selected in a structure's link-to-page field. I could not find that anywhere. The only thing that seemed to be available was the selected page's url.
thumbnail
Christoph Rabel, modificado hace 6 años.

RE: Liferay 7 get link to page name in freemarker

Liferay Legend Mensajes: 1554 Fecha de incorporación: 24/09/09 Mensajes recientes
Personally, I think that's a bug.

A link to page is a TemplateNode in Freemarker. And TemplateNode has a getFriendlyURL, a getURL and a getName() function. While the url methods return the urls of the page, the getName() method returns the name of the variable. Which is kinda useless. I think the getName method should return the name of the page in this case. But then it would also need a language parameter, I guess.

Well, anyway:
Since it is a TemplateNode, could use getLayoutId() to get the ID and then fetch the layout using LayoutLocalService.
https://docs.liferay.com/portal/7.0-ga3/javadocs/portal-kernel/com/liferay/portal/kernel/templateparser/TemplateNode.html
thumbnail
William Gosse, modificado hace 6 años.

RE: Liferay 7 get link to page name in freemarker

Liferay Master Mensajes: 533 Fecha de incorporación: 4/07/10 Mensajes recientes
I totally agree or have a method like getPageName. Thanks
Beat Durrer, modificado hace 6 años.

RE: Liferay 7 get link to page name in freemarker

New Member Mensaje: 1 Fecha de incorporación: 16/06/14 Mensajes recientes
Christoph Rabel:
Since it is a TemplateNode, could use getLayoutId() to get the ID and then fetch the layout using LayoutLocalService.


That would be too easy. It is a protected method, you cannot simply call it.
It's simply ridiculous that there is a getFriendlyUrl but no getLinkTitle. Even the content editor shows the title, so why so stupid!

Anyway, here is a solution, that works (at least on public links which are not cross-site or anything)
First, remove the access restriction in freemarker: https://stackoverflow.com/a/43133190/304179
Then use this code:

&lt;#assign layoutService = serviceLocator.findService("com.liferay.portal.kernel.service.LayoutLocalService") /&gt;
&lt;#assign serviceContext = staticUtil["com.liferay.portal.kernel.service.ServiceContextThreadLocal"].getServiceContext() /&gt;
&lt;#assign themeDisplay = serviceContext.getThemeDisplay() /&gt;

&lt;#list link.getSiblings() as cur_link&gt;
    <a href="${cur_link.getFriendlyUrl()}" class="quicklink">
         &lt;#assign layout = layoutService.getLayout(themeDisplay.getScopeGroupId(),false,cur_link.getData()?number) /&gt;
        &lt;#assign pageTitle = layout.getName(locale) /&gt;
        ${pageTitle}
    </a>
<!--#list-->