AEM: How to dynamically get the current page path and pass it to the request parameter for drop down - content-management-system

options=/bin/services/myservlet.GET_DROPDOWN_VALUES.json?locale='some_locale'
if I put locale=en-us it works fine,
How can I dynamically get the current page locale and pass it to the request parameter ?

So, your servlet requires locale as one of the parameters to retrieve dropdown values. I can think of 2 options to do this.
Invoke the servlet with $Path as one of the parameters. $Path will give you the complete page path, all the way till jcr:content. options=/bin/services/myservlet.GET_DROPDOWN_VALUES.json?compNode=$PATH.
In your servlet, construct page object using $PATH, then as #jwepurchase mentioned, get the locale using page.getLanguage(false).
String compNodePath = (String) request.getParameter("compNode");
String pagePath = StringUtils.substringBefore(compNodePath, "jcr:content");
PageManager pageMgr = request.getResourceResolver().adaptTo(PageManager.class);
Page page = pageMgr.getContainingPage(pagePath);
Locale pageLocale = page.getLanguage(false);
getLanguage will look for jcr:language property in currentpage or its ancestors. This property gets set when you set the Language field in page properties (advanced tab) of your page(usually set in the root locale page) to an appropriate value. eg: If you set the language to english(us), jcr:language will be en_us.
Similar steps as option 1, if value in page property is not set, you can use currentPage.getAbsoluteParent(DEPTH_VAL) to retrieve the locale value. Not a recommended soln though.

com.day.cq.wcm.api.Page.getLanguage(false) will return the value of the jcr:language property on the page or the first parent page where it is sent. Generally this isn't set on every page.
If you have included Adobe's global.jsp or used the <cq:defineObjects/> tag in your JSP, you should find that "currentPage" is already in scope, providing access to a Page object.
I generally have a custom taglib function to make it easier to access via EL. But as a scriptlet I expect the following would work:
<%= ((Page)getPageContext().getAttribute("currentPage")).getLanguage(false)%>

Related

Adding current user and current date in the template in Blueprint development

I finished simple, intermediate and advanced blueprint tutorials of the Atlassian https://developer.atlassian.com/server/confluence/tutorials-and-guides/.
Currently, I have a soy file and a js file for Blueprint wizard page; a Java file as context provider and an xml file for template.
I added the current date to the title easily. I also want to insert current date and the current user (page creator) to the page before sending user to edit. (For example; default meeting notes blueprint). However I couldn't find how to set these values in template.
In xml template, the current date should be as following format:
<time datetime="2021-02-17"/>
So, I cannot add something like "<at:var>" for datetime value. So, I cannot change it from the context provider class.
Also, I tried to set this value through Javascript and JQuery, however I just can change things about the wizard page. I failed on manipulating the actual template page.
Nearly same issue with the page creator. I should add the user as following format:
<ri:user ri:userkey="2c9680f7405147ee0140514c26120003"/>
However, I couldn't set the userkey through Java and also I couldn't get the page creator value.
Thus;
How can I add the current date to the template page?
How can I add the user mention to the template page?
How can I get the creator of the page?
Thanks already for all your interests.
Have a nice day!
The answer is coming from me, too.
In template's xml, we are adding two variables for the date and the user with enabled XHTML support:
<at:var at:name="currentDate" rawxhtml=true />
<at:var at:name="currentUser" rawxhtml=true />
In the context provider class, we will set these variables with html tags:
context.put("currentDate", "<time datetime=\" + sdf.format(new Date()) + "\"/>");
context.put("currentDate", "<ri:user ri:userkey=\" + AuthenticatedUserThreadLocal.get().getKey() + "\"/>");
where the context is the context map for blueprint and sdf is the instance of SimpleDateFormat class with a pre-defined date (not date-time) format.

Content altered in HTL/ Sightly in AEM 6

This is the weirdest issue I've ever faced in a long time. I have a URL that is authored inside a multifield. The URL has an underscore eg. http://example.net/_pinkPanther_is_pink it is currently inside ${item.link}
When I do Click <br> ${item.link} and inspect, it renders as
Click
<br> http://example.net/_pinkPanther_is_pink
If you notice both values are coming from the same variable in Sightly still when the link is used inside href of anchor tag there is double underscore added by God know who after example.net/
Does anybody have a clue as to what on earth is going on ?
That's caused by the display context aware XSS protection. Sightly/HTL automatically detects the display context of a HTL expression, using its location within the structure of the HTML page to detect it.
For example, if the expression appears in a place that would produce a text once rendered, then it is said to be in a text context. If it is found within the value of an attribute, then it is said to be in an attribute context, and so forth. More about contexts in the htl specification page.
In your example, the implicit context inside the href attribute is uri while in the later case is text.
In order to overwrite this behaviour, you may explicitly set the context like href="${item.link # context='text'}.

TYPO3 Extbase: Set storagepid for Backend Module

I have written a small extension that provide news for Backend User.
Ist is just 2 Parts. One part showing the news for all BE User as a own Module, and the other part are create and edit functions provided by the TCA.
To work with the TCA forms and the default extbase getter, i need the correct storagePid. I can't set them via TypoScript, because I'm never in a page context.
My Idea was to use the Plugin settings with the file ext_conf_template.txt
# cat=persistence/enable; type=int; label=Storage pid
storagePid = 4457
But how can I tell TYPO3 to look at this Settings?
At least Repository->findAll() must respect it
Normally you would define this using TypoScript:
module.tx_yourextensionkey.persistence.storagePid = 123
Not being in a page context is not a blocker as long as you place the configuration on the first root TypoScript template (or include the TypoScript via other means which cause global inclusion not specific to any sys_template record or page tree location).
Maybe not the best solution, but it works.
I have written my own function in the repository with the pid as parameter.
I'm using the TYPO3 Query builder.
With $query->equals('pid', $pid); I get entries with my pid.
And with $query->getQuerySettings()->setRespectStoragePage(false); the default pid will be ignored.
And as the last step. My Controller gets the pid from the Settings, unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['my_extension']['storagePid']); an gives it to the function.

Select locale using custom URL parameter in GWT

Is it possible to change the name of the URL parameter which tells GWT what language should load to be a custom parameter instead of "locale"?
For example instead of being:
http://myapp.com/?locale=en
it would be:
http://myapp.com/?language=en
I need to change the name because I don't have control over the final host page and the URLs from which the language will be loaded, although they have a specific parameter to choose the locale (which name is not locale, of course).
Add the following to your gwt.xml file:
<set-configuration-property name="locale.queryparam" value="language"/>

Watir-webdriver: how to change attribute value without js/jquery

How can I change a href attribute value using watir-webdriver without using js/jquery?
I can get an attribute value:
#browser.frames[2].div(:id,"mid-2").link(:class,"btn-lrg").attribute_value("href")
But I also need to change a bit of the href attribute value.
I think that the only way to modify the link is to use javascript. The code is quite maintainable since the element is retrieved using watir.
#Get the first link (or any element you want)
element = browser.frame.link
#Check element's initial attribute
puts element.attribute_value('href')
#=> "page_a.html"
#Execute javascript to change the attribute
script = "return arguments[0].href = 'page_b.html'"
browser.execute_script(script, element)
#Check that the attribute has changed
puts element.attribute_value('href')
#=> "page_b.html"