liferay change page title dynamically - liferay

I'm trying to change the title of the page, but the method PortalUtil.setPageTitle("title", request); is not working from the jsp. I also tried in the doView method.
My second attempt was throught servletrequest:
In doView I wrote
HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
httpRequest.setAttribute("hola", "hola");
And in the portal normal I tried with:
#set ($holas =$request.get('attributes').get('hola'))
#set ($holas2 = $request.getSession().getAttribute("hola"))
$holas
$holas2
but Velocity only shows $holas $holas2.

Looks like I got it wrong in my first attempt - thus I've replaced the previous answer with this one: Add this code to your JSP or doView:
<%
com.liferay.portal.util.PortalUtil.setPageTitle("Honk", request);
%>

In your jsp you should try
<%
layout.setTitle("title");
%>
layout is an Layout object generated by jsp.

Use below code,
String title = (String)renderRequest.getAttribute("title");
HtmlPageTitleUtil.setHtmlTitle(title, request, true);
Pass title attribute from the controller or you can use static text as well.
Import the above utility class as well as,
<portlet:defineObjects />
<theme:defineObjects />
this to jsp and its done.

Related

Kentico 9 cms:cmsTextBox placeholder localization

I've duplicated the search box webpart so i can make changes. I'm trying to add a localization string to the placeholder attribute.
This isn't working:
<cms:CMSTextBox ID="txtWord" runat="server" EnableViewState="false" MaxLength="1000"
ProcessMacroSecurity="false" placeholder="<%= CMS.Helpers.ResHelper.GetString("kff.Search--PlaceHolderCopy")%>" />
nor does this:
<cms:CMSTextBox ID="txtWord" runat="server" EnableViewState="false" MaxLength="1000"
ProcessMacroSecurity="false" placeholder='<%= CMS.Helpers.ResHelper.GetString("kff.Search--PlaceHolderCopy")%>' />
I have a JS Snippet that does work, but i'm hoping to avoid copy in JS files.
var $searchField = $('.searchTextbox');
if ($('body').hasClass('ENCA')) {
// search field placeholder copy
$searchField.attr('placeholder', 'Search For Stuff');
}
else {
$searchField.attr('placeholder', 'Recherche');
}
Can I add the localization string to the server tag, or should it be done in the code behind. I'm not sure the best location in the code behind for this either, I can't see a Page_Load block.
You could add the following line in the SetupControl method in the codebehind:
txtWord.Attributes.Add("placeholder", ResHelper.GetString("kff.Search--PlaceHolderCopy"));
You cannot really use the <%= syntax to set properties of server-side controls.
Also, please note that the CMSTextBox control has a WatermarkText property, which might be what you are looking for. It uses the TextBoxWatermarkExtender control from the AjaxControlToolkit library.
There is no need to duplicate the webpart and have duplicate code just for something this simple. Just create a different webpart layout for that webpart and add the following code above the Panel:
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
txtWord.Attributes.Add("placeholder", ResHelper.GetString("yourstring"));
}
</script>

p:remoteCommand returns <eval> twice in ajax response

I try to render a new page in a new window (or tab) with the link I get from a selected page object in an autoComplete component.
After trying multiple options the only chance in my opinion is to use javascript to catch the submit, trigger a remote command, that gives me a javascript call with the link attribute from the page object.
JSF-Snipplet (with reduced attributes in autoComplete)
<h:form id="autoCompleteForm">
<p:autoComplete id="autoComplete" widgetVar="autoCompleteWidget" value="#{pageBean.selectedPage}" />
<p:remoteCommand action="#{pageBean.showPage}" name="showPage" />
</h:form>
some JS:
// form submit
('#autoCompleteForm').on('submit', function(e) {
e.preventDefault();
showPage();
});
// open link
var openLink = function(pageLink) {
window.open(pageLink, '_blank');
};
Bean part for action
public void showPage() {
RequestContext context = RequestContext.getCurrentInstance();
context.execute("openLink('" + selectedPage.getLink() + ".xhtml')");
}
Everything works nice together, but the response contains the eval tag twice.
<partial-response>
<changes>
<update id="javax.faces.ViewState"><![CDATA[2851816213645347690:-2276123702509360418]]></update>
<eval><![CDATA[openLink('target.xhtml');]]></eval>
<eval><![CDATA[openLink('target.xhtml');]]></eval>
</changes>
</partial-response>
I tried different approaches with redirects or returning view names, but that all leads to no satisfying solutions (e.g. URL not changing or no new window).
Problem solved:
I had defined PrimePartialViewContextFactoryin my faces-config before:
<factory>
<partial-view-context-factory>org.primefaces.context.PrimePartialViewContextFactory</partial-view-context-factory>
</factory>
By removing it the application acts like expected.
This also solves a problem (JSON.parse: unexpected non-whitespace character after JSON data) with pagination and sorting in DataTables.

How to get client side portlet-id in liferay?

I'm using AlloyUI in my liferay portlet.
I want to use my <input>'s id in javascript. The problem is that the id of the elements are changed in client side.
For example:
If I set an <input>'s Id to "username" it is changed to _hospital_WAR_hospitalportlet_userName i.e. _hospital_WAR_hospitalportlet_ is appended to the Id, where Hospital is my portlet name.
How can I get client-side Id so that I can use it in jquery?
The string _hospital_WAR_hospitalportlet_ prepended to the Id of the <input> is nothing but the portlet-namespace.
This is only prepended to your <input>'s name & id attribute if you use <aui:input> tag and the name & id attributes are not changed if you just use plain-vanilla html <input> tag.
But as it is a good practice to use <aui:input> you can do the following to get the portlet-namespace in your javascript code:
If you are using javascripts inside a JSP i.e. using within <script> .. </script> or <aui:script> .. </aui:script> then you can use <portlet:namespace /> or <%= renderResponse.getNamespace() %> to get the string _hospital_WAR_hospitalportlet_ inside your javascript, something like.
jQuery("#<portlet:namespace />username").val();
// Or
jQuery("#<%= renderResponse.getNamespace() %>username").val();
But if you are using a *.js file then I suggest you pass the namespace as an argument to the javascript function in the js file:
function changeValues(portletNamespace) { // this function is in a js file
jQuery("#" + portletNamespace + "username").val("Lets change");
}
calling this function from the JSP:
<input type="button" onClick="changeValues('<portlet:namespace />')" />
Hope this helps. I don't know if there is a way to get the namespace or portletId directly through some javascript function defined by Liferay. If you get something like that you can post it here and that would be very helpful.
Try this:
Liferay.Portlet.ready(
/*
This function gets loaded after each and every portlet on the page.
portletId: the current portlet's id
node: the Alloy Node object of the current portlet
*/
function(portletId, node) {
}
);

How to hide portlet in liferay by using javascript

Actually on a single page I have 2 portlet but I want to hide the first portlet by clicking the submit button and only the second portlet should be visible I used the following code:
document.getElementById("portlet-id").style.visibility='none'
But after refreshing the page, again portlet is visible can anyone provide me the solution as to how I can proceed.
You can set the visibility of the portlet to false in the JSP by using the following code:
<%
renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE);
%>
This would hide your portlet from user's view.
Everytime your portlet is rendered you can check a parameter which was set in the request or session (your choice) to either show the portlet or not show the portlet, like:
<%
String paramFromRequestToHide = renderRequest.getParameter("hidePortlet");
// can also fetch from session: portletSession.getAttribute("hidePortlet");
if (paramFromRequestToHide .equals("YES")) { // you can use your favorite data-type
renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE);
} else {
renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);
}
%>
Another method:
If you don't want to go with the above approach then you can combine your javascript approach with the parameter approach as follows:
<%
String paramFromRequestToHide = renderRequest.getParameter("hidePortlet");
if (paramFromRequestToHide .equals("YES")) {
%>
<aui:script>
Liferay.Portlet.ready(
/*
This function gets loaded after each and every portlet on the page.
portletId: the current portlet's id
node: the Alloy Node object of the current portlet
*/
function(portletId, node) {
document.getElementById(portletId).style.display = 'none';
// or alternatively using pure Alloy UI
// node.hide();
}
);
</aui:script>
<%
} else {
%>
<aui:script>
Liferay.Portlet.ready(
function(portletId, node) {
document.getElementById(portletId).style.display = 'block';
// or alternatively using pure Alloy UI
// node.show();
}
);
</aui:script>
<%
}
%>
In case you want to check-out Alloy UI API and some of the demos to learn Alloy UI since starting from Liferay 6.1 Alloy UI is the de-facto javascript library for liferay. Now Alloy UI has an official web-site with many helpful tutorials and examples.
Hope this gives you ample material to proceed :-)
You also can do like this :
If your portlet id is : callcenter_WAR_xyzyportlet
$('#p_p_id_callcenter_WAR_xyzyportlet_').css({display:'none'});

Dynamically setting the ListId in the <SharePoint:ListView> control

Is there a way to dynamically set the ListId field in the ListView control. We cannot guarantee that the list we are interested in has a consistent GUID between installations (The list is deployed as part of a site template that we do not control). I've tried using the PreInit event to set a variable (see the list guid: section. If I remove the ListView tag, I see the proper GUID printed out. So I'm collecting the GUID correctly. However, The listview control errors out with the following message "Guid should contain 32 digits with 4 dashes". This tells me that the tag is not getting the variable set. Is this correct? Is there another way to specify the list to use?
Can this be done?
Sample code follows:
<%# Register TagPrefix="Sharepoint" ...details deleted.. %>
<br>
...stuff deleted.
<br>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
... more stuff deleted...
<p>list guid: <%=ListGuid %>
<Sharepoint:ListView ListId="<%=ListGuid %>" Enabled="true" runat="server" />
<p>
</asp:Content>
<script runat="server">
string ListGuid = string.Empty;
protected void Page_PreInit(object sender, EventArgs e)
{
SPSite Site = SPContext.Current.Site;
using (SPWeb HelpDesk = Site.OpenWeb("HelpDesk"))
{
SPList list = HelpDesk.Lists["Charge Numbers"];
ListGuid = list.ID.ToString();
}
}
</script>
Having server-side code like you do that gets injected into more server side code is a little weird and seems unlikely to work. What does the SharePoint log say about the list ID that it's trying to load? My guess is that it's not really trying to load the list represented by the GUID that you're setting in your ListGuid variable.
Instead of trying to force some global ListGuid variable to have the correct guid, why not just set the control's ListId property inside your application page's Load event handler?
In the aspx file:
<SharePoint:ListView Id="lv" runat="server" />
In the application page's codebehind:
protected ListView lv
In the Load event:
SPList list = HelpDesk.Lists["Charge Numbers"];
Guid myguid = list.ID;
lv.ListId = myguid.ToString();
I think the error message you are receiving is telling you what's going wrong... the ToString() is converting the GUID to an invalid format. There is an overload for the tostring
list.ID.ToString("N");
list.ID.ToString("D"); (this is the default)
list.ID.ToString("B");
list.ID.ToString("P");
... Try them all

Resources